#!/bin/bash
#
# Description: Issue to run privileged 'tar' commands and output both
#              stdOut and stdErr to invoking process
#
# Change Activity:
#   05/07/2007 ferrants    xxxxxx - initial version
#
#STARTUSAGE
#
# Usage:
#   tarUtil -c listFiles -a <tar archive file name>
#   tarUtil -c extractFile -a <tar archive file name> -f <file to extract>
#   tarUtil -c restoreArchive -a <tar archive file name>
#   tarUtil -c excludeFile -a <tar archive file name> -x <file to exclude>
#
# Where:
#   -c   The operation to be performed.  Must be one of:
#        . listFiles - executes the 'tar' command with options '-tf'
#        . extractFile - executes the 'tar' command with options '-xPf' <filename(s)>
#        . restoreArchive - executes the 'tar' command with options '-xPf'
#        . excludeFile - executes the 'tar' command with options '-xPf' -X <filename(s)>
#
#   -a   The input tar archive file to process
#   -f   The file(s) to specifically extract from the archive file
#   -x   The file(s) to spceficially exclude when unpacking the archive file
#
# Return Codes:
# 99 - usage syntax error
# * - other system command return codes
#
#ENDUSAGE
#****************************************************************************

function showUsage() {
   # Print out the prologue comments as usage info
   sed -e '/STARTUSAGE/,/ENDUSAGE/ s/^#//' -e '1,/STARTUSAGE/ d' -e '/ENDUSAGE/,$ d' "$0"
}


#-------------------------------------------------------------------------------
# common exit point for tarUtil processing
#-------------------------------------------------------------------------------
exit_cleanup() {

   if [ $1 -eq 0 ]; then
      echo "'tar' command, <$cmd>, succeeded, exit code = 0." >> $log
   else
      echo "'tar' command, <$cmd>, failed and exited with rc=$1." >> $log
   fi

   # return the stderr from the 'tar' command no matter what the exit value
   if [ -f $stdErr ]; then
      cat $stdErr > /dev/stderr
   fi

   # remove temporary stdOut/stdErr files
   rm -f $stdOut
   rm -f $stdErr

   echo "tarUtil script end execution on `date`" >> $log
   exit $1
}

#-------------------------------------------------------------------------------
# funtion for concatenating tar output to current log file
#-------------------------------------------------------------------------------
appendOutputToLog() {

   # $1 is redirected stdOut from tar command
   # $2 is redirected stdErr from tar command

   if [ -f $1 ]; then
      y=`ls -l $1 | awk '{print $5}'`
      if [ "$y" -gt 0 ]; then
         cat $1 >> $log
      else 
         echo "No stdOut returned from 'tar' command..." >> $log
      fi
   else
      echo "No stdOut generated from 'tar' command." >> $log
   fi

   if [ -f $2 ]; then
      y=`ls -l $2 | awk '{print $5}'`
      if [ "$y" -gt 0 ]; then
         cat $2 >> $log
      else 
         echo "No stdErr returned from 'tar' command..." >> $log
      fi
   else
      echo "No stdErr generated from 'tar' command." >> $log
   fi

   echo -e "\n" >> $log

   return 0
}

#-------------------------------------------------------------------------------
# function for returning stdout from 'tar' command
#-------------------------------------------------------------------------------
echoStdOut() {

   # $1 is redirected stdOut from tar command
   # $2 is return code from tar command

   if [ $2 -eq 0 ]; then
      y=`ls -l $1 | awk '{print $5}'`
      if [ "$y" -gt 0 ]; then
         # echo to stdOut
         cat $1
      else 
         echo "'tar' command rc=0, and no stdOut returned." >> $log
      fi
   else
      echo "Non-zero 'tar' return code - do not echo stdOut." >> $log
   fi

   return 0
}

#-------------------------------------------------------------------------------
# Just in case we have NLS troubles reading system information...
#-------------------------------------------------------------------------------
LANG=en_US
export LANG

# Initialize some constants
log='/var/hsc/log/tarUtil.log'
stdOut='/tmp/tar.stdOut'
stdErr='/tmp/tar.stdErr'

# Initialize variables that hold option values 
giveUsage=0
cmd=
archive=
file=
excludeFile=

# Parse the options
while getopts 'c:a:f:x:?' optname; do
   case "$optname" in
      c) cmd="$OPTARG";;
      a) archive="$OPTARG";;
      f) file="$OPTARG";;
      x) excludeFile="$OPTARG";;
      \?) giveUsage=1; break;;
   esac
   noopt=0
done

echo "tarUtil script begin execution on `date`" > $log
echo "args are: cmd=$cmd, archive=$archive, file=$file, and excludeFile=$excludeFile" >> $log

if [ "$giveUsage" -eq 1 ]; then
   showUsage
   exit 99
fi
      
if [ -z "$cmd" ]; then
   echo "Missing required argument command argument"
   showUsage
   exit 99
fi

#
# Handle the various requests
#

if [ "$cmd" == "listFiles" ]; then
   if [ -z "$archive" ]; then
      echo "Missing required -a argument"
      showUsage
      exit 99
   fi

   # list the file in the archive, invoking procedure should be listening for stdout
   tar -tf $archive 1>$stdOut 2>$stdErr
   tarRC=$?
   appendOutputToLog $stdOut $stdErr
   echoStdOut $stdOut $tarRC
   exit_cleanup $tarRC

elif [ "$cmd" == "extractFile" ]; then
   if [ -z "$archive" ]; then
      echo "Missing required -a argument"
      showUsage
      exit 99
   fi
   if [ -z "$file" ]; then
      echo "Missing required -f argument"
      showUsage
      exit 99
   fi

   # extract the specific file in the archive
   tar -xPf $archive $file 1>$stdOut 2>$stdErr
   tarRC=$?
   appendOutputToLog $stdOut $stdErr
   echoStdOut $stdOut $tarRC
   exit_cleanup $tarRC

elif [ "$cmd" == "restoreArchive" ]; then
   if [ -z "$archive" ]; then
      echo "Missing required -f argument"
      showUsage
      exit 99
   fi

   # extract all files in the archive
   tar -xPf $archive 1>$stdOut 2>$stdErr
   tarRC=$?
   appendOutputToLog $stdOut $stdErr
   echoStdOut $stdOut $tarRC
   exit_cleanup $tarRC

elif [ "$cmd" == "excludeFile" ]; then
   if [ -z "$archive" ]; then
      echo "Missing required -f argument"
      showUsage
      exit 99
   fi
   if [ -z "$excludeFile" ]; then
      echo "Missing required -x argument"
      showUsage
      exit 99
   fi

   # extact all files in the archive except for those listed
   tar -xPf $archive -X $excludeFile 1>$stdOut 2>$stdErr
   tarRC=$?
   appendOutputToLog $stdOut $stdErr
   echoStdOut $stdOut $tarRC
   exit_cleanup $tarRC

else
   echo "Unknown command verb: $cmd"
   showUsage
   rc=98
fi
