#! /bin/sh

# 'saveset-dvd.sh'
#
# - writes data to DVD+-R(W)
# - allows to specify directories & exclude patterns
# - allows external config file
# - ... but supports only 1 DVD
#
# TO DO: - check that mkisofs is 1.13 or later
#        - check integrity of CD image after burning
# -----------------------------------------------------------------
# Note 1: adjust parameters below!
# Note 2: This must be run as root.
# -----------------------------------------------------------------
# Creation:  2001-10-03, JHa (first "draft").
# Revisions:
# 2001-10-05, JHa: added check for free space, preserve existing
#             file permissions (Thanks to Hendric Stattmann !)
# 2001-10-28, JHa: excluded wwwoffle completely
# 2001-11-03, JHa: adjusted for mkisofs-1.14 syntax
# 2002-04-18, JHa: fixed IMGDIR stuff (Thanks to Jochen Gruse !)
# 2002-12-28, JHa: added error check in mkisofs "dry run".
# 2003-01-27, JHa: added comments
# 2003-09-05, JHa: archive name is derived from $HOST + $DATE.
# 2003-09-28, JHa: allows external config file. MAXSIZE is now
#             variable to allow non-standard CD sizes.
# 2003-10-30  HST: simplified exit status handling.
# 2003-10-30  HST: added max. ISO sizes for CD-R media w more than 650MB.
# 2005-07-05  HST: implemented growisofs for DVD+-R(W) writing. EXPERIMENTAL!
#
# -----------------------------------------------------------------
# Copyright (c) 2001-2003 Joerg Hau <joerg.hau(at)dplanet.ch>.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of
# the License, or (at your option) any later version, provided that
# the copyright notice remains intact even in future versions.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# -----------------------------------------------------------------

# disable filename expansion
#
set -f

# --- user-specific setup ---
#
# All parameters below can also be specified (or changed ;-)
# via a config file that is given on the command line.
# As an example, the config to save my digital photos looks
# like this (without the '#' of course ;-):
#
# DIRLIST="home/jha/bildsche"
# EXCLUDE="*~ *.tgz"
# NAME=`cat /etc/HOSTNAME`.jha-graf.`date +%Y%m%dT%H%M`
#

# list of directories to archive. Do not use slashes at the end!
#
DIRLIST="root etc home"

# ... and list of exclude pattern (see mkisofs manpage)
#
#EXCLUDE="*cache* *~ *thumbnails* */mp3/* *.wav \
#/home/jha/graf/* /var/spool/wwwoffle*"

# the CD-RW device & speed, 'cdrecord' syntax (dev=BUS,ID,LUN)
# for a IDE CD-RW, this is usually "dev=0,0,0"

# For DVD, /dev/dvd is used by default as $WRITE_DEV
# Use "speed=1" if experiencing problems.

WRITE_DEV="/dev/dvd"
WRITE_SPEED="2"

# Blanking option:
# for CD-RW media, specify "blank=fast". For CD-R and DVD+-R, set it to "".
# FIXME: APPROPRIATE OPTION FOR DVD+-RW
BLANK="blank=fast"

# name for the Volume
#
NAME=`cat /etc/HOSTNAME`.`date +%Y%m%dT%H%M`

# Recommanded maximum image sizes (in 2048-byte blocks):
# See an extended list of CD-R(W) capacities at:
# http://cdrecord.berlios.de/old/private/cd-cap.html

MAXSIZE=2295104

#
# --- end of user-specific setup ---
#
# "No user adjustable parts below this line" :-)
#

if [ `whoami` != 'root' ]; then
    echo "You must be root to run this script."
    exit 2
fi

# if any external config file was specified, source it
#
if [ $# -eq 1 ]; then
    echo -n "Reading configuration from $1 ..."
    if [ -e "$1" ]; then
        source $1
        echo "done."
    else
        echo "Configuration file $1 not found."
        exit 1
    fi
fi


# concatenate list of directories & excludes
#
DIRS=`for X in $DIRLIST; do echo -n "$X/=/$X "; done`
EXCL=`for X in $EXCLUDE; do echo -n "-m "$X" "; done`

echo -n "Preparing to archive ("
for X in $DIRLIST; do echo -n " $X"; done
echo " )"

echo "Excluding ( "$EXCLUDE" )"

# get size from a "dry run", same parameters as for the final run below
#
# for mkisofs 1.14 and above:
#
SIZE=`/usr/bin/mkisofs -J -allow-leading-dots -R -V $NAME -quiet -o /dev/null $EXCL \
-print-size -graft-points $DIRS`

# check status of last cmd, abort if not OK
# (command may fail e.g. with too-long filenames)
#
if [ $? != 0 ]; then
	echo " *** Error occurred during 'mkisofs' dry run! Exiting."
	exit 1
fi

# Check if image size fits media.
#
echo "Size of ISO image will be $SIZE blocks."
if (($SIZE > $MAXSIZE)); then
	echo "Sorry, this is too big - should be less than $MAXSIZE blocks."
 	exit 1
fi

# No Image file created in /tmp/ for DVD writing. Free space check ommitted.

# If everything OK: Burn data to DVD.
# Option '-R' preserves file permissions.
#
/usr/bin/growisofs -Z $WRITE_DEV -speed=$WRITE_SPEED -allow-leading-dots \
                   -J -R -V $NAME -quiet $EXCL -graft-points $DIRS 

# FIXME: check for integrity of data
# mount -t iso9660 -o ro /cdrom
# for X in $DIRLIST; do diff -r /$X /cdrom/$X"; done
# ... all we need to implement here is an error check for diff
# and ... well, the consideration of the exclude list ;-)
#

# If we get through here in the script, everything went well.
exit 0



