#!/bin/bash

TMP=/tmp

if [ -n "$DISPLAY" ]; then
  DIALOG=Xdialog
  echo "Click the mouse to tag the device you want to mount read-write, or unmount
   (if mounted). Only choose one device!" > $TMP/tmpmsg1
else
  DIALOG=dialog
  echo "Press the [Space] key to tag the device you want to mount read-write, or unmount (if mounted). Only choose one device!" > $TMP/tmpmsg1
fi

echo "" >> $TMP/tmpmsg1
echo "Device Boot Start End Blocks Id System" >> $TMP/tmpmsg1
if mount | grep -q " on / " ; then
  skip=`mount | grep " on / " | cut -d ' ' -f 1`
  fdisk -l 2>/dev/null | grep -v $skip | grep -v -E "Linux swap|Ext'd|Extended|\? " | grep "^/dev/" > $TMP/tmpmsg2
else
  fdisk -l 2>/dev/null | grep -v -E "Linux swap|Ext'd|Extended|\? " | grep "^/dev/" > $TMP/tmpmsg2
fi

if [ -s $TMP/tmpmsg2 ]; then

Mount() {
if mount | grep -q "^$1" ; then
  M=unmount
fi
}

echo '$DIALOG --title "Mount/Unmount" --cr-wrap --checklist "`cat $TMP/tmpmsg1`" 0 0 0 \' > $TMP/tmpmsg3
cat $TMP/tmpmsg2 | sed "/^$/d" | while read device ; do
device_name="`echo "$device" | awk '{print $1}'`"
unset M
Mount $device_name
device_info="`echo "$device" | awk '{print $2,$3,$4,$5,$6,$7,$8,$9}' | sed "s/\//-/g" | sed "s/ $//g"`"
if grep $device_name $TMP/tmpmsg2 | grep -q NTFS && [ ! "$M" = "unmount" ]; then
if [ "$M" = "unmount" ]; then
echo "$device_name (unmount!)" | sed "/^$/d" | sed -e 's/.*/"&" '"'$device_info'"' off \\/' >> $TMP/tmpmsg3
else
echo "$device_name" | sed "/^$/d" | sed -e 's/.*/"&" '"'$device_info'"' off \\/' >> $TMP/tmpmsg3
echo "$device_name (force!)" | sed "/^$/d" | sed -e 's/.*/"&" '"'$device_info'"' off \\/' >> $TMP/tmpmsg3
fi
else
if [ "$M" = "unmount" ]; then
echo "$device_name (unmount!)" | sed "/^$/d" | sed -e 's/.*/"&" '"'$device_info'"' off \\/' >> $TMP/tmpmsg3
else
echo "$device_name" | sed "/^$/d" | sed -e 's/.*/"&" '"'$device_info'"' off \\/' >> $TMP/tmpmsg3
fi
fi
done

if [ -L /dev/cdrom ]; then
  dev=`ls -l /dev/cdrom | cut -d \> -f 2 | sed "s/ //"`
  dev="`basename $dev`"
  unset M
  Mount /dev/$dev
if [ "$M" = "unmount" ]; then
  echo "/dev/$dev (unmount!)" | sed "/^$/d" | sed -e 's/.*/"&" "CD-ROM drive" off \\/' >> $TMP/tmpmsg3
else
  echo "/dev/$dev" | sed "/^$/d" | sed -e 's/.*/"&" "CD-ROM drive" off \\/' >> $TMP/tmpmsg3
fi
fi

if [ -L /dev/cdrom1 ]; then
  dev=`ls -l /dev/cdrom | cut -d \> -f 2 | sed "s/ //"`
  dev="`basename $dev`"
  unset M
  Mount /dev/$dev
if [ "$M" = "unmount" ]; then
  echo "/dev/$dev (unmount!)" | sed "/^$/d" | sed -e 's/.*/"&" "CD-ROM drive" off \\/' >> $TMP/tmpmsg3
else
  echo "/dev/$dev" | sed "/^$/d" | sed -e 's/.*/"&" "CD-ROM drive" off \\/' >> $TMP/tmpmsg3
fi
fi

echo "2> $TMP/parts" >> $TMP/tmpmsg3

    . $TMP/tmpmsg3
    if [ $? = 1 -o $? = 255 ]; then
       rm -f $TMP/tmpmsg*
       exit
    else

        dev=`sed 's/\/\// \//g' $TMP/parts | sed 's/"//g' | sed 's/\\\//' | awk '{print $1}'`
        force=`sed 's/\/\// \//g' $TMP/parts | sed 's/"//g' | sed 's/\\\//' | awk '{print $2}'`

     if [ "$dev" = "" ]; then
        rm -f $TMP/tmpmsg*
        exit
     fi

     if echo $force | grep -q force ; then
        force=yes
     fi

       if mount | grep -q $dev ; then
          mount=no
          cd /
          mount_dir="`mount | grep $dev | awk '{print $3}'`"
       if [ ! "$1" = "mc" ]; then
          umount $dev 2>/dev/null
          if [ ! $? = 0 ]; then
            fuser -k -m $mount_dir 2>/dev/null
            sleep 2
            umount $dev 2>$TMP/error || umount=no
          fi
       else      
          umount $dev 2>$TMP/error || umount=no
       fi
       fi

     if [ ! "$mount" = "no" ]; then

         mkdir -p /mnt/`echo $dev | cut -b 6-`
       if grep $dev $TMP/tmpmsg2 | grep -q NTFS ; then
       if [ "$force" = "yes" ]; then
         ntfs-3g $dev /mnt/`echo $dev | cut -b 6-` -o force,rw 2>$TMP/error || error=yes
       else
         ntfs-3g $dev /mnt/`echo $dev | cut -b 6-` -o rw 2>$TMP/error || error=yes
       fi
       elif grep $dev $TMP/tmpmsg2 | grep -q BSD ; then
         mount -t ufs -o rw,ufstype=ufs2 $dev /mnt/`echo $dev | cut -b 6-` 2>$TMP/error || error=yes
       elif grep $dev $TMP/tmpmsg2 | grep -q Solaris ; then
         mount -t ufs -o rw,ufstype=sunx86 $dev /mnt/`echo $dev | cut -b 6-` 2>$TMP/error || error=yes
         if [ "$error" = "yes" ]; then
           unset error
           mount -t ufs -o rw,ufstype=sun $dev /mnt/`echo $dev | cut -b 6-` 2>$TMP/error || error=yes
         fi       
       else
         mount $dev /mnt/`echo $dev | cut -b 6-` 2>$TMP/error || error=yes
       fi
       
       if [ "$error" = "yes" ]; then
         $DIALOG --title "ERROR!" --textbox "$TMP/error" 15 70
         exit
       fi
         $DIALOG --title "Done!" --msgbox "`mount | grep "^$dev "`" 0 0

   else
     if [ ! "$umount" = "no" ]; then
       $DIALOG --title "Done!" --msgbox "Unmounted $dev!" 0 0
     else
       $DIALOG --title "ERROR!" --textbox "$TMP/error" 15 70
     fi
    fi
  fi    
       rm -f $TMP/tmpmsg*
fi
