#!/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


export MEDIA_DIALOG='
<window title="Grafburn: ディスクのセットとセッションタイプ" icon-name="cd" window_position="1">
 <vbox>
  <frame 設定>
   <hbox>
    <text use-markup="true">
     <label>"フォーマットしたいメディアタイプを選んでからディスクを挿入して下さい。あなたが完全に空のボックスをチェックすると、cdrecordはディスク上の全てのデータを完璧に消去します。あるいは、高速セッティングでPMA、TOCとプリギャップだけを消去します。通常は高速オプションで十分です。しかし一部のドライブは完全フォーマットが必要かもしれません。「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>COMPLETE</variable>
    </checkbox>
   </hbox>
  </frame>
  <hbox>
   <button help>
    <action>defaulttexteditor /usr/local/apps/Media/Grafburn/help.txt &</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 '"'`" #'
COMPLETE="`echo $MEDIA | tr ' ' "\n" | grep COMPLETE | 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 $COMPLETE in
	false)
	OPTION="fast blanking"
	COMMAND="fast"
	;;
	true)
	OPTION="slow blanking"
	COMMAND="all"
	;;
	*)
	exit 0
	;;
esac

if [ "$MEDIATYPE" = "CD" ];then
 sync
 if [ ! "`echo "$BURNERDRIVE" | grep '/dev/hd'`" = "" ];then
  DEVDEV="ATAPI:$BURNERDRIVE"
 else
  DEVDEV="$BURNERDRIVE"
 fi
 rxvt -name grafburn -bg black -fg green -geometry 80x10 -title \
  "Grafburn: blanking cd media" -e cdrecord blank=$COMMAND -eject -v \
  speed=4 dev=$DEVDEV
else #dvd
 rxvt -name grafburn -bg black -fg green -geometry 80x10 -title \
  "Grafburn: blanking dvd media" -e cdrecord blank=$COMMAND -eject -v \
  -speed=4 dev=$DEDEV
fi
sync

Xdialog --title "Grafburn: blanking media" --icon $APPDIR/icons/media-optical.png \
--msgbox "The $MEDIATYPE is now blank and ready for reuse." 0 0

###END###
