#!/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'.
#200209 got this script from woof-ce, it has gettext translations, plus some dlg changes.
#200209 give this script its own TEXTDOMAIN. translate xmessage text.
#200508 TEXTDOMAIN "pup_" prepended.

#/etc/rc.d/rc.shutdown saves session at shutdown, savesession-dvd is called
#from desktop icon to do an immediate save to multisession-cd.
#200209 note, rc.sysinit will modify PuppyPin to create "save" icon on desktop.

#200209 give script a TEXTDOMAIN...
. /usr/bin/gettext.sh # enables use of eval_gettext (several named variables) and ngettext (plurals)
export TEXTDOMAIN=pup_savesession-dvd
export OUTPUT_CHARSET=UTF-8

#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

. /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
 /usr/lib/gtkdialog/box_ok "$(gettext 'Save session to DVD')" error "$(gettext 'This is not a multisession live-CD/DVD')"
 exit
fi
if [ "$CDR" = "" ];then
 /usr/lib/gtkdialog/box_ok "$(gettext 'Save session to DVD')" error "$(gettext '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
  /usr/lib/gtkdialog/box_ok "$(gettext 'Save session to DVD')" error "$CDR $(gettext 'is currently mounted and cannot be automatically unmounted. You will need to unmount it manually.')" "$(gettext 'Click OK button to quit...')"
  exit
 fi
fi

#200209
M0="$(gettext '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.')"
M0X="$(echo "$M0" | fmt -58)" #word-wrap.
M1="$(gettext '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.')"
M1X="$(echo "$M1" | fmt -58)"
M2="$(gettext 'Click SAVE button to save session...')"
M2X="$(echo "$M2" | fmt -58)"

xmessage -bg "#ffc0c0" -center -title "SaveSession" -buttons "SAVE:10,CANCEL:11" "${M0X}

${M1X}

${M2X}"
[ ! $? -eq 10 ] && exit

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

###END###
