#!/bin/bash
################################ backup.sh #####################################
# Synopsis : Create cpio archives 
# Writen by: Thomas Ruschival - ruschi@doctordoom.shacknet.nu 
# License: GPL
# CVS:
# $Author: ruschi $
# $Date: 2003/10/29 22:38:56 $
# $Revision: 1.1.1.1 $
################################################################################   

##
# function faulty_call(error)
# exits the script returning the proper usage and the error that occurred when 
# processing the command line parameters
##
faulty_call()
{
echo "`basename $1`  -f backupdir [-e exclude] [-l level] -d device ";
echo $2;
exit 1;
}

##
#  function is_switch tests whether an argument starts with a "-"
##
is_option()
{
if [ -n "`echo '$1' | egrep '^-'`" ]
then
	return 1
else
	return 0
fi
}

###  declaration    ###
 declare EXCLUDE
 declare BULEVEL
 declare DIRECTORY
 declare OUTPUT
 declare NEWER
 declare PREVIOUS
 declare TOC
################

if [ $# -lt 2 ] 
then
	faulty_call $0
fi

# if [ $UID -ne 0 ]
# then
#     echo "only the superuser may perform backups"
#     exit 
# fi

## Error handling and Comand line parsing
while [ $# -gt 0 ]
do
  case "$1" in
    -e)
    # the User has specified a backup list, make sure this file exists
      if [ -e $2 ]
      then
        EXCLUDE=" grep -v -f $2 |"
        EXCLUDE=$2
				shift
      else
        faulty_call $0 "FILE $2 does not exist"
      fi    
    ;;
    -f)
	SDIR=`echo $2`
	DIRECTORY=`echo $SDIR | awk '{print $1}'`
	echo "DIR: $DIRECTORY"
	shift
    ;;
    -l)
    ##
    # the user has specified a level test wheter ther is a previous level
    ##
       let PREVIOUS=$2-1
       if [ -e $DIRECTORY/.backupd/cpiostamp.${PREVIOUS} ]
       then
	    BULEVEL=$2
	    NEWER="-newer ${DIRECTORY}/.backupd/cpiostamp.${PREVIOUS}"
       else
	    echo "This Directory was not previously backed up, performing Level 0 Backup"
	    BULEVEL=0
       fi
       shift
     ;;
    -d)
    ##
    # the user has specified a device or file, we don't know, so test it
    # lets assume things in the /dev directory are devices so we have to check
    # wether it is responding or something is bogus.
    ## 	
      if [ -n "`echo $2 | grep '^/dev/'`" ]
      then 
	MTTEST=`mt -f $2 status`
	OUTPUT=$2
      else
	OUTPUT=$2
	touch $OUTPUT
      fi
      shift
    ;;
    *)  
	faulty_call $0 "something else is bogus"
	break # terminate while loop
       ;; 
    esac
    shift
done

echo BULEVEL:   $BULEVEL
echo DIRECTORY: $SDIR
echo OUTPUT:    $OUTPUT
echo EXCLUDE:   $EXCLUDE

echo 

## Create name of TOC file
TOC="${DIRECTORY}/.backupd/TOC.`date +%a.%d.%m.%y`"

# Was this folder ever backed up?
if [ ! -d $DIRECTORY/.backupd/ ]
then
    mkdir  $DIRECTORY/.backupd 
fi

##
# Test whether there  is an exclude if yes we have to extend the backup
# command with another grep that excludes 
# the files mathching the entries in the excludefile
# else we can go on and perform a regular backup
##
if [ -n $EXCLUDE ]
then
	find ${SDIR} ${NEWER} -print | grep -v -f ${EXCLUDE} | cpio -ocav -B -O ${OUTPUT} 2> ${TOC}
else
	find ${SDIR} ${NEWER} -print | cpio -ocav -B -O ${OUTPUT} 2> ${TOC}
fi

## At last we make a timestamp for the next backup
touch  $DIRECTORY/.backupd/cpiostamp.$BULEVEL













