#!/bin/bash


############################################################################
#    Copyright (C) 2008 by David                                           #
#    david@dward.us                                                        #
#                                                                          #
#    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.             #
###########################################################################



# quick and nasty script to sort and format the output from du --max-depth=1 by DaveQB
# Needs a lot of work, but works good enough for now.



function formatted {


	#printf "${Z} is \t\t`echo ${Y}|cut -c1-${1}`,`echo ${Y}|cut -c${2}-`MB's\n"
	printf "`echo ${Y}|cut -c1-${1}`,`echo ${Y}|cut -c${2}-`MB's used by \t\t ${Z}\n"


}
# Make a temporary file
TEMP=`mktemp`

# Output the list to the temporary file.
du --max-depth=1 "$1" | sort -nr > "$TEMP"


#echo "`echo $B | cut -c1,2,3`,`echo $B | cut -c4,5,6`"


while read -r line
do #echo "========"
	# This is the raw size.
	Y=`echo "$line" | cut -f1`
	# This is the directory name
	Z=`echo "$line" | cut -f2`
	# Value brought back to MB
	Y=$(($Y/1024))
	# Check if we need to display it in GB's
	if [ ${#Y} -lt 4 ] 
	then 
		printf "${Y}MB's used by \t\t\t ${Z}\n"
	else
		formatted "$((${#Y} - 3))" "$((${#Y} - 2))"

	fi
	
done < "$TEMP"

