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

DEVICES="$*"
[ -z "$DEVICES" ] && DEVICES="/dev/cdrom /dev/cdwriter"

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 | awk '{print $2}'`

    # Test if mount point has being mounted
    df | grep "$MNT" > /dev/null
    if [ $? = 0 ]; then
       #echo $MNT has been mounted
       # no need to mount it
       DEV=""
    else    
       echo Going to mount it at $MNT
       MSG=`mount $MNT 2>&1`
       if [ ! $? = 0 ]; then
         MSG=`echo $MSG | cut -f2 -d :`
         message "ERROR" "Could not mount $DEV." "$MSG"
         exit 2;
       fi
       sleep 3
    fi

    # explore it
    explorer $MNT
    
    # unmount if necessary
    if [ "$DEV" ]; then
       umount $MNT
       if [ ! $? = 0 ]; then
          echo "WARNING" "Could not unmount $DEV."   
       fi
    fi
    exit 0
  fi
done

message "ERROR" "Could not find any device: " "$DEVICES"
exit 1
