#!/bin/bash
if [ "$1" = "-q" ]; then
	quota=$2
	shift
	shift
else
	quota=10000
fi

nfiles=3			# number of files we show
ndirs=3			# ditto for directories
tmpfile=/tmp/.diskuse.$USER 	# name of temp file used
program_name=diskuse 	# name of this program 
MAILDIRS="mail mbox"	# list of directories where mail is stored
MAILFILES="mail.txt" 	# list of files where mail is likely to be stored
maxMailSize=`perl -e "print $quota*0.4"` # warn if the user has more mail than this
largeMailFolders=""	# list of oversize mail folders
helpfile="/home/elflord/bin/help.txt"	# help file for program
		

# Interactive prompt if user wants to delete a file
# I moved it here because it was getting loooonnnngggg...

function deleteFile {
local input_valid=0
while [ $input_valid -eq 0 ]
	do
	echo "choose a file to delete, enter a number corresponding "
	echo " to the list of files above: 1 .. $nfiles"
		read Y
		if [ -n "$Y" -a $( expr "$Y" : "[0-9]*" ) -ne 0 ]; then
		Filename="filename$Y"
		if [ -n "$(eval echo \$$Filename)" ]; then
			input_valid=1
			echo "confirm action: delete $(eval echo \$$Filename) ? (y/n)"
				read CONFIRM
			case "${CONFIRM}" in (y|Y|yes|YES) 
				echo 
				echo "$(eval echo \$$Filename) deleted"
				echo
				echo "press \"return\" to continue"
				read
				return 0;;
						(*)
				echo "$(eval echo \$$Filename) not deleted"
				echo
				echo "press \"return\" to continue"
				read
				return 1;;
			esac
		fi
		fi
	if [ $input_valid -eq 0 ]; then 
		echo -n "that's not a sensible value. Try again. a number,"
		echo "1 .. $nfiles  please"
	fi
	done
}

# Function to get (and print ) the largest $ndirs directories. Currently not used.

function getDirSizes() {
ls -l ${dir}|grep ^d>/dev/null||return 1
echo "your largest ${ndirs} directories and size (in Kilobytes) are:"
echo 

{ for X in ${dir}/* ${dir}/.[^.]*
do
if [ -d "$X" ]; then
du --summarize $X
fi
done
}|sort -n|tail --lines ${ndirs}|
sed -e "s/$(echo $dir/|sed -e 's/\//\\\//g' )//" 
}

# Function to get (and print) the largest $nfiles files




function mailDiagnostics() {


for x in $MAILDIRS; do
Size=${x}size
if [ -d ${dir}/$x ]; then 
declare $Size=$(du --summarize ${dir}/$x|awk '{print $1}')
echo "your total disk usage for your email directory, $x is: $(eval echo \$$Size)K"
else
	declare $Size="0"
fi

if [ "$(eval echo \$$Size)" -ge "$maxMailSize" ];then
echo "your $x directory is very large, try deleting some messages "
echo "from your email folders. "
fi

done


if [ -n "largeMailFolders" ]; then 
echo "Consider quitting and using your email program"
echo "to delete some mail from the following folders "
echo 
echo "$largeMailFolders $largeMailFiles"|sed -e 's/[^ ]*\///g'
fi
}

function mainDiagnostics(){

if [ -d ${dir}/public_html ]; then 
webpageSize=$(du --summarize ${dir}/public_html|awk '{print $1}')
echo "your disk usage for your webpage (public_html) is: $webpageSize K"
fi


total_diskuse=$(du --summarize ${dir}|awk '{print $1}')
echo "Your total disk usage for your home directory is: ${total_diskuse}K"

if [ "$total_diskuse" -ge $quota ]; then 
echo "you are over quota "  
fi

}



if [ -z "$1" ] ; then 
dir=$HOME 
	else
dir=$1
fi

# main loop for interactive prompting

while true  	
	do
largeMailFolders=""	# list of oversize mail folders under $MAILDIRS
largeMailFiles=""		# list of oversize mailfiles from $MAILFILES
# getDirSizes
echo
echo "your largest ${nfiles} files and their size (in Kilobytes) are : "
echo

find "$dir" -type f -regex ".*" -printf "%k %p\n" |
sort -n  |
# this (computationally expensive) line sort of 
# eliminates mailfiles from the list.
# egrep -v "$(echo "$MAILDIRS"|sed -e 's/ /|/g' -e 's/[0-9] */^/')|$(echo "$MAILFILES"|sed -e 's/ /|/g')"|	
tail --lines=${nfiles}  |
# cat -n |
sed -e "s/$(echo $dir/|sed -e 's/\//\\\//g' )//" |
sed  -e 's/^[\t ]*//'  > $tmpfile

X=1
while [ $X -le $nfiles ];  do
declare filename=filename$X		# "pointer" to filename1 , etc
declare filenumber=filenumber$X	# ditto for filenumber
declare $filename="$( sed -e "${X}!d" -e  's/[0-9]* //' -e s/\"/\\\"/ $tmpfile )"
declare $filenumber="$( sed -e "${X}!d" -e 's/ .*//' $tmpfile )" 


echo  -n "($X) $(eval echo \$$filename) :"
echo  -n "$(eval echo \$$filenumber)K"


for x in $MAILDIRS 
do
if [ $(expr match "$(eval echo \$$filename)" "$(echo "$x/\|$x\$"|sed -e 's/\./\\\./g')" )  -ne 0 ]
then 
	echo " ( mail folder )" 
	if [ -n "$largeMailFolders" ]; then
	largeMailFolders="${largeMailFolders} $(eval echo \$$filename)"
		else
	declare largeMailFolders="$(eval echo \$$filename)"
	fi
fi
done

for x in $MAILFILES
do
if [ $(expr match "$(eval echo \$$filename)" "$(echo "$x/\|$x\$"|sed -e 's/\./\\\./g')" )  -ne 0 ]
then 
	echo " ( mail folder )" 
	if [ -n "$largeMailFiles" ]; then
	largeMailFiles="${largeMailFiles} $(eval echo \$$filename)"
		else
	declare largeMailFiles="$(eval echo \$$filename)"
	fi
fi
done

echo
X=$( expr $X + 1 )

done


echo
mailDiagnostics
echo
mainDiagnostics

echo
echo -n " q : quit   "
echo -n " d : delete a file   "
echo " h : help"
read X
case $X in 	(q|Q)
	echo "goodbye, ${USER}. "
	echo "Type $program_name if you want to return to this later"
	exit 0;;
	 	(d|D)
	deleteFile;;
		(h|H)
	less -Q "$helpfile"
	echo "press \"return\" to continue"
	read
	clear;;
esac

echo


done