#!/bin/sh
# Script ----------------------------------------------------------------------
# Description - Pwutil is a front end to the rest of the pw utils.
# License     - See LICENSE file for details.
#
# $Author: jayrfink $
# $Date: 2008/03/19 16:03:35 $
# $State: Exp $
#------------------------------------------------------------------------------

progname=${0##*/}
toppid=$$
results=/dev/null
trap "exit 1" 1 2 3 15

bomb()
{
    cat >&2 <<ERRORMESSAGE

ERROR: $@
*** ${progname} aborted ***
ERRORMESSAGE
    kill ${toppid}
    exit 1
}

do_cmd()
{
	cmd=$1
	shift
	args=$@
	$cmd $args || bomb "Could not execute ${cmd} ${args} "
}

optslice()
{
	option="${1//-/}"
	option=$(echo ${option} | cut -c 1)
	echo $option
}

usage()
{
	cat <<_usage_
Usage: ${progname} [command_option [args]]
Usage: ${progname} [-h][-G][-g users...][-d][-U][-u users...][-q PATTERN]
Command Options:
    -h          Print usage message
    -G          Run a report on user groups.
    -g users..  Show what groups a user or users are in.
    -d          Display space usage in all user home directories in kb.
    -U          Run a report on users.
    -u users... Get userinformation for a user or userlist.
    -q PATTERN  Get data on all usernames that match pattern PATTERN.
_usage_
}

# Input parsing - the usage explains what each one does
if [ $# -gt 0 -a "$1" = "-h" ];then
    usage
    exit 0
fi
if [ $# -gt 0 -a "$1" = "--help" ];then
    usage
    exit 0
fi  

while [ "$#" -gt "0" ]; do
	opt=$(optslice $1)
    case $opt in
		h) usage ; exit 0 ;;
		G) do_cmd pwgrprep ;;
		g) shift ; do_cmd pwgrps $@ ;;
		d) do_cmd pwdf ;;
		U) do_cmd pwuserep ;;
		u) shift ; do_cmd pwuser $@ ;;
		q) shift ; do_cmd pwquery $@ ;;
		*) usage ; exit 1 ;;
	esac
shift
done

exit  0

