#!/bin/sh
# make incremental dump of filesystems
# usage: dump [+dont-compress] level filesystem...
#
# Dumps are made on $dumpdev, which contains a directory for every filesystem
# dumped.  Of course, you cannot dump that filesystem itself.  For example,
# if dumpdev=/dev/g, there will be the following directories:
#   /dev/g/c
#   /dev/g/d
# et cetera.  Each such directory contains a file named YYYYMMDD.tar, where
# YYYYMMDD is the date of the dump.  If you dump /dev/c on 03 May, 1992,
# there will be a file /dev/g/c/19920503.tar
version="0.0 alpha"
PATH=${root}/bin:${root}/usr/bin
set -e
set -x # Uncomment this line when debugging
umask 022
dumpdev=/dev/g
root=u:
datesfile=${root}/etc/dumpdate
infofile=${root}/etc/dumpinfo

fatalerror () {
  echo Dump aborted on `date +"%m/%d/%Y %H:%M:%S"` due to a signal >> $infofile
}

usage () {
  echo usage: $program \[+dont-compress\] level partition...
}

# Print message on fatal signal
trap fatalerror 2 3 4 5 6 7 8 10 11 12 13 15

echo Dump script version $version, Stephan Neuhaus for MiNT/TOS
program=`basename $0`

if test $# -le 1 -o \( $1 = +dont-compress -a $# -le 2 \) ; then
  usage
  exit 1
fi

do_compress=1
if test $1 = +dont-compress ; then
  do_compress=0
  shift
fi

level=$1 ; shift

if test ! -d $dumpdev -a ! -b  $dumpdev ; then
  echo program: Dump device $dumpdev: not a directory nor block special file
  exit 1
fi

for dumpdir in $* ; do
  filesystem=/dev/${dumpdir}
  if test ! -d $filesystem -a ! -b $filesystem ; then
    echo $program: Source $filesystem: not a directory nor block special file
    exit 1
  fi
  
  if test $level -ne 0 ; then
    lastdate=`gawk '
      BEGIN { found = 0 }
      $1 == level - 1 && $6 == filesystem { lastdate=$4; lasttime=$5; found=1 }
      END { if (found) printf "%s %s", lastdate, lasttime; }' \
      level=${level} filesystem=${filesystem} ${datesfile}`

      if test "x${lastdate}" = x ; then
        echo Previous dump not found in ${datesfile}
        exit 1
      fi
  fi
  startdate=`date +"%m/%d/%Y %H:%M:%S"`
  dumpdate=`echo $startdate | cut --delimiter=' ' --fields=1 | gawk -F / '{printf "%s%s%s", $3, $1, $2}'`
  echo Begin of dump info for level $level dump of $filesystem >> $infofile
  echo Level $level dump of $filesystem started on $startdate >> $infofile
  echo -n Dumping $filesystem...
  dumpfile=${dumpdev}/${dumpdir}/${dumpdate}.tar
  if test ! -d ${dumpdev}/${dumpdir} ; then
    mkdir ${dumpdev}/${dumpdir}
  fi
  cd $filesystem
  if test $level -eq 0 ; then
    tar +create +file $dumpfile .
  else
    tar +create +newer "$lastdate" +file $dumpfile .
  fi
  enddate=`date +"%m/%d/%Y %H:%M:%S"`
  echo $level $startdate $enddate $filesystem >> ${datesfile}
  echo -n Writing filelist...
  echo List of files follows: >> $infofile
  tar +list +verbose +file $dumpfile >> $infofile
  echo Level $level dump of $filesystem ended on $enddate \(written on ${dumpfile}\) >> $infofile
  ls -l $dumpfile >> $infofile
  if test $do_compress -eq 1 ; then
    echo -n Compressing...
    compress -f $dumpfile
    cfile=`echo $dumpfile | sed s/tar/taz/g`
    ls -l $cfile >> $infofile
  else
    ls -l $dumpfile >> $infofile
  fi
  echo End of dump info for level $level dump of $filesystem >> $infofile
  echo Done.
done
echo Done.
