#!/bin/bash
#####################################################
# $Id: mnt,v 1.1 2004/06/11 11:22:37 bish Exp $
# Licence: Released under GPL
#
# Notes:
#
# o This script mount devices as an user from the
#   device list in /etc/fstab loadable by user
# o /etc/fstab needs to be properly configured
#   before use
# o Only one line needs to be edited, having the
#   variable DEVICES
# o DEVICES need to exist in /etc/fstab entry
#
# o Uses 'df' command to elicit of device is
#   already mounted or not
#
#####################################################

####################[ Devices ]######################
# Annotate all mount points within quotes with a
# space as seperator. EDIT THIS ! EDIT THIS !
#####################################################

DEVICES="/mnt/floppy /mnt/cdrom /mnt/pen"

#####################################################
# Do not edit below this
#####################################################

PROGNAME=`basename $0`
VERSION="1.1"
TEMP_FILE=$HOME/$PROGNAME.$$.1
ROW=3

###############[ ANSI Colour Codes]##################
red="\033[31;40;1m"		# red
grn="\033[32;40m"		# green
brn="\033[33;40m"		# brown
blu="\033[34;40;1m"  		# blue
mgt="\033[35;40m"               # magenta
cyn="\033[36;40m"		# cyan
wht="\033[37;40;1m"		# bright white
yel="\033[33;40;1m"		# yellow
nor="\033[m"       		# Reset color-code
###################################################

###################################################
# Generic Functions for screen I/O
###################################################

function print
{
    echo -en "\033[$1H\033[$2G$3"$4$nor
}
    
function gotoXY
{
    echo -en "\033[$1H\033[$2G"
}

function do_it
{
      print $((LASTCNT+6)) 10 $nor "Confirm $mgt $ACT $nor [y/n] : "
      read YN
      gotoXY $((LASTCNT+7)) 10
      case $YN in
      Y|y) ${CMD[$OPT]}
           if [ "$CD" = "y" ]; then
              eject
           fi
           ;;
        *) echo
      esac
      print $((LASTCNT+8)) 10 $nor "Press [$yel Return $nor] to quit"
      read
      gotoXY $((LASTCNT+9)) 10
}
      
#################################################
#   Main routine
#################################################
clear

print $ROW 10 $wht "$PROGNAME"
print $ROW 47 $brn "Version: $VERSION"
print $((ROW+2)) 10 $nor "Utility to mount local devices as : $mgt $USER $nor"
print $((ROW+2)) 10 $wht "--------------------------------------------------"
print $((ROW+3)) 10 $grn "Sl"
print $((ROW+3)) 15 $grn "Device"
print $((ROW+3)) 31 $grn "Status"
print $((ROW+3)) 45 $grn "Action"   
print $((ROW+4)) 10 $wht "--------------------------------------------------"
CNT=0


for i in $(echo $DEVICES); do
    CNT=$((CNT+1))
    df | grep $i > $TEMP_FILE
    if [ -s $TEMP_FILE ]; then
       STATUS="$blu mounted"
       DEV[$CNT]="$i"
       CMD[$CNT]="umount $i"
       ACTION="[$mgt UnMOUNT $nor]"
       CD="n"
       if [ "$i" = "/mnt/cdrom" ]; then
          CD="y"
       fi   
    else
       STATUS="$cyn NOT mounted"
       DEV[$CNT]="$i"
       CMD[$CNT]="mount $i"
       ACTION="[$grn MOUNT $nor]"
    fi 

    print $((CNT+9)) 10 $cyn "$CNT]"
    print $((CNT+9)) 15 $nor "${DEV[$CNT]}"
    print $((CNT+9)) 30 $nor "$STATUS" 
    print $((CNT+9)) 45 $nor "$ACTION"

done

LASTCNT=$((CNT+9))
QUIT="n"

while [ "$QUIT" = "n" ]; do
   print $((LASTCNT+1)) 10 $cyn "0]"
   print $((LASTCNT+1)) 15 $red "Abort"
    
   print $((LASTCNT+3)) 10 $nor "Enter option (0-$CNT) : "
   print $((LASTCNT+4)) 10 $wht "--------------------------------------------------"
   gotoXY $((LASTCNT+3)) 32
   read OPT
   gotoXY $((LASTCNT+5)) 10   
   
   if [ $OPT -lt 1 ]; then
      echo "Bye"
      QUIT=1
   else         
      if [ $OPT -gt $CNT ]; then
         QUIT="n"
      else
         ACT="${CMD[$OPT]}"
         do_it
         QUIT="y"
      fi        
   fi
 done

##################################################

rm -f $TEMP_FILE   
exit
              
