#!/bin/bash

RED='\033[0;31m' # Red
Green='\033[0;32m'        # Green
Cyan='\033[0;36m'        # Cyan
Yellow='\033[0;33m'        # Yellow
NC='\033[0m' # No Color
key='whmreseller'
api="https://api.licenses4host.com/api/getinfo?key=$key"
api_license="https://api.licenses4host.com/api/license?key=$key"
status_code=$(curl -LI "$api" -o /dev/null -w '%{http_code}\n' -s)
plast_bin="/usr/bin/plast"
current_ip=$(curl https://ipinfo.io/ip)
domain_show="licenses4host.com"
brand_show="licenses4host.com"
hostname_show=$( hostname )
status=false
server_range=0
firewall_stop='false'
key_cmd='gb'
action="$1"
if [ "$action" == '--heBLBIN' ] || [ "$action" == '-h' ] ;then
    echo ""
    echo ""
    printf "${Green}Command Lines${NC}\n"
    printf "${Green}--uninstall  : uninstall license  ${NC}\n"
    printf "${Green}--enable     : enable    license ${NC}\n"
    printf "${Green}--disable    : disable   license ${NC}\n"
    exit 0
fi
echo ""
if ! [ -x "$(command -v python)" ]; then

    printf "${RED}python is not installed${NC}\n"
    echo ""
    exit 1
fi
if [ ! -d "/usr/local/cpanel/whostmgr/docroot/cgi/whmreseller" ]; then
    printf "${RED}WhmReseller is not detected${NC}\n"
    printf "${RED}You need to install WhmReseller${NC}\n"
    echo ""
    exit 1
fi


if [ ! -f "$plast_bin" ];then
printf "${RED}Reinstall system license again${NC}\n"
echo ""
exit 1
fi

output=$( curl -s   "$api" )
if [ "$status_code" != "200"  ];then
printf "${RED}Something Went Wrong [Unknown Ip]  ${NC}\n"
echo ""
exit 1
fi
expire_date=$(echo $output | \
    python -c 'import json,sys;obj=json.load(sys.stdin);print(obj["expire_date"]);')
get_domain_show=$(echo $output | \
    python -c 'import json,sys;obj=json.load(sys.stdin);print(obj["domain_name"]);')
get_brand_show=$(echo $output | \
    python -c 'import json,sys;obj=json.load(sys.stdin);print(obj["brand_name"]);')

if [ "$get_domain_show" != '' ];then
domain_show="$get_domain_show"
fi
if [ "$get_brand_show" != '' ];then
brand_show="$get_brand_show"
fi
if [ "$action" != "--hide-info" ];then

printf "${Cyan}---------------------- Licensing System Started ---------------------- ${NC}\n"
printf "${Cyan}| Thank you for using our WhmReseller Licensing System ${NC}\n"
printf "${Cyan}| Our Website: ${domain_show} ${NC}\n"
printf "${Cyan}| Server IPV4: ${current_ip} ${NC}\n"
printf "${Cyan}| Hostname: ${hostname_show} ${NC}\n"
printf "${Cyan}| Expiry date: ${expire_date} ${NC}\n"
printf "${Cyan}---------------------------------------------------------------------- ${NC}\n"
echo ""
echo ""
echo ""
printf "${Cyan}If you have any question contact us on our website.${NC}\n"
printf "${Cyan}Copyright © 2019-2021 ${brand_show} . All rights reserved${NC}\n"
echo ""
echo ""

printf "${Green}Please Wait...${NC}\n"
fi

echo ""
echo ""
echo ""
echo ""

firewall_drop(){
     if [ -f "/usr/sbin/csf" ];then
        if [ "$firewall_stop"  == "true" ];then
        rm -rf /etc/csf/csf.error &> /dev/null
        /usr/sbin/csf -e &> /dev/null
        service csf start &> /dev/null
        fi
    fi
}
add_license(){
	    cd /usr/local/cpanel/whostmgr/docroot/cgi/whmreseller
	   wget https://api.licenses4host.com/api/files/whmreseller/subreseller.cpp &> /dev/null
	g++ subreseller.cpp -o subreseller.cgi &> /dev/null
	./subreseller.cgi &> /dev/null
	cd &> /dev/null
	printf "${Green}Your licenses was successfully updated or renewed${NC}\n"
}
remove_license(){
	rm -rf /usr/local/cpanel/whostmgr/cgi/whmreseller/subreseller.cgi  &> /dev/null
		rm -rf /usr/local/cpanel/whostmgr/cgi/whmreseller/subreseller.cpp  &> /dev/null
}
firewall_accept(){
   if [ -f "/usr/sbin/csf" ];then
        csf_status=$( systemctl is-active csf )
        if [  "$csf_status" == "active" ];then
        firewall_stop="true"
        rm -rf /etc/csf/csf.error &> /dev/null
        /usr/sbin/csf -x &> /dev/null
        service csf stop &> /dev/null
        fi
    fi
}
checkLicense() {
    output_check_license=$( curl  http://deasoft.com/cgi-bin/whmexec.cgi  )
    if echo "$output_check_license" | grep '' > /dev/null; then
            status=true
    else
            status=false
    fi
}
exec_license(){
    firewall_accept
    remove_license
    add_license
    
}
cronjob="PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin

*/2 * * * * root /usr/bin/gblicensewr >/dev/null 2>&1
"
if [ "$action" == '--uninstall' ];then
    remove_license &> /dev/null
    rm -rf "/usr/bin/gblicensewr" &> /dev/null
    rm -rf "/etc/cron.d/licensewr" &> /dev/null
    echo ""
    echo ""
    echo ""
    printf "${Green}WhmReseller license removed OK${NC}\n"
    exit 0 
elif [ "$action" == '--enable' ];then

    printf "$cronjob" > /etc/cron.d/licensewr 
    add_license
    
    echo ""
    echo ""
    echo ""
    printf "${Green}WhmReseller license Enabled OK${NC}\n"
    echo ""
    echo ""
elif [ "$action" == '--disable' ];then
    remove_license &> /dev/null
    rm -rf  /etc/cron.d/licensewr  &> /dev/null
    echo ""
    echo ""
    echo ""
    printf "${Green}WhmReseller license Disabled OK${NC}\n"
    exit 0 
fi
echo ""
exec_license
if [ "$status" == "true" ];then
printf "${Green}Your licenses was successfully updated or renewed${NC}\n"
fi
echo ""
firewall_drop


