#!/bin/bash -x
# This script can show the users space usage.
# Use check-space.sh {GREATER-THAN-SPACE}
# example:
#    check-space.sh 10240
#
# This command will show the user space usage
# greater than 10240 K-Bytes 
#
# example 2:
#    check_space.sh 10 -m
#
# List user space usage greater than 10M
# The "-m" will show more friendly!
#
# Programmer: Tzu-Yi Liao (babiku007@yahoo.com.tw)

UUID=`echo $UID`
[ "$UUID" != "0" ] && echo -e "You must be root to use this utility!" && exit 1; 
[ "$1" == "" ] && echo -e "Please give a size to check!\n" && exit 1;
clear
echo "Scanning user home directory, this task will spend some minutes ...";
user_home=`cat /etc/passwd | awk -F ':' '$3 > 499 {print $6}'`

for u in $user_home
do
	u_inf=`du -s ${u} 2> /dev/null`
	u_path=`echo $u_inf | awk '{print $2}'`
	u_space_kb=`echo $u_inf | awk '{print $1}'`
	u_space_mb=$((u_space_kb / 1024));
	if [ "$2" == "-m" ] && [ $((u_space_mb)) -gt $1 ]; then
		echo -en "${u_space_mb} MB\t\t${u_path}\n"
	elif [ "$2" == "" ] && [ $((u_space_kb)) -gt $1 ]; then
		echo -en "${u_space_kb} KB\t\t${u_path}\n"
	fi
done
exit 0;
