#!/bin/bash

# VARS
ACTION=$1
Green='\e[1;32m'
Red='\e[0;31m'
NOCOLOR='\e[0m'

function ask {
	# Author: Dave James Miller
	# Author URI: https://gist.github.com/davejamesmiller/1965569
	while true; do
		if [ "${2:-}" = "Y" ]; then
			prompt="Y/n"
			default=Y
		elif [ "${2:-}" = "N" ]; then
			prompt="y/N"
			default=N
		else
			prompt="y/n"
			default=
		fi
		read -p "$1 [$prompt] " REPLY
		if [ -z "$REPLY" ]; then
			REPLY=$default
		fi
		case "$REPLY" in
			Y*|y*) return 0 ;;
			N*|n*) return 1 ;;
		esac
	done
}

usage () {
cat << EOF
   #########################
  #  CpCleaner Installer v1.0.0            #
 #  Generator : GK~root team           #
########################

Usage: bash cpcleaner-installer [ACTION]

Actions:
  -install       CpCleaner installation
  -update        Update CpCleaner
  -uninstall     Uninstall CpCleaner

EOF
exit 0
}

freshinstall () {	
	if [ -e "/usr/local/cpanel/scripts/install_plugin" ]; then		
		if [ ! -f "/usr/local/cpanel/base/3rdparty/CpCleaner/cpplug/cpcleaner.tar.gz" ]; then
			echo ""
			echo -e "Starting CpCleaner installation, ....... ${Green}Please wait...${NOCOLOR}"
			# cPanel files
			yes | cp -r CpCleaner /usr/local/cpanel/base/3rdparty/ >/dev/null 2>&1
			if [ -f "/usr/local/cpanel/base/3rdparty/CpCleaner/index.live.php" ]; then
				echo -e "Copy 3rdparty files ....... ${Green}Complete${NOCOLOR}"
			else
				echo -e "Copy 3rdparty files is not completed ....... ${Red}Failed${NOCOLOR}"
				exit 1
			fi
			# Themes install & Register cPanel apps
			/usr/local/cpanel/scripts/install_plugin /usr/local/cpanel/base/3rdparty/CpCleaner/cpplug/cpcleaner.tar.gz
			/usr/local/cpanel/scripts/install_plugin /usr/local/cpanel/base/3rdparty/CpCleaner/cpplug/cpcleaner.tar.gz --theme x3
			/usr/local/cpanel/bin/rebuild_sprites
			/usr/local/cpanel/bin/register_appconfig /usr/local/cpanel/base/3rdparty/CpCleaner/cpplug/cpcleaner.conf
			echo ""
			echo "##########################################################"
			echo "################ Installation complete ###################"
			echo "##########################################################"
			echo ""
			echo "cPanel users will now have access to CpCleaner from"
			echo "The 'Files' icons group in cPanel. Enjoy!"
			echo ""
			exit 0
		else
			echo -e "CpCleaner already installed on your server ....... ${Red}Failed${NOCOLOR}"
			echo "Nothing to do!"
			echo ""
			exit 1
		fi
	else
		echo -e "CpCleaner can not be installed ....... ${Red}Failed${NOCOLOR}"
		echo "You need to update your cPanel/WHM to v11.50+ first"
		echo "Go to 'WHM >> cPanel >> Upgrade to Latest Version' and update your cPanel!"
		exit 1
	fi
}

remove () {		
	if [ -f "/usr/local/cpanel/base/3rdparty/CpCleaner/cpplug/cpcleaner.tar.gz" ]; then
		# Themes install & Register cPanel apps
		/usr/local/cpanel/scripts/uninstall_plugin /usr/local/cpanel/base/3rdparty/CpCleaner/cpplug/cpcleaner.tar.gz
		/usr/local/cpanel/scripts/uninstall_plugin /usr/local/cpanel/base/3rdparty/CpCleaner/cpplug/cpcleaner.tar.gz --theme x3
		/usr/local/cpanel/bin/rebuild_sprites
		/usr/local/cpanel/bin/unregister_appconfig /usr/local/cpanel/base/3rdparty/CpCleaner/cpplug/cpcleaner.conf
		# Remove cPanel files
		rm -rf /usr/local/cpanel/base/3rdparty/CpCleaner >/dev/null 2>&1
		if [ -f "/usr/local/cpanel/base/3rdparty/CpCleaner/index.live.php" ]; then
			echo -e "Remove '/usr/local/cpanel/base/3rdparty/CpCleaner' folder is not completed ....... ${Red}Failed${NOCOLOR}"
		else
			echo -e "Remove CpCleaner 3rdparty folder ....... ${Green}Complete${NOCOLOR}"
		fi
		echo ""
		echo -e "Uninstalling CpCleaner ....... ${Green}Complete${NOCOLOR}"
		echo ""
	else
		echo ""
		echo -e "CpCleaner not installed on your server ....... ${Red}Failed${NOCOLOR}"
		if ask "Do you want to install CpCleaner?"; then
			freshinstall
		else
			echo "Nothing to do!"
			echo ""
		fi
	fi
}

update () {
	echo ""
	if [ -f "/usr/local/cpanel/base/3rdparty/CpCleaner/index.live.php" ]; then
		yes | cp -r CpCleaner /usr/local/cpanel/base/3rdparty/ >/dev/null 2>&1
		echo ""
		echo -e "Update CpCleaner ....... ${Green}Complete${NOCOLOR}"
		echo ""	
	else
		echo ""
		echo -e "CpCleaner not installed on your server ....... ${Red}Failed${NOCOLOR}"
		if ask "Do you want to install CpCleaner?"; then
			freshinstall
		else
			echo "Nothing to do!"
			echo ""
		fi
	fi
	echo ""
}

[ -z $ACTION ] && usage && exit 1

case $ACTION in
	"-install")         freshinstall;;
	"-uninstall")       remove;;
	"-update")          update;;
	*)                  usage;;
esac

exit 0
