# zip - a Qshell script to compress and restore iSeries save files
#  Written by Michael Swenson


# Check for validity of parameter one, it must be a c to compress or r to restore
# At the same time, check the number of parameters and display usage info if there are
# too few or too many parameters.

declare -u FUNCTION="$1"
if [[ $FUNCTION == C && $# == 3 ]] ; then echo Compressing... ; declare -i VALID1=1 ; fi
if [[ $FUNCTION == R && $# == 2 ]] ; then echo Restoring... ; declare -i VALID1=1 ; fi
if [[ $FUNCTION == D && $# == 2 ]] ; then echo Displaying... ; declare -i VALID1=1 ; fi

if [ ${VALID1:-0} -ne 1 ]
   then 
   echo
   echo "Usage: zip {c|r|d} zipfile [LIBRARY/FILE] "
   echo "Options: "
   echo "  -c  create a new zipfile (jar file)"
   echo "  -r  restore the contents of zipfile (jar file)"
   echo "  -d  display the contents of zipfile (jar file)"
   echo
   echo " Example 1:  to compress a save file, QGPL/MYSAVF into a file named fluffy.jar:"
   echo "    zip c fluffy.jar qgpl/mysavf"
   echo " Example 2:  to restore a save file from a file named squished.zip:"
   echo "    zip r squished.zip"
   echo " Explicit or relative paths may be used (I hope):"
   echo "    zip c /qibm/smashed.flat mylibrary/mysavf"
   echo "    zip r ./mysubdirectory/savf.jar"
   echo " Example 3:  to display the contents of a zip file named squished.zip:"
   echo "    zip d squished.zip"
   echo 
   echo " Questions?  Comments?  Grumbles?  Gripes?  Send them to me: mikswens@us.ibm.com"
   echo "    ...they will be ignored in the order recieved :-)"
   exit 1;
fi


# Check the validity of the jarfile parameter.  If it exists and the fuction is compression,
# then prompt to overwrite the file or not.
#  For restore operations the jarfile must be converted to an explicit path.
JARFILE=$2
if [[ -e "$JARFILE" && $FUNCTION == C ]]
   then 
      echo "File $JARFILE already exists, overwrite existing file? (Y,N)"
      read OVRWRT
      declare -u UOVRWRT=$OVRWRT
      if [ "$UOVRWRT" == Y ]
         then
         echo "...overwriting file."
      elif [ "$UOVRWRT" == N ]
         then
         echo "OK.  exiting zip function."
         exit 2
      else
         echo "What was that supposed to mean?  The choices are Y or N."
         exit 10
      fi
fi

if [ ! -e "$JARFILE" ]
   then
	   if [ "$FUNCTION" == R ]
		   then
         echo "File $JARFILE not found.  Nothing to restore."
         exit 3
		elif [ "$FUNCTION" == D ]
		   then
		   echo "File $JARFILE not found.  Nothing to display."
			exit 3
		fi
else  # put an explicit path onto the jarfile
   if [[ ${JARFILE:0:2} == ./ ]]
      then
      JARFILE=$PWD/${JARFILE#./}
   elif [[ ${JARFILE:0:1} != /  ]]
      then
      JARFILE=$PWD/$JARFILE
   fi
fi


# Compress the file

if [[ $FUNCTION == C ]]
   then
   LIBFILENAME=$3
   declare -u IFSPATH="/QSYS.LIB/${LIBFILENAME%/*}.LIB/${LIBFILENAME#*/}.FILE"
   if [[ ! -e "$IFSPATH" ]]
      then
      echo "File $IFSPATH does not exist.  Nothing to compress."
      exit 3
   else
      jar cvfM $JARFILE $IFSPATH
   fi
fi


# Restore the file

if [[ $FUNCTION == R ]]
   then
   cd / ; jar xvf $JARFILE 
fi


# Display jar file contents

if [[ $FUNCTION == D ]]
   then
   echo jar file $JARFILE
   echo "_________________________________________________________"
   jar tf $JARFILE
   echo "_________________________________________________________"
fi