#!/bin/sh
#
# Written by Holger Nassenstein (h.nassenstein@gmx.de)
#
# Copyright 1999 Holger Nassenstein, distributed under the terms of GNU GPL
#

CFG=/etc/backuponcd
GLOBAL=$CFG/global.rc

function die ()
{
  if test "$1"; then
    echo -e "\a\n $1, exiting."
  fi
  rm -R $arch_dir > /dev/null 2>&1
  exit 1
}

function help ()
{
  echo -e "\nUsage: $(basename $0) -s directory -e excludelist [-a archiver] [-rc settings]
\n       -s directory which is to archived
       -e excludelist may be a empty file, usually a list of mountpoints,
          view example in /etc/backuponcd/excludelist
       -a archiver (optional) overrides your setting (archiver=..)
          in $GLOBAL
       -rc global settings (optional).
           Overrides built in parameter \"-rc $GLOBAL\".
           Must be full path to rc file.
\n      See /usr/share/doc/backuponcd-*/ for a pretty fine manual"
}

function getparam ()
{
  local ret=$(echo $2 | sed -e 's/^[:space:]*'$1'//1')
  if ! test "$ret"; then
    ret="$3"
  fi
  echo $ret
}

while test $# -gt 0; do
  case "$1" in
  -a*) 
        carchiver=$(getparam -a $1 $2)
        ;;
  -e*)
        excludesource=$(getparam -e $1 $2) 
        ;;
  -s*) 
        source=$(getparam -s $1 $2)
        ;;
  -rc*)
        GLOBAL=$(getparam -rc $1 $2)
        ;;
  -h*|--h*|-H*|--H*) 
	help; die
        ;;
  esac
shift
done

error=""

if test -f "$GLOBAL"; then
  source $GLOBAL
else
  error="rc file $GLOBAL does not exist"
fi

if ! test -f "$excludesource"; then
  error="$(echo $error) \nNo exclude list"
fi   

if test -d "$source"; then
  cd $source
else 
  error="$(echo $error) \nNo source directory"
fi   

if test -x "$(whereis -b $carchiver | awk '{print $2}')"; then
  case "$archiver" in
   afio)
     archiver=$carchiver
     ;;
   tar)
     archiver=$carchiver
     ;;
  esac
fi

if ! test -x "$(whereis -b $archiver | awk '{print $2}')"; then
  error="$(echo $error) \nArchiver $archiver not found"
fi

if test "$error"; then
  help; die "$error"
fi

free=$(expr $(df $TMP | grep / | awk '{print $4}') - $(expr 2 \* $tracksize \* 1024))
if test $free -lt 1000; then
  die "Not enough disk space available \
\n Your setting tracksize=$tracksize (Mbyte), needed: (2 * tracksize) $(expr 2 \* $tracksize) Mbyte \
\n Available under TMP=$TMP: $(expr $(df $TMP | grep / | awk '{print $4}') / 1024) Mbyte"
fi

mkdir $arch_dir
mkdir $log_dir
mkdir $script_dir
excludelist=$log_dir/excludelist
arch_name=$archiver.gz

echo "/^[:space:]*#/d
s/#.*//
s/[:space:]*$//
/^[:space:]*$/d" > $log_dir/sedscr

case "$archiver" in
 afio)    
    arch_name=gz.$archiver
    echo $afioexcludes > $notcomp
    echo "s/^/\^/" >> $log_dir/sedscr
 ;;
 tar)
    echo "s/\/$//" >> $log_dir/sedscr
 ;;      
 *)
    die "Current possible archiver tar or afio, edit $GLOBAL or set parameter -a"
 ;;
esac

sed -f $log_dir/sedscr $excludesource > $excludelist
rm $log_dir/sedscr

if test $(basename $source) == "/"; then 
  volume_name="_root-tree_$(date +$dateformat)_disk_"
else
  volume_name="_$(basename $source)_$(date +$dateformat)_disk_"
fi

cat << EOF > $variable
arch_name=$arch_name
archive=$arch_dir/$arch_name
end_mark=$arch_name.end_mark
toast_dir=$toast_dir
logfile=$logfile
dev="$dev"
devdepend="$devdepend"
ejectcmd="$ejectcmd"
loadcmd="$loadcmd"
volume_name="$volume_name"
tracksize=$tracksize
tracks=$tracks
rawdevice=$rawdevice
mountpt=$mountpt
EOF

$ejectcmd > /dev/null 2>&1
echo -e "\n\aBackup is generated from $source, using archiver $archiver, reading global settings \
\nfrom $GLOBAL, processing excludelist $excludesource \
\n\nPlease change the disk. Insert volume 1 and press any key to continue." > /dev/tty
read ans < /dev/tty

toast_cmd="$burn_it $variable 2>> $logfile"

case "$archiver" in
 afio)
    find * | grep -v -f $excludelist | tee $filelist | \
    afio -oZvxz -M 6m -E $notcomp -L $logfile - | \
    eval $toast_cmd
 ;;
 tar)
    nice tar -cpvzf- -X $excludelist * | \
    eval $toast_cmd
 ;;
esac


echo -e "
-------------------------------------------------
              Verifying the archive                                   
-------------------------------------------------" | tee -a $logfile

case "$archiver" in              
 afio)
  $verify $variable | afio -rZvxz -L $logfile - 2>> $logfile
 ;;
 tar)
  $verify $variable | nice tar --compare -pzf- | tee -a $logfile
 ;;
esac

cp $0 $script_dir
cp $(whereis -b $burn_it | awk '{print $2}') $script_dir
cp $(whereis -b $verify | awk '{print $2}') $script_dir
cp $GLOBAL $script_dir
chmod -x $script_dir/*
gzip $script_dir/*
gzip $log_dir/*

mv $script_dir $toast_dir
mv $log_dir $toast_dir

mkdir $log_dir
cdrecord -v $dev -reset
cdrecord $dev -toc
( multiinfo="-C $(cdrecord $dev -msinfo) -M $rawdevice"
nice -n -10 mkisofs -R -J $multiinfo $toast_dir | \
cdrecord -v $dev $devdepend -eject -multi - ) 2>> $logfile
rm -R $arch_dir
