#!/bin/csh -f

# Do some sanity checking and reporting on quotaadmin.conf file.
# Note: This only looks at home directory quotas.

switch (`uname -s``uname -r`)
case SunOS5*:
	breaksw
case Linux*:
	breaksw
default:
	exec echo ${0}: does not run on `uname -s` `uname -r`
endsw

while($#argv)
	switch ("$1")
	case -d:
		set DEBUG
#		shift
#		breaksw
# fall through to verbose
	case -v:
		set VERBOSE
		shift
		breaksw
	default:
		set DQ = '"'
		exec echo ${0}: "What do I do with $DQ$1$DQ?"
	endsw
end

set DOMAIN = `Batch/domain-master | awk '{print $1}'`
switch ("$DOMAIN")
case dcs-ilab:
case ilab:
    set QAC = /koko/var/quotas/ilab/quotaadmin.conf
    breaksw
default:
    set QAC = /usr/local/adm/etc/quotaadmin.conf
endsw

set CAT = "cat"
#if (! -r $QAC) set CAT = "/usr/local/bin/slide cat"
if (! -r $QAC) set CAT = "Sys/slido cat"

#set DEFAULT = `grep ^DEFAULT $QAC | grep -v / | sed -e 's;:; ;g' -e 's;DEFAULT;;'`
set DEFAULT = `$CAT $QAC | grep ^DEFAULT | grep -v / | sed -e 's;:; ;g' -e 's;DEFAULT;;'`
if ($#DEFAULT != 2) then
    @ NODEF = $#DEFAULT / 2
    exec echo $NODEF DEFAULT lines found in $QAC
endif

set DEFS = $DEFAULT[1]
set DEFH = $DEFAULT[2]
if ($?VERBOSE) echo Default hard quota is $DEFH
if ($DEFS > $DEFH) echo "Default soft quota ($DEFS) is greater than hard quota ($DEFH)"

#set QUOTADATA = `grep ^QUOTA: $QAC | grep -v / | sed -e 's;:; ;g' | awk '{print $2,$3,$4}' | sort`
set QUOTADATA = `$CAT $QAC | grep ^QUOTA: | grep -v / | sed -e 's;:; ;g' | awk '{print $2,$3,$4}' | sort`
while ($#QUOTADATA)
    set QWHO = $QUOTADATA[1]
    set QS = $QUOTADATA[2]
    set QH = $QUOTADATA[3]
#    echo $QWHO $QS $QH
    set MSG
    if ($QH < $QS) set MSG = "Hard quota: $QS is less than soft quota: $QS"
    if ($QS && $QS < $DEFS) then
	if ("$MSG" != "") set MSG = "$MSG;"
	set MSG = "$MSG Soft quota: $QS is less than default: $DEFS"
    endif
    if ($QH && $QH < $DEFH) then
	if ("$MSG" != "") set MSG = "$MSG;"
	set MSG = "$MSG Hard quota: $QH is less than default: $DEFH"
    endif
    if ("$MSG" != "") then
	/bin/ypmatch $QWHO passwd >& /dev/null
	if ($status) then
	    echo $QWHO does not exist in passwd yp map
	    goto skipuser
	endif
	set GPAT = `groups $QWHO | sed -e 's;.* : ;:;' -e 's; ;:|:;g'`":"
#	set QGDATA = `grep ^QUOTAGROUP: $QAC | egrep "$GPAT" | grep -v / | sed -e 's;:; ;g' | awk '{print $2,$3,$4}'`
	set QGDATA = `$CAT $QAC | grep ^QUOTAGROUP: | egrep "$GPAT" | grep -v / | sed -e 's;:; ;g' | awk '{print $2,$3,$4}'`
	if ("$QGDATA" == "") then
	    unset SUPPRESS
	    if (-e $0.exceptions) then
		sed 's;#.*;;' $0.exceptions | grep -w $QWHO > /dev/null
		if (! $status) set SUPPRESS
	    endif
	    if ($?SUPPRESS) then
		if ($?VERBOSE) echo ${QWHO}: $MSG "(suppressed)"
	    else
		echo ${QWHO}: $MSG
	    endif
	else
#	    echo ""
#	    echo QGDATA = $QGDATA
	    set QGNAMES
	    @ QGS = 0
	    @ QGH = 0
	    while ($#QGDATA)
		if ("$QGNAMES" == "") then
		    set QGNAMES = $QGDATA[1]
		else
		    set QGNAMES = "$QGNAMES,"$QGDATA[1]
		endif
		@ QGS += $QGDATA[2]
		@ QGH += $QGDATA[3]
		shift QGDATA; shift QGDATA; shift QGDATA
	    end
	    set MSG
	    if ($QS < $QGS) then
		if ("$MSG" != "") set MSG = "$MSG;"
		set MSG = "$MSG Soft quota: $QS is less than default($QGNAMES): $QGS"
	    endif
	    if ($QH < $QGH) then
		if ("$MSG" != "") set MSG = "$MSG;"
		set MSG = "$MSG Hard quota: $QH is less than default($QGNAMES): $QGH"
	    endif
#	    echo $QGNAMES $QGS $QGH
	    if ("$MSG" != "") then
		unset SUPPRESS
		if (-e $0.exceptions) then
		    sed 's;#.*;;' $0.exceptions | grep -w $QWHO > /dev/null
		    if (! $status) set SUPPRESS
		endif
		if ($?SUPPRESS) then
		    if ($?VERBOSE) echo ${QWHO}: $MSG "(suppressed)"
		else
		    echo ${QWHO}: $MSG
		endif
	    endif
#	    echo ""
	endif
    endif
skipuser:
    shift QUOTADATA; shift QUOTADATA; shift QUOTADATA
end

if ($?VERBOSE) then
    echo ""
#    grep ^QUOTAGROUP: $QAC | grep -v /
    $CAT $QAC | grep ^QUOTAGROUP: | grep -v /
endif
