#!/usr/bin/env bash

#et -x

set -u

case "$(hostname)" in
	esmf04m)
		TEMPDIR=/scratch/strombrg
		;;
	*)
		TEMPDIR="${TMPDIR:-/tmp}"
		;;
esac

TEMPFILE=/"$TEMPDIR"/disk-summary.temp.bz2

if [ "$#" = 0 ]
then
	fses="$(disk-based-mount-points)"
elif [ x"$1" = x--esmf ]
then
	fses="/home /ptmp /ptmp2 /datashare /data"
else
	fses="$@"
	case "$(hostname)" in
		esmf04m)
			echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' 1>&2
			echo '!!! You might want to specify the --esmf option! !!!' 1>&2
			echo '!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!' 1>&2
			;;
	esac
fi

export filesystem=0
export atime=1
export ctime=2
export mtime=3
export uid=4
export username=5
export gid=6
export groupname=7
export size=8
export perms=9
export filename=10 # not precisely, but we don't use it anyway

# using mtee this way should be much more efficient.  The downside is
# that we end up with 3 temporary files instead of one, but the three
# should add up to far less disk space and I/O time than the much larger
# single temp file.
for fs in $fses
do
	if tty -s
	then
		echo Generating raw data for $fs... 1>&2
	fi
	if cd "$fs" > /dev/null 2>&1
	then
		find . -xdev -print0 | xargs -0 file-report | while read line
			do
				echo "$fs $line"
			done
	else
		echo Failed to cd to "$fs" 1>&2
		exit 1
	fi
done | mtee -q \
	"sum-by -u computer-size -t -p -b $filesystem -b $groupname -s $size | expand -15 > $TEMPDIR/groupname" \
	"sum-by -u computer-size -t -p -b $filesystem -b $groupname -b $username -s $size | expand -15 > $TEMPDIR/groupname-username" \
	"sum-by -u computer-size -t -p -b $filesystem -b $username -s $size | expand -15 > $TEMPDIR/username"


#The output will be columnar, with fields in the following order:
#	access time
#	inode change time
#	modification time
#	uid
#	username
#	gid
#	groupname
#	size
#	permissions bits
#	filename (which may contain whitespace)

echo Disk usage as of "$(date)"

echo
echo
echo A number with a pound sign in front of it is a uid or gid without a corresponding username or group name | fmt

echo
echo
echo By filesystem by PI:
cat "$TEMPDIR/groupname"
#bunzip2 -c "$TEMPFILE" | sum-by -u computer-size -t -p -b $filesystem -b $groupname -s $size | expand -15

echo
echo
echo By filesystem PI by user:
cat "$TEMPDIR/groupname-username"
#bunzip2 -c "$TEMPFILE" | sum-by -u computer-size -t -p -b $filesystem -b $groupname -b $username -s $size | expand -15

echo
echo
echo By filesystem by user:
cat "$TEMPDIR/username"
#bunzip2 -c "$TEMPFILE" | sum-by -u computer-size -t -p -b $filesystem -b $username -s $size | expand -15

