#!/bin/bash
#
# Issue assorted VPD collection tasks that can only be executed
#  with root privilege 
#
# Description: Issue to run privileged VPD commands 
#
# Change Activity:
#   09/15/2006 ferrants    570565 - initial version
#
#STARTUSAGE
#
# Usage:
#   vpdCommandUtil -c copyHome -f <vpd data file> -u <userId>
#                  -c doChecksum -f <vpd data file> -m <vpd file on media>
#
# Where:
#   -c   The operation to be performed.  Must be one of:
#        . doChecksum - verifies the VPD data was copied properly to the
#                       external remote media
#        . copyHome - copies the compressed VPD *.zip file to the currently
#                     logged in userId's home directory. This is a "bonus"
#                     for service personnel who normally just 'ssh' into
#                     remote systems.
#
#   -f   The compressed VPD data file just collected
#   -m   The vpd file on the remote media
#   -u   The current userid
#
# Return Codes:
# 10 - checksum mismatch between VPD file on disk and on media
# * - 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"
}

# Initialize variables that hold option values 
giveUsage=0
cmd=
file=
userid=
mediafile=

# Parse the options
while getopts 'c:f:m:u:?' optname; do
   case "$optname" in
      c) cmd="$OPTARG";;
      f) file="$OPTARG";;
      m) mediafile="$OPTARG";;
      u) userid="$OPTARG";;
      \?) giveUsage=1; break;;
   esac
   noopt=0
done

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" == "copyHome" ]; then
   # This request will write the description file to stdout
   if [ -z "$file" ]; then
      echo "Missing required -f argument"
      showUsage
      exit 99
   fi
   if [ -z "$userid" ]; then
      echo "Missing required -u argument"
      showUsage
      exit 99
   fi
   
   # do the file copy and set the file permissions/ownership
   cp $file /home/$userid/
   cpRC=$?
   
   if [ $cpRC -eq 0 ]; then
      homefile=`basename $file`
      chmod 644 /home/$userid/$homefile
      chown $userid.users /home/$userid/$homefile
      chownRC=$?
      if [ $chownRC -eq 0 ]; then
         exit 0
      else
         exit $chownRC
      fi
   else
      exit $cpRC 
   fi
   
elif [ "$cmd" == "doChecksum" ]; then
   # verify checksum values
   if [ -z "$file" ]; then
      echo "Missing required -f argument"
      showUsage
      exit 99
   fi
   if [ -z "$mediafile" ]; then
      echo "Missing required -u argument"
      showUsage
      exit 99
   fi
   
   realsum=`sum $file`
   
   # flush media data first, then calculate checksum
   /bin/sync
   mediasum=`sum $mediafile`
   
   if [ "$realsum" == "$mediasum" ]; then
      exit 0
   else
      # checksum mismatch
      exit 10
   fi

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