#!/bin/sh
#(c) Copyright Barry Kauler 2006 www.puppylinux.com
#2007 Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html)
#BK updated may 2007 for 2.16.
#BK updated v2.22, sync with rc.shutdown multisession bugfix.
#v3.01: BK 11 oct 2007, removed multisavefunc() to /etc/rc.d/functions4puppy.
#v412 /etc/DISTRO_SPECS, renamed pup_xxx.sfs, pup_save.2fs etc.
#v555 pup files renamed to woofr555.sfs, woofsave.2fs.
#w482 use xorriso instead of cdrtools/cdrkit if installed.
#120323 partial replace 'xmessage' with 'pupmessage'.

#/etc/rc.d/rc.shutdown saves session at shutdown, savesession-dvd is called
#from desktop icon to do an immediate save to multisession-cd.

#w482 use xorriso if it is installed (see also functions4puppy)...
CDRECORD='cdrecord'
MKISOFS='mkisofs'
if [ "`which xorriso`" != "" ];then
 CDRECORD='xorrecord'
 MKISOFS='xorrisofs' #growisofs reads this variable.
fi
export CDRECORD MKISOFS

#variables created at bootup by /initrd/usr/sbin/init...
. /etc/rc.d/PUPSTATE #v2.02
. /etc/DISTRO_SPECS #v412
ORIGPUPMODE=$PUPMODE #v2.22
#PUPPYVERSION="`cat /etc/puppyversion`"

. /etc/rc.d/functions4puppy #v3.01 has multisavefunc.

CONSRUN="$1" #will execute multisavefunc from a rxvt console.
#...this script calls itself a second time, as multisavefunc uses dialog,
#   and runs without X.
#also test if X not running...
[ "`pidof X | grep '[0-9]'`" = "" ] && CONSRUN="yes"

if [ ! "$PUPSAVE" = "" ];then
 SAVEFS="`echo -n "$PUPSAVE" | cut -f 2 -d ','`"   #f.s. and partition where ${DISTRO_FILE_PREFIX}save.2fs is located.
 SAVEPART="`echo -n "$PUPSAVE" | cut -f 1 -d ','`" # "
 SAVEFILE="`echo -n "$PUPSAVE" | cut -f 3 -d ','`"
else
 exit #v2.22
fi
CDR="/dev/$SAVEPART"


if [ ! "$CONSRUN" = "" ];then
 multisavefunc
 exit
fi


if [ "$PUPMODE" != "77" ];then
 pupmessage -bg "red" -center -title "SaveSession-DVD" "Sorry, this is not a multisession live-CD/DVD"
 exit
fi
if [ "$CDR" = "" ];then
 pupmessage -bg "red" -center -title "SaveSession-DVD" "Sorry, this is not a multisession live-CD/DVD"
 exit
fi


#before can save a session back to DVD, have to be sure that DVD is unmounted...
if [ "`mount | grep "$CDR"`" != "" ];then
 sync
 echo "Unmounting $CDR..."
 fuser -k -m $CDR
 sync
 umount $CDR
 if [ ! $? -eq 0 ];then
  pupmessage -bg "red" -center -title "SaveSession: ERROR" "Sorry, $CDR is currently mounted and cannot be automatically
unmounted. You will need to unmount it manually.

Click OK button to quit..."
  exit
 fi
fi

xmessage -bg "#ffc0c0" -center -title "SaveSession" -buttons "SAVE:10,CANCEL:11" "This program takes a snapshot of changed and new files and
saves them to CD/DVD. For the multisession-CD/DVD, this happens
at shutdown, however, if you are working on critical files and
do not want to lose your work in case of a power-outage or
system crash (latter very unlikely), this program is a solution
-- just run it whenever you want to backup.

WARNING: CDs have much less space than DVDs, and there is more
wasted space in each saved session, meaning that a CD will fill
up fast. Therefore, for CDs use this Save script infrequently.

Click SAVE button to save session..."
[ ! $? -eq 10 ] && exit

#recursive execution...
exec rxvt -e /usr/sbin/savesession-dvd yes

###END###
