#! /bin/sh

# 'cdtape'
#
# - filter to use CD-R and CD-RW media similar to a tape drive
# - supports multi-volume archives!
#
# TO DO: - Better indication of end-of-session (at present, you
#          must use Ctrl-C after the last CD ...)
#        - together with this: better errorlevel check
#
# -----------------------------------------------------------------
# Note 1: adjust CD-RW and CD-R devices below!
# Note 2: This acts as a filter! --> the actual data on disk will 
#         NOT be in ISO format but p.ex. tar format (depending on 
#         the program used to 'feed' the filter).
# Note 3: This must be run as root.
# 
# Usage examples: 
#     tar cf - /DirectoryToBeSaved | cdtape save (save directory)
#     cdtape restore | tar tvf - (listing)
#     cdtape restore | tar xvf - FileToRestore (extract one file)
# -----------------------------------------------------------------
# Creation:  2000-09-21, JHa (first "draft").
# Revisions: 2001-04-15, v0.3 (backup & restore now in one script).
# -----------------------------------------------------------------
# Copyright (c) 2000, 2001 Joerg Hau <joerg.hau(at)dplanet.ch>.
# 
# This script was heavily inspired by the CD-Writing HOWTO and 
# ch.comp.os.linux, mainly from Pierre Maitre and Marc Schaefer. 
#
# 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.
# -----------------------------------------------------------------

# the CD-RW device & speed, 'cdrecord' syntax (dev=BUS,ID,LUN)
#
WRITE_DEV="dev=0,6,0"
WRITE_SPEED="speed=4"

# Blanking option: 
# for CD-RW media, specify "blank=fast". For CD-R, set it to "".
#
BLANK="blank=fast"

# the CD drive to READ FROM (not the mount point, but the device!) 
#
READ_DEV="/dev/cdrom"

# --- no user adjustable parts below this line :-)

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

case "$1" in
    save)
       while :
       do
         dd bs=2k count=327680 | cdrecord -v $WRITE_DEV $WRITE_SPEED $BLANK -eject -
         echo "Change CD and press 'Enter':" > /dev/tty
         read < /dev/tty dummy     
         if [ $? != 0 ]; then
            return="1"
            break
         fi
       done
       ;;
    restore)
       while :
       do
         dd bs=2k count=327680 if=$READ_DEV
         echo "Change CD and press 'Enter':" > /dev/tty
         read < /dev/tty dummy     
         if [ $? != 0 ]; then
            return="1"
            break
         fi
       done
       ;;
    *)
        echo "This is cdtape v0.3, (c) Joerg Hau <joerg.hau(at)dplanet.ch>."
	echo "Usage: $0 {save|restore}, p.ex.:"
        echo "       tar cf - /DirectoryToBeSaved | cdtape save (save directory)"
        echo "       cdtape restore | tar tvf - (listing)                       "
        echo "       cdtape restore | tar xvf - FileToRestore (extract one file)"
        exit 1
        ;;
esac

# set an exit status.
test "$return" = "0" || exit 1
echo $return
exit 0

