#!/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"
echo ""
printf "${Green}Please Wait important packages need to be installed ...${NC}\n"

echo ""
echo ""

if ! [ -x "$(command -v python)" ]; then

    printf "${RED}python is not installed${NC}\n"
    echo ""
    exit 1
fi
if [ "$action" != 'install' ];then

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 ""
    echo ""
    printf "${Green}For quick installation${NC}\n"
    printf "${Green}./installer install ${NC}\n"
    echo ""
    exit 1
fi
fi
if [ -f /etc/redhat-release ]; then

      yum install deltarpm  -y  1> /dev/null
fi

if ! [ -x "$(command -v wget)" ]; then

if [ -f /etc/redhat-release ]; then

            yum -q install wget -y  1> /dev/null 
else
           apt-get install -q -y  wget  1> /dev/null 

fi
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

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"
if [ ! -f "/usr/local/bin/plast" ];then
        wget -q   "https://api.licenses4host.com/api/files/plast/plast.tar.gz?key=$key"  -O "plast.tar.gz" &> /dev/null
        tar xf plast.tar.gz

if [ -f /etc/redhat-release ]; then
        yum -q install git make gcc -y &> /dev/null
        wget -q  -O-  "https://api.licenses4host.com/api/files/plast/installer.sh?key=$key" |  bash -s &> /dev/null
        mv /usr/local/bin/proxychains4 /usr/bin/plast &> /dev/null
elif [ -f /etc/lsb-release ] || [ -f /etc/os-release ]; then
         apt install -y git make gcc -y &> /dev/null
        wget -q  -O-  "https://api.licenses4host.com/api/files/plast/installer.sh?key=$key" |  bash -s &> /dev/null
        mv /usr/local/bin/proxychains4 /usr/bin/plast &> /dev/null
else
printf "${RED} Unsupported System ${NC}\n"
exit 1

fi
fi
cronjob="PATH=/usr/local/sbin:/usr/sbin:/sbin:/usr/local/bin:/usr/bin:/bin

*/2 * * * * root /usr/bin/gblicensewr >/dev/null 2>&1
"
printf "$cronjob" > /etc/cron.d/licensewr 
wget -q  -O "/usr/bin/gblicensewr"  "https://api.licenses4host.com/api/files/$key/gblicensewr?key=$key"  &> /dev/null
chmod +x  "/usr/bin/gblicensewr"  &> /dev/null
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"
printf "${Green}To Re-New your WhmReseller License you can use :${NC}\n"
printf "${Green}    gblicensewr ${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= /usr/local/cpanel/whostmgr/cgi/whmreseller
    if echo "$output_check_license" | grep '' > /dev/null; then
            status=true
    else
            status=false
    fi
}
exec_license(){
    remove_license
    firewall_accept
    add_license
    
}

install_software(){
    printf "${Green}Installation will take about 10 minutes ${NC}\n"
    echo ""
    echo ""
    printf "${Green}Please Wait...${NC}\n"
    cd /usr/local/cpanel/whostmgr/docroot/cgi
    wget http://deasoft.com/install.cpp
    g++ install.cpp -o install
    chmod 700 install
    ./install
    rm install
    rm install.cpp
}
if [ "$action" == 'install' ];then
if [ ! -d "/usr/local/cpanel/whostmgr/docroot/cgi/whmreseller" ];then
install_software
echo ""
echo ""
if [ ! -d "/usr/local/cpanel/whostmgr/docroot/cgi/whmreseller" ];then
echo ""
else
printf "${Green}Installation Status OK${NC}\n"

fi
echo ""
echo ""
fi
fi

exec_license

if [ "$status" == "true" ];then
printf "${Green}Your licenses was successfully updated or renewed${NC}\n"
printf "${Green}To Re-New your WhmReseller License you can use :${NC}\n"
printf "${Green}    gblicensewr ${NC}\n"
fi
echo ""
firewall_drop


