#!/bin/sh
#January, 2007 Nathan Fisher

# Determine the path to this application.
CURDIR="`pwd`"
APPDIR=`dirname "$0"`
cd "${APPDIR}"
APPDIR="`pwd`"
cd "${CURDIR}"

PROFILE=$HOME/.config/grafburnrc
. $PROFILE
#tmp file
TMPFILE=$HOME/.config/grafburntmp

if [ "$PREVISODIR" = "" ];then
 PREVISODIR=/
fi

ISOFILE=$1
if [ "$ISOFILE" = "-select" ];then
  export MAIN_DIALOG='
  <window title="Grafburn: ISOイメージファイルの選択" icon-name="document-open" window_position="1">
  <vbox>
    <chooser>
      <height>500</height><width>600</width>
      <variable>CHOOSER</variable>
    </chooser>
    <hbox>
      <button help>
        <action>man grafburn &</action>
      </button>
      <button cancel>
        <action>EXIT:abort</action>
      </button>
      <button ok></button>
    </hbox>
  </vbox>
  </window>
  '
  
  RETURN="`gtkdialog3 --program=MAIN_DIALOG`"
  ABORT="`echo $RETURN | grep abort`"
  [ ! "$ABORT" = "" ] && exit 0
  ISOFILE="`echo $RETURN | cut -f 2 -d '"'`" #'
  [ "$ISOFILE" = "" ] && exit 1
  ISISO="`echo $ISOFILE | grep .iso`"
  [ "$ISISO" = "" ] && exit
  ISODIR="`dirname $ISOFILE`"
  cat $PROFILE | grep -v PREVISODIR > $TMPFILE
  echo "PREVISODIR=$ISODIR" >> $TMPFILE
  mv -f $TMPFILE $PROFILE
fi


export MEDIA_DIALOG="
<window title=\"Grafburn: ディスクとセッションタイプの設定\" icon-name=\"cd\" window_position=\"1\">
 <vbox>
  <frame 設定>
   <hbox>
    <text use-markup=\"true\">
     <label>\"書き込みたいメディアタイプを選択して適切なメディアを入れて下さい。ここではマルチセッションを無効、有効にもできます。「OK」を押すと同時に <b>Grafburnはすぐにディスクに書き込みを開始します！</b>\"</label>
    </text>
   </hbox>
   <hbox>
    <combobox>
     <variable>MEDIATYPE</variable>
     <item>CD</item>
     <item>DVD</item>
    </combobox>
   </hbox>
   <hbox>
    <checkbox>
     <label>マルチセッションで書き込む</label>
     <variable>MULTISESSION</variable>
     <default>$BURNMULTI</default>
    </checkbox>
   </hbox>
  </frame>
  <hbox>
   <button help>
    <action>man grafburn &</action>
   </button>
   <button ok></button>
   <button cancel>
    <action>EXIT:abort</action>
   </button>
  </hbox>
 </vbox>
</window>
"
MEDIA="`gtkdialog3 --program MEDIA_DIALOG`"
[ ! "`echo $MEDIA | grep abort`" = "" ] && exit 0
MEDIATYPE="`echo $MEDIA | tr ' ' "\n" | grep MEDIATYPE | cut -f 2 -d '"'`" #'
MULTI="`echo $MEDIA | tr ' ' "\n" | grep MULTISESSION | cut -f 2 -d '"'`" #'

if [ "$BURNDEV" = "" ];then
  gxmessage -center -title "Grafburn: error" -bg "#dcdad0" -buttons \
  "GTK_STOCK_DIALOG_ERROR" -default "GTK_STOCK_DIALOG_ERROR" \
   "There is no default cd burner device set.
Launching preferences dialog."
  exec $APPDIR/set_prefs
fi
BURNERDRIVE=$BURNDEV

while [ ! "`mount | grep "$BURNERDRIVE"`" = "" ];do
 gxmessage -bg red -center -name "grafburn" -title "Grafburn: ERROR" \
 -buttons "GTK_STOCK_DIALOG_ERROR" -default "GTK_STOCK_DIALOG_ERROR" \
 "The CD/DVD drive, described as:
 $BURNERDRIVE
is currently mounted.
Use pmount or MUT to unmount it.

Please unmount the drive, then try again..."
done

case $MEDIATYPE in
 CD)
  if [ "$MULTI" = "false" ];then
   BURNMULTI="-dao"
   BURNMSG=""
  else
   BURNMULTI="-multi -tao -pad"
   BURNMSG=""
  fi
  ;;
 DVD)
  if [ $MULTI = false ];then
   BURNMULTI=""
   BURNMSG="(blank DVD-R. A DVD-RW will be fast-wiped if it already has data)"
  else
   BURNMULTI="yes"
   BURNMSG="(blank DVD-R. A DVD+/-RW will be fast-wiped if it has data)"
  fi
  ;;
 *)
  exit
  ;;
esac

while [ 1 ];do #burn-again-loop

if [ "$MEDIATYPE" = "CD" ];then
 sync
 if [ ! "`echo "$BURNERDRIVE" | grep '/dev/hd'`" = "" ];then
  DEVDEV="ATAPI:$BURNERDRIVE"
 else
  DEVDEV="$BURNERDRIVE"
 fi
 echo "Now executing:"
 echo " cdrecord $BURNMULTI -data -eject -v speed=4 dev=$DEVDEV $ISOFILE"
 rxvt -name grafburn -bg black -fg green -geometry 80x10 -title \
  "Grafburn: burn cd " -e cdrecord $BURNMULTI -data -eject -v speed=$CDSPEED \
  dev=$DEVDEV "$ISOFILE"
else #dvd
 if [ "$MULTI" = "false" ];then
  echo "Now executing:"
  echo " growisofs -speed=4 -Z $BURNERDRIVE=$ISOFILE"
  rxvt -name grafburn -bg black -fg green -geometry 80x10 -title \
   "Grafburn: burn dvd" -e growisofs -speed=$DVDSPEED -Z $BURNERDRIVE="$ISOFILE"
  growisofs -M $BURNERDRIVE=/dev/zero
 else
  echo "Now executing:"
  echo " growisofs -speed=4 $BURNERDRIVE=$ISOFILE"
  rxvt -name grafburn -bg black -fg green -geometry 80x10 -title \
   "Grafburn: burn dvd" -e growisofs -speed=$DVDSPEED -Z $BURNERDRIVE="$ISOFILE"
 fi
fi
sync
echo "...done"

#v2.10 kirk contributed this...
# ******Verify burn******
DEVNAME="`echo -n "$CDR" | grep '/dev' | cut -f 3 -d '/'`"
if [ ! "$DEVNAME" = "" ];then
 gxmessage -bg "80ff80" -center -name "grafburn" -title \
 "Grafburn: Finished" -buttons "GTK_STOCK_YES:1,GTK_STOCK_NO:0" -default \
 "GTK_STOCK_NO" "Would you like to verify that the burn was successful? 
This may take a while depending on the size of the iso file."
 if  [ "$?" = "1" ] ;then  
  gxmessage -bg "80ff80" -center -name "grafburn" -title "Grafburn: Burn Verify" -buttons "GTK_STOCK_OK:10" "Make sure the CD/DVD drive is closed,
and 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 
   gxmessage -bg "80ff80" -center -name "grafburn" -title "Grafburn: Burn Verify" -buttons "GTK_STOCK_OK:10" "The burn has been verified as good!"
  else
   gxmessage -bg "ffc0c0" -center -name "grafburn" -title "Grafburn: Burn Verify" -buttons "GTK_STOCK_OK:10" "The burn was not verified. You should start over"
  fi
 fi
fi

eject $BURNERDRIVE
gxmessage -bg green -center -name "grafburn" -title "Grafburn: Finished" \
-buttons "FINISHED:10,REPEAT:11" "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..."
[ $? -lt 11 ] && break
done #burn-again-loop

###END###
