#!/bin/bash
#AutoPilot Auto Install
#JoshB HostGator
#josh@hostgator.com

#AP urls
ver2url=http://hostgator.whmautopilot.com/whmauto_V2_hostgator.zip
ver3url=/dev/null
clear

#AP version check
echo "#############################"
echo "## Which Version of AP"
echo "## 1) Version 2        "
echo "## 2) Version 3 "
echo "## Please select option 1 or 2"
echo "#############################"
apversion=fakevar
read apversion

if [ $apversion = 1 ]
then
 echo "Installing Version 2"
else
 if [ $apversion = 2 ]
 then
  echo "Installing Version 3"
  wget toolbox.hostgator.com/cstrong/whmapinstall
  sh whmapinstall
  rm -vf whmapinstall
  exit 0 
 else
  echo "you must choose option 1 or 2"
  exit 0
 fi
fi

# user/domain information
echo ""
echo "please enter the account's cpanel username"
username=fakevar
read username
echo ""
echo "please enter the accounts domain name"
domainname=fakevar
read domainname
echo ""
echo "please enter the path from public_html to install AP"
echo "Leave Blank to install into public_html"
apppath=""
read appath

# confirm information
clear
echo "#########################################"
echo "## Please confirm info"
echo "## Domain: $domainname"
echo "## Username: $username"
echo "## Full Path: /home/$username/public_html/$appath"
echo "#########################################"
echo "Press 1 to confirm, any other key to exit"
apconfirm=fakevar
read apconfirm
if [ $apconfirm = 1 ]
then
 echo "continuing install" 
else
 exit 0
fi

#quota check
clear
for i in `quota -u $username -sv |awk '{print$2}'`; do if [ $(echo $i |grep \* |wc -l ) -gt 0 ]; then echo ACCOUNT IS OVER QUOTA THIS MUST BE FIXED BEFORE THE INSTALL; exit0 ;fi; done

#phpsuexec check
#if [`HEAD localhost |grep PHP-CGI` = ""]
if [ $( /usr/local/cpanel/bin/rebuild_phpconf --current |grep suphp |wc -l ) -gt 1 ]
then
 phpsuexec=1
else
 phpsuexec=0
fi
#creating directories and moving data 
if [ ! -e /home/$username/public_html/$appath ]
then
 mkdir /home/$username/public_html/$appath
fi

cd /home/$username/public_html/$appath
wget $ver2url
unzip whmauto_V2_hostgator.zip
chown $username:$username /home/$username/public_html/$appath -R
cd "whmauto_V2_hostgator/rename_upload/"
mv * ../../
cd ../../
chmod 777 whmauto_V2_hostgator/mib_data -R
mv whmauto_V2_hostgator/mib_data /home/$username/

if [ $phpsuexec  = 0 ]
then chmod 777 /home/$username/public_html/$appath/inc
fi

#time to check/create DB
if [[ -e /var/lib/mysql/${username}_pilot/ ]]
then
 dbname=pilot2
 mysqladmin create ${username}_$dbname
else
 dbname=pilot
 mysqladmin create ${username}_$dbname
fi
echo "replace into user (host,user,password) values('localhost','${username}_pilot',password('pilot123')); FLUSH PRIVILEGES;" | `mysql mysql`
echo "GRANT ALL ON \`${username}\\_${dbname}\`.* TO '${username}_pilot'@'localhost'; FLUSH PRIVILEGES;" |`mysql mysql`

#Cronsetup
usermod -G $username,get-users $username
echo "15 0 * * * GET http://$domainname/$appath/admin/cron_1215.php > /dev/null" >> /var/spool/cron/$username
echo "0 */8 * * * GET http://$domainname/$appath/admin/cron_resolver.php > /dev/null" >> /var/spool/cron/$username
echo "5 0 * * 6 GET http://$domainname/$appath/admin/cron_weekly_sales.php > /dev/null" >> /var/spool/cron/$username
echo "0 0 30 * * GET http://$domainname/$appath/admin/cron_reports_new_monthly_signups.php > /dev/null" >> /var/spool/cron/$username
echo "0 0 30 * * GET http://$domainname/$appath/admin/cron_reports_monthly_revenue.php> /dev/null" >> /var/spool/cron/$username

#All complete
clear
echo "To complete install, please go to http://$domainname/$appath/admin/"
echo "Please use the following information"
echo "DB Name: ${username}_$dbname"
echo "DB User: ${username}_pilot"
echo "Password:  pilot123"
echo "Resolver IP: 4.2.2.1"
echo "Base Server Path: /home/$username/public_html/$appath"
echo "Data Dir: /home/$username/mib_data"
