#!/bin/bash
#
#	Acknowledgement: The functions (Functions.lib) used in this script are copyrights of
#			 Ensim Corporation. 
#
#	Script Author: Ziaur Rahman (zia at zort dot org)
#
#	"Plagiarism is copying from one source; research is copying from two or more." :)
#
#	Version:	1.1
# 
#	History:
#	DD-MM-YY	Author	What?
#
#	06-05-03	Z	Created.
#
#	14-06-03	Z	Modified Functions.lib and this file drastically to give multiple
#				partition support.
#				(Thanks to Young Yuen <young_yuen at yahoo dot com> for the idea.)
#
#	15-06-03	Z	Modified the script to give users choice to enable quota in any
#				partition interactively.
#				Finxed a bug in the quotacheck and quotaon syntax.
#

. Functions.lib
. strings.lib

number=1
count=0
qdone=0
skip_device=0

echo -e "\nThis script has been tested in the following OSes:"
echo -e "\t1. Redhat Linux 6.2 - kernel: 2.2.14-5.0"
echo -e "\t2. Redhat Linux 7.2 - Kernel: 2.4.7-10"
echo -e "\t3. Redhat Linux 7.3 - Kernel: 2.4.7-10"
echo -e "\t4. Redhat Linux Enterprise Server 2.1 - Kernel: 2.4.9-e.24"
echo
echo -e "\nChecklist of Pre-requisites for this script to work:"
echo -e "\t1. Kernel must be enabled for Quota Support:"
echo -e "\t\ta) cd /usr/src/linux-<version of the kernel>"
echo -e "\t\tb) make menuconfig"
echo -e "\t\tc) Goto: File systems  ---> "
echo -e "\t\td) Select: [*] Quota support "
echo -e "\t\te) Exit and save the new config."
echo -e "\t\tf) Recompile the kernel and install it."
echo -e "\t-- Please consult making custom kernel documentation."
echo -e "\t-- Or, you can install precompiled kernel that supports Quota."
echo -e "\t-- Usually, almost all the recent Redhat vanilla kernels has Quota support by default."
echo -e "\t2. Quota Tools must be installed."
echo -e "\t3. Bash 2.0 or above."
echo -e "\nLooking for mount points in the hard disks..."
get_partition;
if [ $? -eq 0 ]; then
	echo -e "\t####################### Warning #######################"
	echo -e "\t#                                                     #"
	echo -e "\t# enable-quota.sh script will prompt you to activate  #"
	echo -e "\t# quota on all drives and partitions in your system.  #"
	echo -e "\t# If quota is already installed in one of your drive/ #"
	echo -e "\t# partitions, then it will prompt you to reinitialize #"
	echo -e "\t# the quota in that partition.                        #"
	echo -e "\t# Don't panic! This will not destroy any quota        #"
	echo -e "\t# information at all. In fact, its a good idea to run #"
	echo -e "\t# quotacheck weekly/monthly to make sure consistant   #"
	echo -e "\t# quota information is available from the quota files.#"
	echo -e "\t# Please refer to the following link for more         #"
	echo -e "\t# information:                                        #"
	echo -e "\t# http://www.tldp.org/HOWTO/mini/Quota.html           #"
	echo -e "\t#                                                     #"
	echo -e "\t####################### Warning #######################"
	echo -e "The following partitions are found in your system:\n"
	for i in "${partitions[@]}"
	do
		check_quotas $i ${devices[$count]};
		ret=$?
		if [ $ret -eq 0 ]; then
        		echo -e "$number. ${devices[$count]}\t$i\tQuota enabled\n"
			skip_devices[$count]=${devices[$count]}
			skip_device=1
        		log_actions "check_quotas: Quota already enabled for ${devices[$count]}"
		elif [ $ret -ne 0 ]; then
			echo -e "$number. ${devices[$count]}\t$i\tQuota NOT enabled\n"
        		log_actions "check_quotas: Quota is NOT enabled for $device"
		fi
		let "count = $count + 1"
		let "number = $number + 1"
	done

	if [ $number -eq 1 ] && [ $skip_device -eq 1 ]; then
		echo -e "\n\n\tYou have only one device (${devices[$count]}) to enable Quota for,"
		echo -e "\tand Quota is already enabled in that device/partition."
		echo -e "\n\tYou can try running 'quotacheck -mugvf ${devices[$count]}' if you think your"
		echo -e "\tquota files (aquota.user/quota.user and auota.group/quota.group) are corrupted.."
		exit;
	fi

	get_user_response 'y' "$ENABLE_QUOTA_PROMPT: "
        if [ "$usr_resp" = 'n' ]; then
        	echo -e "\n$ENABLE_QUOTA_WARNING\n"
		echo -e "Anyways, I am exiting..."
               	exit
        else
		echo -e "This may take a loooong time depending on your users' data and hard disk size..."
		echo -e "So, grab a chair and relax while I do the job for you.... :)\n"
               	log_actions "$RUNNING_STR enable_quotas"
		count=0
		for j in "${partitions[@]}"
		do
			dcount=0
			scount=0
			device_there=0
			for d in "${devices[@]}"
			do
				for s in "${skip_devices[@]}"
				do
					if [ "${devices[$dcount]}" = "${skip_devices[$sount]}" ]; then
						device_there=1
					fi
					let "scount = $scount + 1"
				done
				let "dcount = $dcount + 1"
			done
			still_do=0
			if [ $device_there -eq 1 ] && [ ${#devices[$count]} -ne $count ]; then
				echo -e "\n=====> Quota is already enabled in ${devices[$count]} ($j) <====="
				get_user_response 'y' "Do you still want to re-initialise Quota in ${devices[$count]} [yes/no]? "
				if [ "$usr_resp" = 'n' ]; then
					let "count = $count + 1"
					continue
				else
					echo -e "Turning off quota in ${devices[$count]}"
					/sbin/quotaoff -ugv $j
				fi
			else
				get_user_response 'y' "Do you want to enable/activate Quota in ${devices[$count]} [yes/no]? "
				if [ "$usr_resp" = 'n' ]; then
                                        let "count = $count + 1"
                                        continue
                                else
					echo
                                fi
			fi
        		echo -e "$RUNNING_ENABLE_QUOTA $j...\n"
               		enable_quotas "${devices[$count]}" "$j"
               		if [ $? -eq 0 ]; then
               			echo -e "enable_quotas: Quota has been successfully enabled in ${devices[$count]} for $j\n"
				qdone=1
               			log_actions "enable_quotas: SUCCESS in ${devices[$count]} ( $j )!"
               		else
               			echo -e "enable_quotas: Falied to activate quota in ${devices[$count]} ( $j )!\n"
               			log_actions "enable_quotas: FAILED for ${devices[$count]} ( $j )!"
                      		log_errors "$ENABLE_QUOTA_ERROR"
				echo -e "Please see /tmp/enablequota.err and /tmp/enablequota.log for more info.\n"
               		fi
			let "count = $count + 1"
		done
		if [ $qdone -eq 1 ]; then
			echo -e "Now, you can use the script 'quotadm' to see reports on your users' quota"
			echo -e "and if you want you can send them warning emails on threshold."
			echo -e "You can download the script at: http://mzrahman.com/codes/quotadm/"
			echo -e "Thanks. - Ziaur Rahman (zia at zort dot org)."
		fi
        fi
else
        echo -e "Could not detect any partition in your system!\n"
        echo
fi
