#!/bin/tcsh -f
# JLdL 21Jan11.
#
# Copyright (C) 2006-2011 by Jorge L. deLyra <delyra@fma.if.usp.br>.
# This program may be copied and/or distributed freely. See the
# _ terms and conditions in /usr/share/doc/<package>/copyright.
#
# This program resets the binary quota files of all quota-enabled
# _ filesystems, for both user and group quotas, based on the
# _ data in the corresponding text files; it does this by
# _ calling the program fix-quotas.
#
# Record the name this script was called with.
set name = `basename $0`
#
# Initialize variables for the configuration file.
set conflag = 0
#
# Initialize a variable for passing options to the programs called
# _ from within this one.
set passopt = ""
#
# Process the command-line arguments.
foreach cla ( $* )
    #
    # Detect options.
    if ( "`echo -n $cla | cut -c 1`" == "-" ) then
	#
	# If we got here with conflag up, there is an error.
	if ( $conflag == 1 ) then
	    echo "${name}: ERROR: option -C requires an argument"
	    exit 1
	endif
	#
	# Now process the options.
	switch ( $cla )
	case "-h":
	case "--help":
	    #
	    # Print a usage message.
	    echo "usage: $name [-C <config>] [-q]"
	    echo "       -C: use alternate configuration file <config>"
	    echo "       -q: run quietly, no progress reporting"
	    exit 0
	    breaksw
	case "-C":
	case "--Config-file":
	    #
	    # Pass the option.
	    set passopt = ( $passopt $cla )
	    #
	    # Raise the flag.
	    set conflag = 1
	    breaksw
	case "-q":
	case "--quiet":
	    #
	    # Pass the option.
	    set passopt = ( $passopt $cla )
	    breaksw
	default:
	    #
	    # Print an error message.
	    echo "${name}: ERROR: unknown option $cla"
	    exit 1
	    breaksw
	endsw
    #
    # Process non-option arguments.
    else
	#
	# Get the arguments of options.
	if ( $conflag == 1 ) then
	    #
	    # Pass the argument of the option.
	    set passopt = ( $passopt $cla )
	    #
	    # Lower the flag.
	    set conflag = 0
	#
	# This script takes no arguments.
	else
	    #
	    # Print an error message.
	    echo "${name}: ERROR: takes no arguments"
	    exit 1
	endif
    endif
end
#
# If we got here with conflag up, there is an error.
if ( $conflag == 1 ) then
    echo "${name}: ERROR: option -C requires an argument"
    exit 1
endif
#
# Get from the system all the filesystems with user quotas enabled.
set fss = `mount | egrep 'usrquota|usrjquota' | tr -s " " | cut -d " " -f 3`
#
# Loop over those filesystems.
foreach fs ( $fss )
    #
    # Call the program fix-quotas, passing the options.
    fix-quotas $passopt -u $fs
end
#
# Get from the system all the filesystems with group quotas enabled.
set fss = `mount | egrep 'grpquota|grpjquota' | tr -s " " | cut -d " " -f 3`
#
# Loop over those filesystems.
foreach fs ( $fss )
    #
    # Call the program fix-quotas, passing the options.
    fix-quotas $passopt -g $fs
end
