#!/bin/sh

# quotagauge - displays network user's disk quote usage.
# usage: quotagauge [-g graphic mode]

# get user's short name.
thisUser=`whoami`

# uncomment to get quota from network.nidb and filter (for jaguar servers)
# maxSpace=`nicl / -read /users/$thisUser homedirectoryquota | awk '{print $2}'`

# get quota from LDAP for panther servers.  comment out if using jaguar server.  replace examples with your info.
maxSpace=`ldapsearch -h hostname -b "dc=example,dc=edu" -x -LLL uid=$thisUser apple-user-homequota|grep apple|awk {'print $2'}`

# convert to MB
maxMegs=`expr $maxSpace / 1024 / 1024`

# Returns user's home directory disk usage in 1024 KB blocks.
currentSpace=`du -sk ~ | awk '{print $1}'`

# convert to MB
currentMegs=`expr $currentSpace / 1024`

# floating point math for percentage used
spaceUsed=`echo "scale=2; $currentMegs / $maxMegs" | bc`
percentUsed=`echo "scale=0; $spaceUsed * 100" | bc | sed -e "s/.00//g"`

if [ $# -eq 0 ]
then
    echo "user: $thisUser used: $currentMegs max: $maxMegs percent: $percentUsed"
    exit 0
fi

while [ $# -gt 0 ]
do 
    case $1 in
        -g) # Use AppleScript to present the usage data in a dialog box.
        eval `echo osascript -e \'tell app \"Finder\" to activate\' \
        -e \'tell app \"Finder\" to display dialog \"Disk usage for $thisUser:  \
        You have used $currentMegs MB of your $maxMegs MB quota. \
        $percentUsed percent full.\" buttons \{\"OK\"\} giving up after 30 default button 1\'`>/dev/null
        break
        ;;
        *) echo "Usage: quotagauge [-g]; option -g for graphic mode" 1>&2
        exit 1
        break
        ;;
    esac
done
