#! /bin/ksh
# copy_depending.ksh
# If filesize too small and the filesize of something else okay and filesize of first is 
#   less than minbytes copy second onto first ksh script tutorial
# Possible usage is that parameter two file is a backup of parameter 1 file and in the 
#   meantime a bad change happened to file 1 causing it to become smaller than 
#   parameter 3 minimum bytes setting ... this script gets it back to the way it was
#   as the backup happened

if [ ! -f "$1" -o ! -f "$2" -o -z "$3" ]; then
  echo "Usage: copy_depending.ksh [file1] [file2] [minbytes]  # if filesize too small and\
 the filesize of something else okay and filesize of first is less than minbytes\
 copy second onto first";
else

  size1="`stat $1 | cut -d' ' -f 8`";
  size2="`stat $2 | cut -d' ' -f 8`";
  b="`basename $1`"
  if [ $size1 -lt $3 ]; then
   if [ $size1 -lt $size2 ]; then
    if [ $1 -nt $2 ]; then
     msg="New $1 from $2 (backup, perhaps) and will email contents of the clobbered $1";
     cat "$1" | uuencode "$b" | mailx -s "$msg" rmetcalfe15@gmail.com;
     cp -f $2 $1
    fi
   fi
  fi
fi

exit

