#!/bin/sh
#(c) Copyright 2006, 2007 Barry Kauler.
#Lesser GPL licence v2 (http://www.fsf.org/licensing/licenses/lgpl.html). 2007 www.puppylinux.com
#simple script to burn an iso file to cd, using cdrecord.
#7Sept2007: cdecord -scanbus bugfix, plus overall improvements.
#v3.02 2Nov2007 BK: bugfix. v3.94 24dec2007: no /dev/hd*


MEDIATYPE="`Xdialog --wmclass "mini-cd" --title "Burniso2cd" --stdout --menubox "Welcome to our little CD/DVD burner program!\nPlease choose whether you want to burn to a CD or DVD media.\nNote that DVDs are always burnt 'open' so can be used by Puppy\nas normal or multisession, whereas for CD you will be given\nthe choice further on whether to burn as closed or open." 0 0 5 CD "CD-R or CD-RW (must be blank)" DVD "DVD-R or DVD-RW (must be blank)"`"
[ ! $? -eq 0 ] && exit


SELECTIONS="`probedisk | grep '|cdrom|' | grep '/dev/' | cut -f 1,3 -d '|' | tr " " '_' | tr -s '_'`"
SELECTIONX="`echo "$SELECTIONS" | tr '|' " " | tr "\n" " "`"

if [ "$SELECTIONX" = "" ];then
 xmessage "Sorry, no CD/DVD drive detected"
 exit
fi

BURNERDRIVE="`Xdialog --wmclass "mini-cd" --title "Burniso2cd: Choose burner drive" --stdout --menubox "Choose the CD/DVD drive to burn to" 400x200 5 $SELECTIONX 2> /dev/null`"
[ ! $? -eq 0 ] && exit

[ ! -f /etc/cdburnerdevice ] && echo -n "$BURNERDRIVE" | sed -e 's/\/dev\///g' > /etc/cdburnerdevice

ISOFILE="`Xdialog --wmclass "mini-cd" --backtitle "Please choose the iso file..." --title "Burniso2cd: Choose iso file" --stdout --no-buttons --fselect "*.iso" 0 0`"
if [ ! $? -eq 0 ];then
 exit
fi

CDR="$BURNERDRIVE"
CDDESCR="`echo "$SELECTIONS" | grep "$CDR" | cut -f 2 -d '|'`"

while [ ! "`mount | grep "$CDR"`" = "" ];do
 xmessage -bg "#ff8080" -center -name "burniso2cd" -title "Burniso2cd: ERROR" "The $CDR CD/DVD drive, described as:
 $CDDESCR
is currently mounted.
Use MUT or Pmount to unmount it.

Please unmount CD drive, then click OK button..."
done

if [ "$MEDIATYPE" = "CD" ];then
 Xdialog --wmclass "mini-cd" --title "Burniso2cd" --stdout --ok-label "NORMAL" --cancel-label "MULTI"  --yesno "You have selected iso file:
 $ISOFILE

Do you want to burn a multi-session CD?
A multi-session CD is one that is left \"open\" so that further
tracks may be burnt later. The iso file that you have selected
will be burnt now as the first track.
Note, this is an experimental option for the Puppy live-CD, to
allow burning of sessions back to CD, thus personal data is saved
to CD and a hard drive or usb stick is not required.

Please choose 'NORMAL' unless you have some special reason to burn
the CD multi-session (such as the iso is specifically for multisession).

Click 'NORMAL' button for normal burn...
Click 'MULTI' button for multi-session..." 0 0

 RETVAL=$?
else
 RETVAL=12
fi

case $RETVAL in
 0) #normal cd
  BURNMULTI="-dao" #v2.02 added -dao
  BURNMSG=""
  ;;
 1) #multi cd
  BURNMULTI="-multi -tao -pad" #v2.02 added -tao -pad
  [ "`which xorriso`" != "" ] && BURNMULTI="-multi -tao padsize=300k" #w482
  BURNMSG=""
  ;;
 12) #multi dvd
  BURNMULTI="yes"
  BURNMSG="(blank DVD-R. A DVD+/-RW will be fast-wiped if it has data)"
  ;;
 *)
  exit
  ;;
esac

RECHECK="yes"
BURNSPEED="10"

while [ 1 ];do #burn-again-loop
  BURNSPEED="`Xdialog --wmclass "mini-cd" --title "Burniso2cd: Burn $MEDIATYPE" --stdout --spinbox "Please insert blank $MEDIATYPE into $CDR
$BURNMSG
(which is described as: ${CDDESCR})

Then click 'OK' button..." 0 0 4 32 $BURNSPEED "Set burn speed"`"

 [ ! $? -eq 0 ] && exit

 sync
 if [ "$MEDIATYPE" = "CD" ];then
  if [ ! "`echo "$CDR" | grep '/dev/hd'`" = "" ];then
   DEVDEV="ATAPI:$CDR"
  else
   DEVDEV="$CDR"
  fi
  echo "Now executing:"
  if [ "`which xorriso`" != "" ];then #w482
   #xorriso -as cdrecord -multi -tao -pad -data -eject -v speed=10 dev=/dev/sr0 cd-puppy.iso
   echo " xorriso -as cdrecord $BURNMULTI -data -eject -v speed=$BURNSPEED dev=$DEVDEV $ISOFILE"
   rxvt -name burniso2cd -bg orange -geometry 80x10 -e xorriso -as cdrecord $BURNMULTI -data -eject -v speed=$BURNSPEED dev=$DEVDEV "$ISOFILE"
  else
   echo " cdrecord $BURNMULTI -data -eject -v speed=$BURNSPEED dev=$DEVDEV $ISOFILE"
   rxvt -name burniso2cd -bg orange -geometry 80x10 -e cdrecord $BURNMULTI -data -eject -v speed=$BURNSPEED dev=$DEVDEV "$ISOFILE"
  fi
 else #dvd
  #growisofs uses mkisofs (from cdrtools pkg) but xorriso can replace it ...
  #note, have /etc/profile.d in xorriso pkg that already does this...
  [ "`which xorriso`" != "" ] && export MKISOFS="xorrisofs"
  echo "Now executing:"
  echo " growisofs -speed=$BURNSPEED -Z $CDR=$ISOFILE"
  rxvt -name burniso2cd -bg orange -geometry 80x10 -e growisofs -speed=$BURNSPEED -Z $CDR="$ISOFILE"
 fi
 sync
 echo "...done"

 #v2.10 kirk contributed this...
 # ******Verify burn******
 DEVNAME="`echo -n "$CDR" | grep '/dev' | cut -f 3 -d '/'`"
 if [ ! "$DEVNAME" = "" -a "$RECHECK" = "yes" ];then
  Xdialog --wmclass "mini-cd" --title "Burniso2cd: Finished" --stdout --yesno "Would you like to verify that the burn was successful? 
This may take a while depending on the size of the iso file." 0 0
  if  [ "$?" = "0" ] ;then  
   xmessage -bg "#80ff80" -center -name "burniso2cd" -title "Burniso2cd: Burn Verify" -buttons "OKAY:10" "Make sure the CD/DVD drive is closed,
then click OK"
   if [ "$( md5sum $ISOFILE | cut -f 1 -d " " )" = "$(dd if=$CDR | head -c `stat --format=%s $ISOFILE` | md5sum | cut -f 1 -d " " )" ] ;then 
    xmessage -bg "#80ff80" -center -name "burniso2cd" -title "Burniso2cd: Burn Verify" -buttons "OKAY:10" "The burn has been verified as good!"
   else
    xmessage -bg "#ffc0c0" -center -name "burniso2cd" -title "Burniso2cd: Burn Verify" -buttons "OKAY:10" "The burn was not verified. You should start over"
   fi
  else
   RECHECK="no" #do not check if doing repeat burns.
  fi
 fi

 eject $CDR
 Xdialog --wmclass "mini-cd" --title "Burniso2cd: Finished" --stdout --ok-label "FINISHED" --cancel-label "REPEAT" --yesno "If all went well, you have now burnt iso file
$ISOFILE
to a $MEDIATYPE!

If you want to burn the iso file to another $MEDIATYPE,
click 'REPEAT' button...

Otherwise, click 'FINISHED' to quit..." 0 0
 [ $? -ne 1 ] && break
done #burn-again-loop

###END###
