bash << 'EOF'
#!/bin/bash

log_rotate()
{
if [ -n "$(find "$1" -prune -size +10485760c)" ];
then
	if [ -f ${1}.1 ];
	then 
		mv ${1}.1 ${1}.2
	fi
	if [ -f $1 ];
	then
		mv $1 ${1}.1
	fi
fi
}

sdh_mdh()
{
for doc_root in `grep -e "$1" /etc/userdatadomains | awk -F "==" '{print $5}' | sort | uniq`;
do 
if [ -d $doc_root ];
then
user=`echo $doc_root | cut -d'/' -f3`
if [ ! -f ${doc_root}/.htaccess ];
then
echo -e "\nErrorDocument 403     /cgi-sys/403_def.html"  >> ${doc_root}/.htaccess
echo -e "\nErrorDocument 404     /cgi-sys/404_def.html"  >> ${doc_root}/.htaccess
echo -e "\nErrorDocument 500     /cgi-sys/500_def.html"  >> ${doc_root}/.htaccess
echo -e "\nErrorDocument 502     /cgi-sys/502_def.html"  >> ${doc_root}/.htaccess
echo -e "\nErrorDocument 503     /cgi-sys/503_def.html"  >> ${doc_root}/.htaccess
echo -e "\nErrorDocument 504     /cgi-sys/504_def.html"  >> ${doc_root}/.htaccess

else

cp -pr ${doc_root}/.htaccess ${doc_root}/.htaccess_bkp`date +%F`

if [ -z "$(grep -i "ErrorDocument 403" ${doc_root}/.htaccess)" ]; then echo -e "\nErrorDocument 403     /cgi-sys/403_def.html" >> ${doc_root}/.htaccess; else echo "Error document 403 is already there in ${doc_root}/.htaccess"; fi
if [ -z "$(grep -i "ErrorDocument 404" ${doc_root}/.htaccess)" ]; then echo -e "\nErrorDocument 404     /cgi-sys/404_def.html" >> ${doc_root}/.htaccess; else echo "Error document 404 is already there in ${doc_root}/.htaccess"; fi
if [ -z "$(grep -i "ErrorDocument 500" ${doc_root}/.htaccess)" ]; then echo -e "\nErrorDocument 500     /cgi-sys/500_def.html" >> ${doc_root}/.htaccess; else echo "Error Document 500 is already there in ${doc_root}/.htaccess"; fi
if [ -z "$(grep -i "ErrorDocument 502" ${doc_root}/.htaccess)" ]; then echo -e "\nErrorDocument 502     /cgi-sys/502_def.html" >> ${doc_root}/.htaccess; else echo "Error Document 502 is already there in ${doc_root}/.htaccess"; fi
if [ -z "$(grep -i "ErrorDocument 503" ${doc_root}/.htaccess)" ]; then echo -e "\nErrorDocument 503     /cgi-sys/503_def.html" >> ${doc_root}/.htaccess; else echo "Error Document 503 is already there in ${doc_root}/.htaccess"; fi
if [ -z "$(grep -i "ErrorDocument 504" ${doc_root}/.htaccess)" ]; then echo -e "\nErrorDocument 504     /cgi-sys/504_def.html" >> ${doc_root}/.htaccess; else echo "Error Document 504 is already there in ${doc_root}/.htaccess"; fi

fi

chown $user. ${doc_root}/.htaccess

echo -e "$(date) Added Error document redirection rule to ${doc_root}/.htaccess" >> /var/log/monetization.log
echo -e "Added Error document redirection rule to ${doc_root}/.htaccess"
else
echo -e "$doc_root does not exists"
fi
done
echo -e "\e[1;32mDone!!\e[0m"
}

pkg=package_placeholder
log_file="/var/log/monetization.log"
if [ -f $log_file ];
then
	log_rotate "$log_file"
fi

case $pkg in
'sdh' | 'mdh')
echo -e "$(date) Entered domain_placeholder from SDH/MDH package for changing custom error page" >> /var/log/monetization.log 
cp_user=`/scripts/whoowns domain_placeholder`;
	if [ -z $cp_user ];
        then
                echo -e "Invalid Domain"
                echo -e "$(date) invalid domain name domain_placeholder" >> /var/log/monetization.log
                exit 1
        else
        sdh_mdh ": $cp_user=="
        fi
;;

'rh')
choice=choice_placeholder
	if [ $choice == "all" ] || [ $choice == "ALL" ];
	then
		echo -e "$(date) Entered the Parent Domain domain_placeholder from RH package for changing custom error page for the entire package" >> /var/log/monetization.log
		owner=`/scripts/whoowns domain_placeholder`
		if [ -z $owner ];
		then
			echo -e "Invalid Domain"
			echo -e "$(date) invalid domain name domain_placeholder" >> /var/log/monetization.log
			exit 1
		else
			if [ -z "$(grep -e "^${owner}:" /var/cpanel/resellers)" ];
			then
				echo -e "$(date) domain_placeholder is not a reseller parent domain" >> /var/log/monetization.log
				echo -e "domain_placeholder is not a reseller parent domain"
			else
				sdh_mdh "==$owner=="
			fi
		fi

elif [ $choice == "single" ] || [ $choice == "SINGLE" ];
then
	echo -e "$(date) Entered the Domain domain_placeholder from RH package for changing custom error page for the cpanel account alone" >> /var/log/monetization.log
	cp_user=`/scripts/whoowns domain_placeholder`;
	if [ -z $cp_user ];
	then
		echo -e "Invalid Domain"
		echo -e "$(date) invalid domain name domain_placeholder" >> /var/log/monetization.log
		exit 1
	else
		sdh_mdh ": $cp_user=="
	fi
else
echo -e "Wrong Choice :("
fi
;;

*)
echo -e "Wrong package selection :("
exit 1
;;
esac
EOF