#!/bin/sh
# Mount and view device
# License : GNU GPL
# (c) Eko M. Budi, 2004
# (c) Vector Linux, 2004
# Modified by Nightflier for Vector Linux 6.0

# Display options menu
export MAIN_DIALOG='
<window title="CD/DVD" icon-name="menu-Reload.png">
 <vbox>
  <hbox>
    <button>
      <label>"  Data Disc  "</label>
      <input file>/usr/share/icons/ROX-nuvola/filesystems/folder_blue.png</input>
      <action>echo DATA > /tmp/cd-dvd-choice</action>
      <action>killall gtkdialog</action>
    </button>
  </hbox>
  <hbox>
    <button>
      <label>"  Audio CD  "</label>
      <input file>/usr/share/icons/ROX-nuvola/apps/kscd.png</input>
      <action>echo AUDIO > /tmp/cd-dvd-choice</action>
      <action>killall gtkdialog</action>
    </button>
  </hbox>
  <hbox>
    <button>
      <label>" Video DVD "</label>
      <input file>/usr/share/icons/ROX-nuvola/apps/xine.png</input>
      <action>echo VIDEO > /tmp/cd-dvd-choice</action>
      <action>killall gtkdialog</action>
    </button>
  </hbox>
  <hbox>
    <button>
      <label>"    Vburn     "</label>
      <input file>/usr/share/icons/ROX-nuvola/devices/cdwriter_mount.png</input>
      <action>echo VBURN > /tmp/cd-dvd-choice</action>
      <action>killall gtkdialog</action>
    </button>
  </hbox>
  <hbox>
   <button cancel></button>
  </hbox>
 </vbox>
</window>
'

gtkdialog --program=MAIN_DIALOG

# what does user want to do?
choice=$(cat /tmp/cd-dvd-choice)
rm /tmp/cd-dvd-choice

# mount data disc and explore in file manager
if [ "$choice" = "DATA" ];then
for i in 5 4 3 2 1; do
DEVICES="$*"
[ -z "$DEVICES" ] && DEVICES="/dev/cdwriter /dev/dvd /dev/cdrom $(/sbin/probedisk |grep "|cdrom|"|cut -d "|" -f1)"

 for DEV in $DEVICES; do
  # Find entry in /etc/fstab
  echo Trying $DEV ...
  LINE=`grep -e "^\s*$DEV" /etc/fstab`
  if [ $? = 0 ]; then

    # Get the mount point
    MNT=`echo $LINE | cut -f2 -d ' '`

    # Test if mount point has being mounted
    df | grep "$MNT" > /dev/null
    if [ $? = 0 ]; then
       echo "$MNT has being mounted"
       DEV=""
    else
       echo "Going to mount it at $MNT"
       if ! mount $MNT; then
         continue;
       fi
       # Give time to settle
       sleep 3
    fi

    # explore it
	if [ -e /usr/bin/xfe ];then xfe $MNT;else xterm -e mc $MNT;fi

    # unmount if necessary
    if [ "$DEV" ]; then
       umount $MNT
       if [ ! $? = 0 ]; then
          echo "WARNING" "Could not unmount $DEV."   
       fi
    fi
    exit 0
  fi
 done
 echo "Trying again.. ($i)"
 sleep 5
done
# If it goes this far, there is something amiss
zenity --error --title "Error" --text " Possible causes: \n - No disc in drive \n - Drive not ready \n - No drive found"
exit 1
fi

# play audio CD
if [ "$choice" == "AUDIO" ];then
if [ "$(wodim -toc | grep "track:" -o)" ];then
    xmms /mnt/cdrom &
    exit 0
    else
      zenity --error --title "Error" --text " No audio CD found"
    fi
fi

# play video DVD
if [ "$choice" ==  "VIDEO" ];then
  xine -D -f -g dvd:/ &
  exit 0
fi

# burn (re)writable CD
if [ "$choice" == "VBURN" ];then
  vburn &
  exit 0
fi
