#!/bin/sh
# 
# File: rundvbackup
# Author: Robert Jordens <jordens@debian.org>
# License: GNU General Public License Version 2 (GPLv2), incorporated herein by
# reference
# Id: $Id$
# Warning: read it before you use it
# 
# TODO: how shall we start/stop the recorder in the right moment? 
#       tar takes its time while scanning and filling the buffer

# exit on errors
set -e -x

HOST=$( hostname )
STAMP=$( date '+%Y%m%d%H%M%S' )
# * DIRS sorted by priority but smart: dirs with large files 
#   scattered throughout to reduce underruns) 
#   TODO: write multiplexer/interleaver or a tar that randomizes the list
# * TODO: if you change DIRS between backups that results in consistent 
#   but less known/defined behaviour
DIRS="/etc /home /var /boot /root /bin /sbin /usr /win"
# TODO: has to exist
STATEDIR=/var/lib/dvbackup
FONT=-misc-fixed-medium-r-normal--6-60-75-75-c-40-iso10646-1
LOGOTEMPLATE=/usr/share/doc/dvbackup/examples/logotemplate.ppm

# sequence should be about:
#  0 1 2 2 2 1 2 2 2 1 2 2 2 0 1 2 2 2 1 2 2 2 1 2 2 2 0 .....
# or for example:
#  0 monthly (first saturday of the month)
#  1 weekly (every sunday)
#  2 daily (everyday except sundays and the first saturday of the month)
# requires you to atmost play in 3 backups to get the current state.
# TODO: example crontab
if [ -z "$1" -o "x$1" = "x0" ]; then
        LEVEL=0
        PARENT=""
else
        LEVEL=$1
        # if this fails, we should bail out and TODO: usage
        PARENT=$(( $LEVEL - 1 ))
fi
TITLE=$HOST-l$LEVEL-$STAMP
test -n "$PARENT" && PARENTSTATEFILECOMMON=$STATEDIR/state-$HOST-l$PARENT
test -n "$PARENTSTATEFILECOMMON" && \
	PARENTSTATEFILE=$( readlink $PARENTSTATEFILECOMMON )
THISSTATEFILECOMMON=$STATEDIR/state-$HOST-l$LEVEL
test -e $THISSTATEFILECOMMON && OLDSTATEFILE=$( readlink $THISSTATEFILECOMMON ) 
THISSTATEFILE=$STATEDIR/state-$TITLE
LOGO=$( mktemp -t dvbackup-logo-XXXXXX )
# TODO: nice paragraphing "/home /etc\n/usr /var\n...". This here is 
#       sufficient for 8 lines
DIRSNL=$( echo $DIRS | tr ' ' '\n' )
# TODO: based on
convert -font "$FONT" -draw "text 1,7 '$TITLE'" -draw "text 1,13 '$DIRSNL'" \
        $LOGOTEMPLATE $LOGO

# safe because we have backups of this statefile ($THISSTATEFILE-HighestStamp)
# TODO: missing error handling, roll-back on error/abort
if [ "$LEVEL" = 0 ]; then
        touch $THISSTATEFILE
else
        cp $PARENTSTATEFILE $THISSTATEFILE
fi

(nice -n -10 tar \
        --create \
        --listed-incremental $THISSTATEFILE \
        --label "$TITLE" \
        --ignore-failed-read \
        --blocking-factor 128 \
        --preserve-permissions \
        --same-owner \
        --one-file-system \
        --sparse \
        --verbose --verbose \
        $DIRS \
| rsbep \
| dvbackup \
        --prefix=250 \
        --set-backup-title=$TITLE \
        --set-picture=$LOGO \
        --verbose \
| dvconnect \
        --send \
        --device=/dev/video1394/0 \
        --buffers=1024 \
        --kbuffers=32 \
        --underrun-data=/usr/share/dvbackup/underrun-pal.dv \
        -- - \
) 2>&1 | gzip -3 > $STATEDIR/$TITLE.log.gz

rm -f $LOGO

# TODO: missing log-rotation and statefile aging
rm -f $THISSTATEFILECOMMON
test -e "$OLDSTATEFILE" && bzip2 $OLDSTATEFILE
ln -s $(basename $THISSTATEFILE) $THISSTATEFILECOMMON

# TODO: testing of success of the backup
# dvconnect --device /dev/video1394/0 | dvbackup -t
# dvconnect --device /dev/video1394/0 | dvbackup --decode --verbose | rsbep -d -v
# | tar Ox > /dev/null
# tar --ignore-zeros

# TODO: replay of backups: 
# TODO: this is damn hard at 3MB/sec. overruns guaranteed
# --atime-preserve
#       for i in range(0,levels necessary): do
#            replay backup-$i-HighestStampBelowStampOf($i-1)

# something with cpio
# find $DIRS -print0 | cpio --format=crc --create --null --verbose

