#!/bin/sh
# Append tracks to a multisession disk
# May 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

#Explain to the user what to do
if [ ! "$OPENDIR" = "false" ];then
 rox $TMPDIR
 Xdialog --icon $APPDIR/icons/big/drive-optical.png \
 --left --title "Grafburn: ファイルとフォルダの追加" --msgbox \
"tempdirにファイラウィンドウを開きました。\n\
このディレクトリに含めたい好きなファイルや\n\
ディレクトリを置いて下さい。\n\
\n\
注意：スペースを節約するためにファイルを\n\
tempディレクトリにリンクするだけで、\n\
ファイルは通常通りISOイメージになります。\n\
\n\
ここに置かれたあらゆるファイルは\n\
ディスクイメージになります。\n\
完了するまでこのウィンドウは閉じないで下さい。\n\n\
完了したら「OK」ボタンを押して下さい。" 0 0

 #Close the filer window
 rox -D $TMPDIR
else
 Xdialog --icon $APPDIR/icons/big/drive-optical.png \
 --left --title "Grafburn: ファイルとフォルダの追加" --msgbox \
"Grafburnは「"$TMPDIR"」内の全部のファイルやディレクトリを\n\
ISOイメージの作成に使います。スペースを節約するために\n\
あなたのファイルをtempディレクトリにシンボリックリンクして下さい。\n\
「OK」ボタンを押す前に、ディスクに含めたい全部があるか\n\
確認して下さい..." 0 0
fi
NAME=newsession.iso

export MEDIA_DIALOG="
<window title=\"Grafburn: append tracks\" icon-name=\"cd\" window_position=\"1\">
 <vbox>
  <frame Settings>
   <hbox>
    <text use-markup=\"true\">
     <label>\"Please select the media type you wish to burn, then insert the proper media. You can also enable or disable multisession burn here. Please insert the disk you want to append tracks to into your burning drive now, because as soon as you press OK <b>Grafburn will immediately try to burn the disk!</b>\"</label>
    </text>
   </hbox>
   <hbox>
    <combobox>
     <variable>MEDIATYPE</variable>
     <item>CD</item>
     <item>DVD</item>
    </combobox>
   </hbox>
   <hbox>
    <text>
     <label>Set the volume name:</label>
    </text>
    <entry>
     <default>grafburn_project</default>
     <variable>VOLUME</variable>
    </entry>
   </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 '"'`" #'
VOLUME="`echo $MEDIA | tr ' ' "\n" | grep VOLUME | cut -f 2 -d '"'`" #'
PUBLISHER="`echo $MEDIA | tr ' ' "\n" | grep PUBLISHER | cut -f 2 -d '"'`" #'

if [ "$BURNDEV" = "" ];then
  export SET_BURNER_DIALOG="
  <window title=\"Grafburn: error\" icon-name=\"gtk-dialog-error\" window-position=\"1\">
   <vbox>
    <frame>
     <text>
      <label>There is no default cd burner device set.</label>
     </text>
     <text>
      <label>Launching the preferences dialog.</label>
     </text>
    </frame>
    <hbox>
     <button ok></button>
    </hbox>
   </vbox>
  </window>"
  gtkdialog3 --program SET_BURNER_DIALOG
  exec $APPDIR/set_prefs
fi
BURNERDRIVE=$BURNDEV

 export UMOUNT_DIALOG="
 <window title=\"Grafburn: unmount\" icon-name=\"gtk-dialog-error\" window-position=\"1\"
  <vbox>
   <frame>
    <text>
     <label>The CD/DVD drive, described as: $BURNERDRIVE is currently mounted.</label>
    </text>
    <text>
     <label>Use pmount or MUT to unmount it before continuing.</label>
    </text>
   </frame>
   <hbox>
    <button ok></button>
   </hbox>
  </vbox>
 </window>"

while [ ! "`mount | grep "$BURNERDRIVE"`" = "" ];do
 gtkdialog3 --program UMOUNT_DIALOG
done

case $MEDIATYPE in
	CD)
	BURNMULTI="-multi -tao -pad"
	BURNMSG=""
	if [ ! "`echo "$BURNERDRIVE" | grep '/dev/hd'`" = "" ];then
	 DEVDEV="ATAPI:$BURNERDRIVE"
	else
	 DEVDEV="$BURNERDRIVE"
	fi
	MSINFO="`cdrecord dev=$BURNDEV -msinfo`"
	rxvt -name grafburn -bg black -fg green -geometry 80x10 -title \
	 "Grafburn: append cd " -e mkisofs -D -f -J -R -o $OUTDIR/$NAME -V "$VOLUME" \
	 -publisher "$PUBLISHER" -p "$PREPARER" -C $MSINFO -M $BURNDEV "$TMPDIR"
	rxvt -name grafburn -bg black -fg green -geometry 80x10 -title \
	 "Grafburn: burn cd " -e cdrecord $BURNMULTI -data -eject -v speed=$CDSPEED \
	 dev=$DEVDEV "$OUTDIR/$NAME"
	rm -f $OUTDIR/$NAME
	eject $BURNDEV
	export OK_DIALOG="
	<window title=\"Grafburn: finished\" icon-name=\"gtk-dialog-info\" window-position=\"1\">
	 <vbox>
	  <frame success>
	   <text>
	    <label>If all went well the files should be appended to the cd.</label>
	   </text>
	  </frame>
	  <hbox>
	   <button ok></button>
	  </hbox>
	 </vbox>
	</window>"
	gtkdialog3 --program OK_DIALOG
	unset OK_DIALOG
	exit 0
	;;
	DVD)
	rxvt -name grafburn -bg black -fg green -geometry 80x10 -title \
	 "Grafburn: append dvd " -e growisofs -M $BURNDEV -D -f -J -R -V "$VOLUME" \
	 -publisher "$PUBLISHER" -p "$PREPARER" "$TMPDIR"
	eject $BURNDEV
	export OK_DIALOG="
	<window title=\"Grafburn: finished\" icon-name=\"gtk-dialog-info\" window-position=\"1\">
	 <vbox>
	  <frame success>
	   <text>
	    <label>If all went well the files should be appended to the dvd.</label>
	   </text>
	  </frame>
	  <hbox>
	   <button ok></button>
	  </hbox>
	 </vbox>
	</window>"
	gtkdialog3 --program OK_DIALOG
	unset OK_DIALOG
	exit 0
	;;
esac












