#!/bin/sh
#
# This will process a file that specifies which files should be saved
# if they exist.
#
# The file format is...
#
#	directory	< restore | remove | ignore >
#
#		restore	- replace the new directory with the old
#		remove  - remove any existing directory
#		ignore  - just ignore it (removing from this list is
#			  the same thing)
#

Usage="Usage: $0 <mode> <save-file> <dest-dir> <packages> [audit-file]"

if [ $# -lt 2 ]
then
    echo $Usage; exit 0
fi

if [ $1 = "-s" ]
then
    ScriptTag="SAVE_FILES"
fi
 
if [ $1 = "-r" ]
then
   ScriptTag="RESTORE_FILES" 
fi

SAVEFILES=$2
DESTDIR=$3
PACKAGES=$4
AUDITLOG=$5

if [ ! -f "$SAVEFILES" ]
then
    echo "WARNING: Save file $SAVEFILES does not exist..." 
    exit 1
fi

#
# If we are reading an audit file that indicates that we have gotten
# this far before, then skip execution and exit.
#

if [ "$AUDITLOG" ]
then
    PREVRUN=`grep $ScriptTag $AUDITLOG`

    if [ "$PREVRUN" ]
    then
    	exit 1
    fi
fi

#
# for each entry in the save file list, rename any existing corresponding
# file to designated name. If 'restore', these files will be restored
# and any files that have been taken off the media that share those
# file names will have the data and time appended to their names. During
# the restore process, designated by -r, the files are restored to
# there original name.
#
SAVETAG="# "$ScriptTag
DATE=`date +%m%d%y%H%M`

if [ $1 = "-s" ]
then
    awk '/#.*$/ {next}
        /^[\ \t]*$/ {next}
    	{
	  if ($2 == "restore")
            {
	      restoresrc = dest "/" $1
	      if (NF > 2) 
		temp = $3;
	      else 
		temp = $1 ".save"
	      restoredest = dest "/" temp

	      print "if [ ! -d " restoredest " ] "
    	      print "then"
              print "   if [ -d " restoresrc " ] "
    	      print "   then"
	      print "       mv " restoresrc " " restoredest
    	      print "   fi"
    	      print "fi"
	    }
	  if ($3 == "prestore")
            {
	      restoresrc = dest "/" $1
	      if (NF > 4) 
		temp = $5;
	      else 
		temp = "prestore.save"
	      restoredest = dest "/" temp

	      print "if [ ! -d " restoredest " ] "
    	      print "then"	
	      print "	mkdir -p " restoredest
              print "   cp " restoresrc "/" $2 " " restoredest
    	      print "fi"
	    }
	  if ($2 == "save") 
	    {
	      savesrc = dest "/" $1 
	      savedest = dest "/" $1 "." DateStamp 
              print "   if [ -d " savesrc " ] "
    	      print "   then"
    	      print "       mv " savesrc " " savedest
	      print "	    echo \"Moving " savesrc " to " savedest "\""
    	      print "   fi"
	    }
	  if ($3 == "psave") 
	    {
	      savesrc = dest "/" $1 
	      savedest = dest "/" $1 "." DateStamp 
              print "   if [ -d " savesrc " ] "
    	      print "   then"
	      print "       if [ ! -d " savedest " ] "
	      print "	    then"
	      print "	        mkdir -p " savedest
	      print "       fi"
    	      print "       cp -r " savesrc "/" $2 " " savedest
	      print "	    echo \"Copying " savesrc "/" $2 " to " savedest "\""
    	      print "   fi"
	    }
        } ' dest=$DESTDIR DateStamp=$DATE $SAVEFILES | sh
fi

if [ $1 = "-r" ]
then
    awk '/#.*$/ {next}
        /^[\ \t]*$/ {next}
	{
	  if ($2 == "restore")
            {
	      restoredest = dest "/" $1
	      if (NF > 2) 
		temp = $3;
	      else 
		temp = $1 ".save"
	      restoresrc = dest "/" temp

              print "if [ -d " restoresrc " ] "
    	      print "then"
	      print "    if [ -d " restoredest " ] "
    	      print "    then"
	      print "        mv " restoredest " " restoredest "." DateStamp
	      print "        mv " restoresrc  " " restoredest
	      print "	     echo \"Restoring old " restoredest " and moving\""
	      print "	     echo \"    installed copy to " restoredest "." DateStamp "\""
    	      print "    fi"
    	      print "fi"
	    }
	  if ($3 == "prestore")
            {
	      if (NF > 4) 
		temp = $5;
	      else 
		temp = "prestore.save"

              print "if [ -d " dest "/" temp " ] "
    	      print "then"
	      print "    if [ ! -d " dest "/" $4 " ] "
	      print "    then"
	      print "        mkdir -p " dest "/" $4
	      print "    fi"
	      print "    if [ -d " dest "/" $1 " ] "
	      print "    then"
	      print "        cp " dest "/" $1 "/" $2 " " dest "/" $4
	      print "        cp " dest "/" temp "/" $2 " " dest "/" $1 
	      print "	   echo \"Restoring old " dest "/" $1 "/" $2 " and\""
	      print "	 echo \"    moving installed copies to " dest "/" $4 "\""
    	      print "    fi"
	      print "    rm -rf " dest "/" temp
    	      print "fi"
	    }
        } ' dest=$DESTDIR DateStamp=$DATE $SAVEFILES | sh
fi

#
# Put the appropriate entries into the audit-log
#

if [ "$AUDITLOG" ]
then
#   put entry into the audit log, this not only indicates that the
#   script has been run, but can be viewed to see what files were
#   selected.
#
    echo $SAVETAG		>> $AUDITLOG
    echo ""			>> $AUDITLOG
    echo ""
    awk '/#.*$/ {next}
        /^[\ \t]*$/ {next}
        { print $1
    	} ' $SAVEFILES 		>> $AUDITLOG
    echo ""			>> $AUDITLOG
fi

