#
# Copyright (c) Ensim Corporation, 2001   All Rights Reserved.
#
# This software is furnished under a license and may be used and copied
# only  in  accordance  with  the  terms  of such  license and with the
# inclusion of the above copyright notice. This software or any other
# copies thereof may not be provided or otherwise made available to any
# other person. No title to and ownership of the software is hereby
# transferred.

# These functions are extracted from Ensim's Installation Scripts in 
# accordance with the above copyright statement. - Ziaur Rahman

# Some modifications were made to suit the independent nature of the
# enablequota.sh script. - Ziaur Rahman

# "Plagiarism is copying from one source; research is copying from two or more." :)


############################################
#       Get device name where /home is located
############################################
get_partition()
{
        local device_list=`df`
        device_list=`echo "$device_list" | grep "^/" |grep -v "/boot" |grep -v "/dev/shm" |grep -v "/proc"`
        local i=''
	local counter=0
        local mount_found=0
        local slash_device=''
        local slash_free=''

OLDIFS="$IFS"
IFS='
'
        for i in $device_list
        do
                local tmp=`echo "$i" | sed -e 's/  */ /g'`
                device=`echo $tmp | cut -d" " -f1`
                free_space=`echo $tmp | cut -d" " -f4`
                mount_point=`echo $tmp | cut -d" " -f6`

                if [ "$mount_point" = '' ] || [ "$free_space" = '' ]; then
                        log_errors "$ERR_DEVICE_INFO: $device"
                        continue;
		else
			partitions[$counter]=$mount_point
			devices[$counter]=$device
		fi
		let "counter = $counter + 1"
        done
IFS="$OLDIFS"

        log_actions "$FOUND_HOME $device"
        return 0
}

############################################
#       Check filesystem quotas is enabled
############################################
check_quotas()
{
        #qcheck blockdevice path-to-quota.user
        # get_partition must be called first to set device
	local parti=$1
	local devi=$2
        local usr_msg=''
        local grp_msg=''
        local usr_ret=''
        local grp_ret=''
	local qtrue=0

       	usr_file=`find $parti -maxdepth 1 -type f -name aquota.user`
       	grp_file=`find $parti -maxdepth 1 -type f -name aquota.group`

	if [ "$usr_file" = '' ] || [ "$grp_file" = '' ]; then
		usr_file=`find $parti -maxdepth 1 -type f -name quota.user`
       		grp_file=`find $parti -maxdepth 1 -type f -name quota.group`
		if [ "$usr_file" = '' ] || [ "$grp_file" = '' ]; then
			qtrue=0
		else
			qtrue=1
		fi
	else
		qtrue=1
	fi

	if [ $qtrue -eq 1 ]; then
       		usr_msg=`./qcheck "$devi" "$usr_file" USR 2>&1`
       		usr_ret=$?

       		grp_msg=`./qcheck "$devi" "$grp_file" GRP 2>&1`
       		grp_ret=$?

		#echo -e "DEBUG: $parti\t $usr_file\t $usr_ret\n";
        	#echo -e "DEBUG: $parti\t $grp_file\t $grp_ret\n";
        	if [ $usr_ret -ne 0 ] || [ $grp_ret -ne 0 ]; then
               		log_errors "$NO_QUOTAS: $usr_msg"
               		log_errors "$grp_msg"
               		return 1
        	else
               		return 0
        	fi
	else
		return 1
	fi
}

############################################
#       Enable quotas on / partition
############################################
enable_quotas()
{
        local home_device=$1
        local mntdir=$2
        if [ x"$home_device" = x'' ]; then
                echo "$INSTALLER_ERROR"
                return 1
        fi

        find_cmd '/sbin/quotacheck' && find_cmd '/sbin/quotaon'
        if [ $? -ne 0 ]; then
                return 1
        fi

        local tmp_fstab="/tmp/fstab.$$"
        local sys_cmd=''

	grep LABEL=$mntdir /etc/fstab |grep usrquota > /dev/null 2>&1 \
                && grep LABEL=$mntdir /etc/fstab |grep grpquota > /dev/null 2>&1
        if [ $? -eq 1 ]; then
                /bin/awk '{{ if ($1 ~ device || $1 ~ mntpnt || $2 ~ mdir) { print $1 "\t" $2 "\t"  $3 "\t" "usrquota,grpquota," $4, $5, $6, $7, $8 } else { print $0 }}}' \
                device="$home_device" mntpnt="LABEL=$mntdir\$" mdir="$mntdir\$" \
                /etc/fstab > "$tmp_fstab"
                if [ $? -ne 0 ]; then
                        rm -f "$tmp_fstab"
                        log_errors "$ENABLE_QUOTA_MODIFY_FSTAB_ERROR"
                        return 1
                fi

                grep "grpquota" "$tmp_fstab" >/dev/null 2>&1
                if [ $? -eq 0 ]; then
                        sys_cmd="cp -f /etc/fstab /tmp/fstab.backup"
                        $sys_cmd;
                        if [ $? -ne 0 ]; then
                                log_errors "$SYS_CMD_ERROR: $sys_cmd"
                                return 1
                        fi
                        log_actions "$FSTAB_BACKUP"
                        sys_cmd="mv -f $tmp_fstab /etc/fstab"
                        $sys_cmd;
                        if [ $? -ne 0 ]; then
                                log_errors "$SYS_CMD_ERROR: $sys_cmd"
                                return 1
                        fi
                else
                        rm -f "$tmp_fstab"
                        log_errors "$ENABLE_QUOTA_MODIFY_FSTAB_ERROR"
                        return 1
                fi
        fi

        /bin/mount -o remount $mntdir >/dev/null 2>&1
        /sbin/quotacheck -mugcvf $home_device && /sbin/quotaon -v $mntdir 
        if [ $? -ne 0 ]; then
                log_errors "$SYS_CMD_ERROR: /sbin/quotacheck && /sbin/quotaon"
                return 1
        fi

        return 0

}


############################################
#       Write actions to log and display
############################################
log_actions()
{
        local act_msg="$1"
	local log_dir="/tmp"
	local install_log="enablequota.log"
        if [ x"$act_msg" = x'' ]; then
                return 1
        fi

        act_msg=`strip_ws "$act_msg"`
        if [ $? -ne 0 ]; then
                return 1
        fi

        echo -n `date +'%D %H:%M:%S'`' : ' >> "$log_dir/$install_log"
        echo -e "$act_msg" >> "$log_dir/$install_log"

        return 0
}


############################################
#       Strip out whitespace at end of msgs
############################################
strip_ws()
{
        local msg="$1"
        local output=''

        output=`echo "$msg" | sed -e "s/\([[:print:]]\)[[:space:]]*$/\1/"`
        if [ $? -ne 0 ]; then
                return 1
        else
                echo "$output"
                return 0
        fi
}

############################################
#       Query user response
############################################
get_user_response()
{
        usr_resp=$1
        local q_done='n'
        while [ "$q_done" = 'n' ]
        do
                echo -n "$2 [$1] "
                read usr_resp
                case "$usr_resp" in
                "")
                        usr_resp="$1"
                        q_done='y'
                        break
                        ;;
                Y*|y*)
                        usr_resp='y'
                        q_done='y'
                        break
                        ;;
                N*|n*)
                        usr_resp='n'
                        q_done='y'
                        break
                        ;;
                *)
                        q_done='n'
                        ;;
                esac
        done
}

############################################
#       Write errors to err_log and display
############################################
log_errors()
{
        local err_msg="$1"
	local log_dir="/tmp"
        local err_log="enablequota.err"
        if [ x"$err_msg" = x'' ]; then
                return 1
        fi

        err_msg=`strip_ws "$err_msg"`
        if [ $? -ne 0 ]; then
                return 1
        fi

        echo -n `date +'%D %H:%M:%S'`' : ' >> "$log_dir/$err_log"
        echo -e "$err_msg" >> "$log_dir/$err_log"

        return 0
}

#############################################
#       Find system command
#############################################
find_cmd()
{
        local cmd="$1"

        which $cmd >/dev/null 2>&1
        if [ $? -ne 0 ]; then
                log_errors "$SYS_CMD_MISSING: $cmd"
                return 1
        else
                return 0
        fi
}
