#!/bin/sh
#script to backup /root to CD.
#(c) Copyright Barry Kauler 2004,2005 www.goosee.com/puppy


if [ ! -f /etc/cdburnerdevice ];then
 xmessage -bg "medium purple" -center -title "Puppy backup to CD" "You must run the CD/DVD drive Wizard first, to select
a burner drive."
 exit
fi
CDR="ATAPI:/dev/`cat /etc/cdburnerdevice`"
DEV="/dev/`cat /etc/cdburnerdevice`"


#####
echo '#!/bin/sh
echo "Executing cdrecord now..."
LINES1=0
LINES2=0
sleep 1
ps | grep "cdrecord" >/dev/null
RET1=$?
while [ $RET1 -eq 0 ];do
 sleep 1
 LINES2=`wc -l /tmp/cdrlog.txt | tr -s " " | cut -f 2 -d " "`
 if [ $LINES2 -gt $LINES1 ];then
  LINESDIFF=`expr $LINES2 - $LINES1`
  tail -n $LINESDIFF /tmp/cdrlog.txt
  LINES1=$LINES2
 fi 
 ps | grep "cdrecord" >/dev/null
 RET1=$?
done
echo "...finished executing cdrecord."
echo -n "Press ENTER key to continue: "
read nnn
' > /tmp/bkupx.sh
chmod 755 /tmp/bkupx.sh
#####

mount | grep "iso9660" > /dev/null 2>&1
if [ $? -eq 0 ];then
 xmessage -bg "#ffc0c0" -center -title "Puppy Backup to CD" -file -<<FILETXT
Puppy sees that a CD is mounted.
If it is in the CD-burner drive, please
unmount it RIGHT NOW, before clicking
on the OKAY button.

HINT: If you have the multi-session backup-CD
mounted, check how much space is left on it
before unmounting it.
FILETXT
fi

##run cdrecord to get the scsibus:target:lun numbers...
##remove all lines with * in them...
##v1.0.0 SCSIADDR0="`cdrecord -scanbus 2>/dev/null | grep -v "*" | grep -i "rw" | cut -f 2 | head -n 1`"
#SCSIADDR0="`cdrecord -scanbus 2>/dev/null | grep -v "*" | grep "[0-9],[0-9],[0-9]" | cut -f 2 | head -n 1`"
#if [ "$SCSIADDR0" = "" ];then
# xmessage -bg "#ffc0c0" -center -title "Puppy Backup to CD" -file -<<FILETXT
#ERROR!
#Puppy is unable to find a SCSI or SCSI-emulated
#CD-burner drive.
#FILETXT
#
# exit 0
#fi
##...example, SCSIADDR0=0,0,0


NUMDAYSCD=1
if [ ! -e /etc/bkup2cdrc ];then
 #echo -e "CDR=$SCSIADDR0" > /etc/bkup2cdrc
 #echo "DEV=/dev/scd0" >> /etc/bkup2cdrc
 ##echo -e "LASTBKUP=\"\"" >> /etc/bkup2cdrc
 NUMDAYSCD=365
fi

#need to see when last backed up, then run backup for intervening period...
SECSFILE=`date -r /etc/bkup2cdrc +%s`
NUMDAYSF=`expr $SECSFILE / 86400`
SECSDATE=`date +%s`
NUMDAYSD=`expr $SECSDATE / 86400`
if [ ! $NUMDAYSCD -eq 365 ];then
 NUMDAYSCD=`expr $NUMDAYSD - $NUMDAYSF`
 if [ $NUMDAYSCD -eq 0 ];then
  NUMDAYSCD=1
 fi
 MSG1="Puppy sees that you backed up $NUMDAYSCD days ago."
else
 MSG1="Puppy sees that you have not backed up before."
fi

while :; do

xmessage -bg "#e0e0ff" -center -name "pbkup" -title "Puppy backup to CD" -buttons \
 Change_#_days:10,Backup_now:12,EXIT:14 \
 -file -<<FILETXT
Welcome to the Puppy backup-to-CD script!

$MSG1

Puppy will backup files in the /root folder, which
has all your personal configuration and data files,
including email, address books, histories, accounts.
Puppy will backup all files modified in the last
$NUMDAYSCD days.

If you want to change the number of days to be backed
up, click on the "Change_#_days" button.

Note, this script currently only burns to a CD, not a DVD.

PLACE A CD IN THE CD-BURNER DRIVE,
THEN PRESS "Backup_now"
FILETXT

XREPLY=$?

if [ $XREPLY -eq 10 ];then #change number of days
 NEWNUMDAYS=`gtk-shell -q -qv "$NUMDAYSCD" -t "#days to backup"`
 if [ $NEWNUMDAYS ];then
  NUMDAYSCD=$NEWNUMDAYS
 fi
 MSG1="Puppy will backup the last $NUMDAYSCD days."
fi

if [ $XREPLY -eq 11 ];then #edit config file
 gtk-shell -ef "/etc/bkup2cdrc" -t "Edit /etc/bkup2cdrc config file"
fi

if [ $XREPLY -eq 12 ];then
 break
fi

#if [ $XREPLY -eq 13 ];then #help
# dillo file:///usr/share/doc/bkup2cd.htm &
#fi

if [ $XREPLY -eq 14 ];then #exit
 break
fi
if [ $XREPLY -eq 0 ];then #exit
 break
fi
if [ $XREPLY -eq 1 ];then #exit
 break
fi

done

if [ ! $XREPLY -eq 12 ];then #backup to cd
 exit
fi

#Note,some of this based on bkup2cd by r.f.smith 2001 gpl
cd $HOME
if [ ! -d tmp ]; then
 mkdir tmp
fi
#CDR="`cat /etc/bkup2cdrc | grep "CDR" | cut -f 2 -d "="`"
#DEV="`cat /etc/bkup2cdrc | grep "DEV" | cut -f 2 -d "="`"
LOG=00README.backup
DATE=`date "+%Y-%m-%d"`
BLIST=$HOME/tmp/backup-list-$DATE
LIST2=$BLIST.tmp
FILE=$HOME/tmp/backup-$DATE.raw
USR="puppy"
LASTDATE=`date --date="$NUMDAYSCD days ago" "+%Y-%m-%d" 2>/dev/null`
  # Write logfile header.
  echo "Backing up from ${LASTDATE} to ${DATE}."
  rm -f $LOG $HOME/tmp/backup-*
  echo "# This backup contains all the files that have been changed in" >>$LOG
  echo "# the period from ${LASTDATE} to ${DATE}." >>$LOG
  echo "# "  >>$LOG
  echo "# This file is automatically generated by bkup2cd ${VER}."  >>$LOG
  echo >>$LOG
 echo -n "Searching for files..."
 #busybox grep is very simple...
 cd $HOME
 find . -mtime -$NUMDAYSCD -type f|grep -v "$FILE" |grep -v "00README\\.backup"|grep -v "$LIST2"|grep -v "$BLIST"|grep -vi "cache"|grep -v "my-applications"|grep -v "tmp"|grep -v "vmlinuz"|grep -v "image\\.gz"|cut -c 3-|sort >$BLIST
  # Build the filespec list for mkisofs, with new graft points for the files.
  cat $BLIST|awk '{printf("%s/%s=%s\n", '\"$DATE\"', $0, $0);}' >$LIST2
  # Add the files with new graftpoints to the logfile.
  cat $BLIST|awk '{printf("%s/%s\n", '\"$DATE\"', $0);}' >>$LOG
  echo " done"

while :; do
rm -f $BLIST
rm -f $FILE

   # Create backup ISO9660 image
   ERRCD=""
   echo -n "Checking for previous tracks on CD..."
   OUTP=`cdrecord -msinfo dev=${CDR} 2>/tmp/cdrerr.txt`
   SECT=`echo ${OUTP}|grep ","`
   OUTP=`cat /tmp/cdrerr.txt|grep "CDB"|awk '{print $2;}'`
   if [ -z $SECT ]; then
    #ERRCD=$OUTP
    # No previous backup found, see codes above.
    case $OUTP in
        52) MSG1="Not a multisession CD";ERRCD="52";;
        43) MSG1="CD is blank";;
        00) MSG1="No CD found";ERRCD="00";;
        *)  MSG1="Unknown value from cdrecord -msinfo";ERRCD="XX"
    esac
    #echo "Creating original backup track ${FILE}..."
    MSG2="original"
    mkisofs -J -R -o $FILE -pad -P "${USR}" -p "bkup2cd" -graft-points -path-list $LIST2 $DATE/$LOG=$LOG
   else
    MSG1="Multisession CD found"
    #echo " found: ${SECT}"
    #echo "Creating additional backup track ${FILE}..."
    MSG2="additional"
    mkisofs -J -R -o $FILE -C $SECT -M $DEV -pad -P "${USR}" -p "bkup2cd" -graft-points -path-list $LIST2 $DATE/$LOG=$LOG
   fi
cp $LOG $BLIST

SIZEFILE="`du $FILE`" #in Kbytes + filename
SIZEFILK=`echo -n $SIZEFILE | tr -s " " | cut -f 1 -d " "`
 if [ ! $SIZEFILK ];then
  SIZEFILK=`echo -n $SIZEFILE | tr -s " " | cut -f 2 -d " "`
 fi
NUMTRACKS=`cdrecord -toc dev=$CDR |grep "track"|tail -n 2|head -n 1|tr -s " "|cut -f 2 -d " "`
if [ ! $NUMTRACKS ];then
 NUMTRACKS=0
fi
if [ $NUMTRACKS -gt 98 ];then #max allowed is 99.
 ERRCD="VV"
 MSG1="CD is full"
else
SIZEEXT=`cdrecord -toc dev=$CDR | grep "track" | tail -n 1 | tr -s " " | cut -f 3 -d " "`
if [ ! $SIZEEXT ];then
 SIZEEXT=0
fi
SIZEEXK=`expr $SIZEEXT + $SIZEEXT`
SIZEMEG=`expr $SIZEEXK / 1024`
SIZENEK=`expr $SIZEEXK + $SIZEFILK`
SIZENEW=`expr $SIZENEK / 1024`
if [ $SIZENEW -gt 640 ];then
 ERRCD="ZZ"
 MSG1="CD is full"
fi
FLAGLAST="no"
if [ $SIZENEW -gt 600 ];then #a bit wasteful
 FLAGLAST="yes"
fi
fi
if [ $NUMTRACKS -eq 98 ];then
 FLAGLAST="yes"
fi

if [ "$ERRCD" ];then
xmessage -bg "#ffc0c0" -center -name "pbkup" -title "Puppy backup to CD" -buttons \
 Change_CD:13,Erase_CD-RW:12,EXIT:14 \
 -file -<<FILETXT
Puppy has looked at the CD-burner drive
and reports this: "$MSG1"

Click "Change_CD" button.

However, if a CD-RW (ReWritable) is inserted,
you may press "Erase_CD-RW" button to wipe it.
FILETXT

XREPLY=$?
else

xmessage -bg "#e0e0ff" -center -name "pbkup" -title "Puppy backup to CD" -buttons \
 Change_CD:13,Erase_CD-RW:12,Burn_track:10,EXIT:14 \
 -file -<<FILETXT
Puppy has looked at the CD-burner drive
and reports this: "$MSG1"

Therefore, Puppy has created an $MSG2 track,
size in Kbytes and filename: 
$SIZEFILE

If you have a CD-RW (ReWritable) and want to
erase it first, click the "Erase_CD-RW" button.

To burn this file to CD, click on "Burn_track"
button.

Note that the same CD can be reused for backups,
and each backup is a "track" and will appear on
the CD as a folder, named by date.
The final track, when the CD is full, will close
the CD to prevent further writing.

The number of tracks already written:   $NUMTRACKS
The size written to CD so far (Mbytes): $SIZEMEG
(note, max allowed on one CD is 99 tracks,
 or size 650M, whichever comes first)
FILETXT

XREPLY=$?
fi

#echo -e "CD-R BURN RECORD\n" > /tmp/cdrlog.txt
if [ $XREPLY -eq 13 ];then #change cd
 xmessage -center -title "Puppy backup to CD" "Press Eject button on drive and insert another CD then click OKAY"
else
if [ $XREPLY -eq 12 ];then #erase cd
 #MSG3="`cdrecord speed=4 blank=fast dev=$CDR`"
 #rxvt -bg "#e0c0ff" -e "cdrecord -v speed=4 blank=fast dev=$CDR"
 cdrecord -v speed=4 blank=fast dev=$CDR > /tmp/cdrlog.txt 2>&1 &
else
if [ $XREPLY -eq 10 ];then #burn track
 if [ "$FLAGLAST" = "no" ];then
 cdrecord -v -eject -multi speed=4 dev=${CDR} -data ${FILE} > /tmp/cdrlog.txt 2>&1 &
 else
 cdrecord -v -eject speed=4 dev=${CDR} -data ${FILE} > /tmp/cdrlog.txt 2>&1 &
 fi
else
 break
fi
fi
fi

#Xdialog --title "Writing to CD..." --fixed-font --no-cancel --smooth --logbox /tmp/cdrlog.txt 18 75

#this displays cdrecord tail...
if [ ! $XREPLY -eq 13 ];then #change cd
/usr/local/bin/rxvt -bg "#e0e0ff" -e /tmp/bkupx.sh
fi

if [ ! $XREPLY -eq 12 ];then #erase cd
if [ ! $XREPLY -eq 13 ];then #change cd
break
fi
fi
done

sync
rm -f /root/tmp/*
rm -f /tmp/cdrerr.txt
rm -f /tmp/cdrlog.txt
rm -f /tmp/bkupx.sh
rm -f $LOG $LIST2
