#!/bin/sh

# diskman : This program will hopefully build a tree structure
# for all the drives present on the system. This is regardless to mounts
# defined in /etc/fstab file. However this file is consulted only 
# towards providing information and option to mount or unmount defined drives.
# This program uses kudzu to query some of the drives.
# Copyright (C) 2003 Narayan Natarajan (venkman69@yahoo.com)
# Date  : Wed Nov  5 23:22:39 EST 2003

# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 

# find all drives
drivefile=`mktemp /tmp/diskman_drives.XXXXXX`
cdromfile=`mktemp /tmp/diskman_cdrom.XXXXXX`
kudzu -c HD -p | tr -d '"' | awk -F: '/device/{dev="/dev/" substr($2,2)}
/desc/{desc=substr($2,2);
 printf("%s:%s\n", dev,desc)}
' > $drivefile

tempfile=`mktemp /tmp/diskman.XXXXXX`
tempfile1=`mktemp /tmp/diskman.XXXXXX`
tempfile2=`mktemp /tmp/diskman.XXXXXX`
awkFS=";"
#echo  "Xdialog --ok-label \"Mount\" --treeview \"Partitions\" 0 0 10 " > $tempfile

getpartitions() {

cat $drivefile|
while read a
do
devpath=`echo $a | awk -F: '{print $1}'`
desc=`echo $a | awk -F: '{print $2}'`
echo "DRIVE$awkFS$devpath$awkFS$desc" >> $tempfile
sfdisk -luM $devpath | tr -s " -*" ":" | awk -F: -v fs=$awkFS '/^\//{dr=$1;
siz=$4/1024;
type=7;
if ( $NF == "Empty" ) next;
printf("%s%s%04.1f GB %s", dr,fs,siz,$type);
for(i=type+1;i<=NF;i++){printf ( " %s",$i)};
printf("\n",level);}' >> $tempfile
done


cat $tempfile |
while read devline
do
dev=`echo $devline | awk -F$awkFS '{print $1}'`
if [ "$dev" == "DRIVE" ]
then
	echo "$devline" >> $tempfile1
	continue
fi
mount=`grep "$dev" /proc/mounts | awk '{print $2}'`
tooltip="Mounted as: $mount"
if [ -z "$mount" ]
then
mount=`mount | grep "$dev" | awk '{print $3}'`
tooltip="Mounted as: $mount"
fi
if [ -z "$mount" ]
then
mount=`grep "$dev" /proc/swaps | awk '{print $1}'`
tooltip="SWAP"
fi
if [ -z "$mount" ]
then
mount=`grep "$dev" /etc/fstab | awk '{print $2}'`
tooltip="Not Mounted: $mount"
fi


if [ -z "$mount" ]
then
mount="?"
tooltip="No Mount Point"
fi

echo "$mount$awkFS$devline$awkFS$tooltip" >> $tempfile1
done




echo "DRIVE${awkFS}CDROM Devices" >> $tempfile1
kudzu -c CDROM -p | tr -d '"' | awk -F: '/device/{dev="/dev/" substr($2,2)}
/desc/{desc=substr($2,2);
 printf("%s:%s\n", dev,desc)}
' > $cdromfile

cat $cdromfile|
while read cdrominfo
do
	cddevpath=`echo $cdrominfo | awk -F: '{print $1}'`
	cddesc=`echo $cdrominfo | awk -F: '{print $2}'`
	cdmdev=`ls -ld /dev/cdrom* | grep $cddevpath | awk '{print $(NF-2)}'`
	mount=`grep -w "$cdmdev" /proc/mounts | awk '{print $2}'`
	tooltip="Mounted as: $mount"
	if [ -z "$mount" ]
	then
	mount=`mount | grep "$cdmdev" | awk '{print $3}'`
	tooltip="Mounted as: $mount"
	fi
	if [ -z "$mount" ]
	then
	mount=`grep "$cdmdev" /etc/fstab | awk '{print $2}'`
	tooltip="Not Mounted: $mount"
	fi
dummy="-";
echo "$mount$awkFS$cdmdev$awkFS$cddesc$awkFS$tooltip" >> $tempfile1


done

set - -x

}

mktreeview(){
#cat $tempfile1
printf  "Xdialog --stdout --item-help --ok-label \"Toggle Mount\" --treeview \"Partitions\" 20 75 0 " > $tempfile2
awk -F$awkFS 'BEGIN {level=1}
/cdrom/{



mountpoint=$1
dev=$2
dev_text=$3
tooltip=$4
text=sprintf("%10s %-40s %-15s",dev,mountpoint,dev_text);
printf("\"%s\" \"%s\" %s %d \"%s\" ",dev, text,status,level,tooltip );
next
}
 /DRIVE/{
printf ("\"%s\" \"%-55s\" on 0 \"%s\" ",$2,$2,$NF);
level=1
next
}
{
mountpoint=$1
dev=$2
dev_text=$3
tooltip=$4
status="off"
if ( tooltip == "SWAP" ) status="unavailable";
text=sprintf("%10s %-40s %-15s",dev,mountpoint,dev_text);
if (dev_text ~ /Ext d/) {
text=sprintf("%-55s",$3);
printf("\"%s\" \"%s\" %s %d \"%s\" ",dev, text,status,level,"Extended Partition" );
level ++;
}
else
{
printf("\"%s\" \"%s\" %s %d \"%s\" ",dev, text,status,level,tooltip );
}
}
' $tempfile1 >> $tempfile2
}

togglemount() {
if grep -wq "$1" /proc/mounts; then
	echo "Unmounting...";
	exitrep=`umount $1 2>&1`;
	if [ $? -ne 0 ]; then
		Xdialog -title "FAILED!" --no-buttons --infobox "Unmounting Failed:\n $exitrep" 0 0 3000;
	else
		Xdialog -title "SUCCEEDED!" --no-buttons --infobox "Unmounted : $1" 0 0 3000;

	fi;
else
	echo "Mounting...";
	exitrep=`mount $1 2>&1`;
	if [ $? -ne 0 ]; then
		Xdialog -title "FAILED!" --no-buttons --infobox "Mounting Failed:\n $exitrep" 0 0 3000;
	else
		Xdialog -title "SUCCEEDED!" --no-buttons --infobox "Mounted : $1" 0 0 3000;
	fi;
fi

}

result=0
while [ $result -eq 0 ]
do
getpartitions
mktreeview

chmod 755 $tempfile2
mountdrive=`$tempfile2`
result=$?
if [ $result -eq 1 ]
then
	continue
fi


echo "toggle mounting $mountdrive"

mountpoint=`grep $mountdrive $tempfile1 | awk -F$awkFS '{print $1}'`

echo "mountpoint=$mountpoint"
if [ "$mountpoint" == "?" ]
then
	echo Not Supported Feature
	exit
fi

togglemount $mountpoint
rm $tempfile $tempfile1 $tempfile2
done
