#!/bin/bash
#shell script to zip up system dumps that are stored on the HMC
#The scripts can run prompted or unprompted(quiet)
# 
# Mode =collection mode
# MODE=0 prompted collection
# MODE=1 collect all system dump types 
# MODE=2 collect FSPDUMP
# MODE=3 collect SYSDUMP
# MODE=4 collect LOGDUMP
# MODE=5 collect SMADUMP
# MODE=6 collect PWRDUMP
# MODE=7 delete zips only
# MODE=8 delete all system dumps 
# MODE=9 run copy utility  

#set -x
cleanup()
{
  rm -f /$ZIPDIR/$basename* 2>/dev/null
  exit 0

}


copytodvd()
{
  #clear
  echo ""
  echo " Would you like to move zip file to a DVD or other device? "
  echo " The answer no will write to the HMC hardfile"
  echo " Please type yes or no."
  read -e answer
  if [[ $answer = yes || $answer = y ]]; then
     FILESIZE=`ls -al $COPYFILE | awk {'print $5'}`
     echo ""
     echo "The file is $FILESIZE bytes. Ensure you have enough room on the device."
     echo "Use formatted Read/Write media only"
     echo "Enter a device name from the list below. Example /media/cdrom "
     echo ""
     /usr/hmcrbin/lsmediadev -F mount_point=description | grep -v null
     echo "network=move to another system via the scp command"
     echo ""
     
     read -e answer1

     INPUT=`/usr/hmcrbin/lsmediadev -F mount_point | grep "$answer1"`
     echo 
     if [ "$answer1" == "$INPUT" ] 
     then
         umount $answer1  2>/dev/null
         mount $answer1  2>/dev/null
         cp $COPYFILE $answer1
         if [[ $? -eq 0 ]];then
            echo $COPYFILE placed on $answer1. 
            umount $answer1
         else
            echo "write error to $answer1"
            echo "$COPYFILE is in the /$ZIPDIR directory"
         fi
     elif [ "$answer1" == "network" ] ;then
         echo "     Enter the user name on the destination host."
         read -e username   
         echo "     Enter the destination host. example myhost.mycompany.com"
         read -e filehost   
         echo "     Enter the destination filesystem on the host. example /tmp/hmcdir"
         echo "     The directory must exist and have correct permissions."
         echo ""                                                    
         read -e filedir   
         echo "     scp $COPYFILE $username@$filehost:$filedir"
         scp $COPYFILE $username@$filehost:$filedir
         if [[ $? -eq 1 ]];then
            echo
            echo "     Command failed $COPYFILE is in the /$ZIPDIR directory."
         else
            echo "The send was successful"
         fi
    
     else
        echo "     \"$answer1\" was not a valid device command or could not be mounted"
        echo "    $COPYFILE is in the /$ZIPDIR directory."
     fi
  else
   echo "     $COPYFILE is in the /$ZIPDIR directory."
  fi
}

collectSYSdump()
{

   #clear
   DUMPFILE=`ls /dump/$DUMPTYPE* 2> /dev/null`
   if [[ $? -eq 0 ]]; then 
       echo;echo "Collecting $DUMPTYPE files"
   else
       echo;echo "$DUMPTYPE files not found"
   fi
   for j in $DUMPFILE
   do 
     zip -gqr /$ZIPDIR/$LOGFILENAME.zip $j
     COLLECTED=1
   done

}

collectpromptedSYSdump()
{
   #clear
   for i in  "FSPDUMP" "SYSDUMP" "LOGDUMP" "SMADUMP" "PWRDUMP"
   do

       DUMPFILE=`ls /dump/$i* 2> /dev/null`

       if [ "$DUMPFILE" != "" ]; then
           echo;echo "Collect $i logs? Please type yes or no"
           read -e answer
           if [[ $answer = yes || $answer = yes ]]; then

                  for j in $DUMPFILE
                  do 
                        zip -gqr /$ZIPDIR/$LOGFILENAME.zip $j
                        COLLECTED=1
                  done
           fi
       fi
   done

}

deleteALL()
{
   #clear
   echo ""
   echo " Warning, this operation deletes all existing system dumps on this HMC "
   echo ""
   echo ""
   echo "Please type yes to continue"
   read -e answer
   if [[ $answer = yes ]]; then
           echo " Are you sure ? Please type yes to continue "
           read -e answer
           if [[ $answer = yes ]]; then
                   for i in  "FSPDUMP" "SYSDUMP" "LOGDUMP" "SMADUMP"  "PWRDUMP"
                   do
                      rm /dump/$i* 2> /dev/null
                   done
           else
              echo "No dumps removed."
           fi
  else
     echo "No dumps removed."
  fi

}

runquiet()
{
   case $MODE in
     1 ) DUMPTYPELIST="FSPDUMP SYSDUMP LOGDUMP SMADUMP PWRDUMP"
         for i in  $DUMPTYPELIST
         do
           DUMPTYPE=$i
           collectSYSdump 
         done;;
     2 ) DUMPTYPE="FSPDUMP"
         collectSYSdump ;;
     3 ) DUMPTYPE="SYSDUMP"
         collectSYSdump ;;
     4 ) DUMPTYPE="LOGDUMP"
         collectSYSdump ;;
     5 ) DUMPTYPE="SMADUMP"
         collectSYSdump ;;
     6 ) DUMPTYPE="PWRDUMP"
         collectSYSdump ;;
     7)  cleanup ;;   # Deletes .zip files that were created by this tool.
     8 ) deleteALL;;  # Deletes all system dumps
     9 ) runcopyprompt ;;
     * ) #Mode not supported. Set to 0 and continue prompted operation
         echo;echo "\"$MODE\" is not a valid option, switching to prompted mode."
         echo 
         MODE=0 ;;
   esac

   if [[ "$MODE" != "0"  ]] ; then

       if [[ "$COLLECTED" == "1" ]]; then
             echo;echo "$LOGFILENAME.zip is in /$ZIPDIR directory"
       else
             echo;echo "No files found or collected"
       fi
       exit 0
   fi

} 
     
runcopyprompt()
{
   LOGFILESFOUND=`ls /dump/$basename* 2> /dev/null`
   if [ "$?" == "0" ]; then
      for i in $LOGFILESFOUND
      do
         COPYFILE=$i
         echo;echo "Found $COPYFILE"
         copytodvd 
      done
   else
      echo;echo "No files that begin with $basename found"
           echo "No logs were collected"
   fi
  
   exit 0
}


#MAIN =============================================================


ZIPDIR="dump"
JAVACORE=0
COLLECTED=0
uid=`id -u`
if [ $uid -ne 0 ]
then 
  echo "You need to be root to run the copyLogs script"
  exit 0
fi

trap "cleanup 1" 2 3 5 9 11 15
stty -crterase
datelabel=`date +%m%d%Y%H%M`
hmcname=`hostname`
basename="HMCSysdump."

LOGFILENAME=$basename$hmcname$datelabel

#delete existing zipfile
rm  /$ZIPDIR/$basename$hmcname* 2> /dev/null


if [[ $# -ne 1 ]]; then
  MODE=0
else
  MODE=$1
fi


if [[ "$MODE" != "0" ]]; then
    runquiet
fi 

echo;echo "Collecting system dumps. "
echo 

collectpromptedSYSdump
runcopyprompt
exit 0
