#!/bin/ksh
# $Revision: 1.2.2.1 $
# QUOTA command for the Sunfiler CLI

case $1 in
'on')
	if [[ -z $2 ]]; then
		/usr/bin/echo "Usage: quota $1 <filesystem>"
		exit 1
	elif [[ ! -a $2 ]]; then
		/usr/bin/echo "The filesystem does not exist"
		exit 1
	elif [[ -z `/usr/bin/cat /etc/mnttab | /usr/bin/grep $2` ]]; then
		/usr/bin/echo "Please mount the filesystem before turning on quotas"
		exit 1
	elif [[ -a $2/quotaon ]]; then
		/usr/bin/echo "Quotas are already on for this filesystem"
		exit 1
	elif [[ ! -a $2/quotas ]]; then
		/usr/bin/touch $2/quotas
	fi
	/usr/sbin/quotaon $2
	/usr/bin/touch $2/quotaon
	;;

'off')
	if [[ -z $2 ]]; then
		/usr/bin/echo "Usage: quota $1 <filesystem>"
		exit 1
	elif [[ ! -a $2 ]]; then
		/usr/bin/echo "The filesystem does not exist"
		exit 1
	elif [[ -z `/usr/bin/cat /etc/mnttab | /usr/bin/grep $2` ]]; then
		/usr/bin/echo "The filesystem must be mounted to turn off quotas"
		exit 1
	elif [[ ! -a $2/quotas || ! -a $2/quotaon ]]; then
		/usr/bin/echo "Quotas are not on for this filesystem"
		exit 1
	else
		/usr/sbin/quotaoff $2
		/usr/bin/rm $2/quotaon
	fi
	;;

'report')
	if [[ -z $2 ]]; then
		/usr/bin/echo "Usage: quota $1 <filesystem>"
		exit 1
	elif [[ ! -a $2 ]]; then
		/usr/bin/echo "The filesystem does not exist"
		exit 1
	elif [[ -z `/usr/bin/cat /etc/mnttab | /usr/bin/grep $2` ]]; then
		/usr/bin/echo "Please mount the filesystem"
		exit 1
	elif [[ ! -a $2/quotaon ]]; then
		/usr/bin/echo "Quotas are not on for this filesystem"
		exit 1
	fi
	/usr/sbin/repquota -v $2
	;;

'clear')
	if [[ -z $2 ]]; then
		/usr/bin/echo "Usage: quota $1 <filesystem>"
		exit 1
	elif [[ ! -a $2 ]]; then
		/usr/bin/echo "The filesystem does not exist"
		exit 1
	elif [[ -z `/usr/bin/cat /etc/mnttab | /usr/bin/grep $2` ]]; then
		/usr/bin/echo "Please mount the filesystem before issuing a clear"
		exit 1
	elif [[ -a $2/quotaon ]]; then
		/usr/bin/echo "Please turn quotas off for this filesystem before issuing a clear"
		exit 1
	fi

	/usr/bin/echo "The clear will remove all quota information for this filesystem."
	/usr/bin/echo "Are you sure you want to proceed (y/n)? \c"

	done=false

	while [[ $done = false ]]; do
		done=true
		read answer
		case $answer in
		y | Y)
			if [[ -a $2/quotas ]]; then
				/usr/bin/rm $2/quotas
				/usr/bin/echo "The quota information for $2 has been cleared."
			fi
			;;
		n | N)
			/usr/bin/echo "The information will not be cleared"
			exit 1
			;;
		*)
			/usr/bin/echo "Please answer Y or N: \c"
			done=false
			;;
		esac
	done
	;;

'edit')
	if [[ -z $5 ]]; then
		/usr/bin/echo "Usage: quota $1 <username> <filesystem> <soft> <hard>"
		exit 1
	else
		if [[ ! -a $3 ]]; then
			/usr/bin/echo "The filesystem does not exist"
			exit 1
		elif [[ -z `/usr/bin/cat /etc/mnttab | /usr/bin/grep $3` ]]; then
			/usr/bin/echo "The filesystem must be mounted to edit quotas"
			exit 1
		fi

		EDITOR=/usr/rbin/utility/quota_edit
		export EDITOR

		/usr/bin/echo $2 > /tmp/quota.$$
		/usr/bin/echo $3 >> /tmp/quota.$$
		/usr/bin/echo $4 >> /tmp/quota.$$
		/usr/bin/echo $5 >> /tmp/quota.$$

		/usr/sbin/edquota $2
		/usr/sbin/quotacheck $3
	fi
	;;

*)
	/usr/bin/echo "Usage: quota { on | off | report | clear | edit }"
	exit 1
	;;
esac
