#!/bin/bash
############################################################################
############ shadow.sh, version 0.02, 5/19/2002, Ray Yeargin ###############
##########  Copyright 2002, Ray Yeargin.    Released under the  ############
##########  terms of the GNU General Public License, version 2  ############
############################################################################
########## This script should be run from cron, probably hourly ############
## It keeps a backup of a source directory in a backup (target) directory ##
## with several versions of changed files, marked by subscripts .0, .1... ##
### The home web page for this script is http://librenix.com/?inode=1953 ###
############################################################################
if [ $# = 2 ]  #base level call... create base dir, call $ME to cp, mkdirs
then
   /bin/mkdir $2 > /dev/null 2>&1
   cd $1
   /usr/bin/find . -type d -exec /bin/bash $0 $1 $2 {} bogoarg \; > /dev/null 2>&1
   /usr/bin/find . -type f -exec /bin/bash $0 $1 $2 {} \;
else
   if [ $# = 3 ]  # compare, roll, and cp files, as needed
   then
      DNAME=`dirname $3`
      FNAME=`basename $3`
      /usr/bin/cmp $1/$DNAME/$FNAME $2/$DNAME/$FNAME > /dev/null 2>&1
      if [ $? != 0 ] 
      then
         /bin/mv $2/$DNAME/$FNAME.2 $2/$DNAME/$FNAME.3 > /dev/null 2>&1
         /bin/mv $2/$DNAME/$FNAME.1 $2/$DNAME/$FNAME.2 > /dev/null 2>&1
         /bin/mv $2/$DNAME/$FNAME.0 $2/$DNAME/$FNAME.1 > /dev/null 2>&1
         /bin/mv $2/$DNAME/$FNAME $2/$DNAME/$FNAME.0 > /dev/null 2>&1
         /bin/cp -p $1/$DNAME/$FNAME $2/$DNAME/$FNAME > /dev/null 2>&1
      fi
   else 
      if [ $# = 4 ]  # create directory names, whether needed or not
      then
         DNAME=`dirname $3`
         cd $2/$DNAME
         /bin/mkdir `basename $3`
      else
         echo
         echo "usage: $0 /sourcedir /targetdir"
         echo
         echo "All three arguments must be full path names"
         echo "    withOUT trailing /'s on directory names."
         echo
         echo "Ensure there is no overlap between the source"
         echo "    and target directories!!"
         echo
      fi
   fi
fi
