#!/bin/sh
########################################################
#                                                      #
#       GPTEngine v 1.1.1 -- Linux Shell Installer     #
#                                                      # 
#                  ( 2005 by popux.com)               #
#                                                      #
########################################################
set +v
stty echo
clear
echo "Welcome to the GPTEngine Setup"
echo "------------------------------"
echo "    ( 2005 by popux.com)     "
echo
echo
echo "This will install GPTEngine on your web site"
echo
echo -n "Do you want to proceed?[y/n]"
valid_input='0'
stty_orig=`stty -g`
stty -echo
while [ "$valid_input" = "0" ];
 do
  read -n 1 answer
  if [ "$answer" = "y" ] || [ "$answer" = "n" ]; then
   valid_input='1'
  fi 
 done 
stty $stty_orig  
if [ "$answer" = "n" ]; then
 clear
 exit
fi
clear
cat data/LinuxInst/terms.txt | less -e
echo "GPTEngine Setup"
echo "---------------"
echo
echo
echo -n "Do you agree to the above terms?[y/n]"
valid_input='0'
stty_orig=`stty -g`
stty -echo
while [ "$valid_input" = "0" ];
 do
  read -n 1 answer
  if [ "$answer" = "y" ] || [ "$answer" = "n" ]; then
   valid_input='1'
  fi 
 done 
stty $stty_orig  
if [ "$answer" = "n" ]; then
 clear
 exit
fi
clear
echo "GPTEngine Setup"
echo "---------------"
echo
echo
echo "Please, provide the following information:"
echo
echo "Web Domain (ex. mysite.com):"
read Web_Domain
echo
echo "Document Root (ex. public_html):"
read Document_Root
echo
echo "Account Username:"
read FTP_Username
echo
echo "Account Password:"
stty_orig=`stty -g`
stty -echo
read FTP_Password
stty $stty_orig
echo $FTP_Password | tr '[:print:]' '*'
echo
echo "Select your Web Server OS:"
select Web_Server_OS in  Linux Windows
 do
  break
 done
clear
echo "GPTEngine Setup"
echo "---------------"
echo
echo
echo "MySQL Username:"
read MySQL_Username
echo
echo "MySQL Password:"
stty_orig=`stty -g`
stty -echo
read MySQL_Password
stty $stty_orig
echo $MySQL_Password | tr '[:print:]' '*'
echo
echo "MySQL Database:"
read MySQL_Database
clear
echo "GPTEngine Setup"
echo "---------------"
echo
echo
echo "Site Name:"
read Site_Name
echo 
echo "Site URL (use 'http://' and don't leave the '/' at the end):"
read Site_URL
echo
echo "Site e-Mail:"
read Site_EMail
echo
echo "Site Reply e-Mail:"
read Site_Reply_EMail
clear
echo "GPTEngine Setup"
echo "---------------"
echo
echo
echo "Choose an Administrator Username (6-16 chars):"
read GPTE_Username
echo
echo "Choose an Admisnitrator Password (6-16 chars):"
stty_orig=`stty -g`
stty -echo
read GPTE_Password
stty $stty_orig
echo $GPTE_Password | tr '[:print:]' '*'
echo
echo "Title [Mr/Mrs/Ms/Miss]:"
read Admin_Title
echo
echo "Administrator Name:"
read Admin_Name
echo
echo "Administrator e-Mail:"
read Admin_EMail
clear
echo "GPTEngine Setup"
echo "---------------"
echo
echo
echo "GPTEngine is ready to be installed on your web site."
echo
echo -n "Make sure you are connected to internet and press any key to resume."
old_tty_settings=$(stty -g)
stty -icanon
Keypress=$(head -c1)
stty "$old_tty_settings"
clear
echo "GPTEngine Setup"
echo "---------------"
echo
echo
chmod 755 data/LinuxInst/gpteaux
data/LinuxInst/gpteaux $GPTE_Username $GPTE_Password $MySQL_Username $MySQL_Password $MySQL_Database $Site_URL
if [ "$Web_Server_OS" = "Linux" ]; then
 mv --reply=yes gpteaux.cgi data/cgi_linux/
else
 mv --reply=yes gpteaux.cgi data/cgi_win32/
fi 
echo "Creating SQL file..."

echo "DROP TABLE IF EXISTS \`GPTE_Default_Config\`;" > data/LinuxInst/gpte.sq2
echo "CREATE TABLE \`GPTE_Default_Config\` (\`Opt\` varchar(32) NOT NULL default '''',\`Val\` text,PRIMARY KEY (\`Opt\`),UNIQUE KEY \`Opt\` (\`Opt\`));" >> data/LinuxInst/gpte.sq2
echo "/*!40000 ALTER TABLE \`GPTE_Default_Config\` DISABLE KEYS */;" >> data/LinuxInst/gpte.sq2
echo "LOCK TABLES \`GPTE_Default_Config\` WRITE;" >> data/LinuxInst/gpte.sq2
echo "INSERT INTO \`GPTE_Default_Config\` VALUES ('Administrator_EMail','$Admin_EMail'),('Administrator_Name','$Admin_Name'),('Administrator_Title','$Admin_Title');" >> data/LinuxInst/gpte.sq2
echo "INSERT INTO \`GPTE_Default_Config\` VALUES ('Site_Name','$Site_Name'),('Site_URL','$Site_URL'),('GPTEngine_Pages_URL','$Site_URL/pages'),('GPTEngine_Docs_URL','$Site_URL/docs');" >> data/LinuxInst/gpte.sq2
echo "INSERT INTO \`GPTE_Default_Config\` VALUES ('PHP_Temp_URL','$Site_URL/cgi-bin/tp');" >> data/LinuxInst/gpte.sq2
echo "INSERT INTO \`GPTE_Default_Config\` VALUES ('Admin_Session_Time_Out','1800'),('Advertiser_Login_With_EMail','0'),('Delete_Suspended_Users','0');" >> data/LinuxInst/gpte.sq2
echo "INSERT INTO \`GPTE_Default_Config\` VALUES ('Free_Referrals_Redistribution','95'),('gpte.cgi_Alias','gpte.cgi'),('gpteclick.cgi_Alias','gpteclick.cgi'),('Max_Clicking_Interval','30'),('Max_Login_Interval','30'),('Max_Mailer_Childs','5');" >> data/LinuxInst/gpte.sq2
echo "INSERT INTO \`GPTE_Default_Config\` VALUES ('Max_Mailer_Running_Time','100'),('Max_Mails_to_Sent','100'),('Max_Mails_to_Sent_Interval','1'),('Max_Reading_Interval','30'),('Max_Surfing_Interval','30'),('Minimum_Payout','10.00'),('NavBar_Frame_Size','20');" >> data/LinuxInst/gpte.sq2
echo "INSERT INTO \`GPTE_Default_Config\` VALUES ('Payout_ID_Info','PayPal_ID'),('PHP_Temp_Dir','tp'),('Point_Value','0.01'),('Same_IP_Blocking_Interval','720'),('Session_Time_Out','3600'),('Site_Copyright_Notice',CONCAT('',CONCAT(YEAR(NOW()),' by $Site_Name'))),('Site_EMail','$Site_EMail');" >> data/LinuxInst/gpte.sq2
echo "INSERT INTO \`GPTE_Default_Config\` VALUES ('Site_Reply_EMail','$Site_Reply_EMail'),('Timer_Frame_Size','20'),('User_Login_With_EMail','0');" >> data/LinuxInst/gpte.sq2
if [ "$Web_Server_OS" = "Windows" ]; then
 echo "INSERT INTO \`GPTE_Default_Config\` VALUES ('SMTP_Need_Authentication','0'),('SMTP_Password',NULL),('SMTP_Server',NULL),('SMTP_Username',NULL);" >> data/LinuxInst/gpte.sq2
else
 echo "INSERT INTO \`GPTE_Default_Config\` VALUES ('Sendmail_Path','/usr/sbin/sendmail'),('Sendmail_Queueing','0');" >> data/LinuxInst/gpte.sq2
fi 
echo "UNLOCK TABLES;" >> data/LinuxInst/gpte.sq2
echo "/*!40000 ALTER TABLE \`GPTE_Default_Config\` ENABLE KEYS */;" >> data/LinuxInst/gpte.sq2
cat data/LinuxInst/gpte.sq1 > gpte.sql
cat data/LinuxInst/gpte.sq2 >> gpte.sql
cat data/LinuxInst/gpte.sq3 >> gpte.sql
rm -f data/LinuxInst/gpte.sq2
if [ "$Web_Server_OS" = "Windows" ]; then
 mv --reply=yes gpte.sql data/cgi_win32/
else  
 mv --reply=yes gpte.sql data/cgi_linux/
fi 
echo "Starting uploading..."
ftp -in $Web_Domain <<Quit_Uploading
quote USER $FTP_Username
quote PASS $FTP_Password
cd $Document_Root
mkdir page_pics
cd page_pics
lcd data/www/page_pics
binary
mput *
cd ..
lcd ..
rename index.html old_index.html
rename favicon.ico old_favicon.ico
ascii
mput *.html
binary
put favicon.ico
mkdir pages
cd pages
mkdir pics
cd pics
mkdir flags
cd flags
lcd ../pages/pics/flags
binary
mput *
cd ..
lcd ..
mput *.jpg
cd ..
mkdir styles
cd styles
lcd ../styles
ascii
mput *
cd ..
lcd ..
mput *.gpte
mput *.html
cd ..
mkdir docs
cd docs
lcd ../docs
mput *
cd ..
mkdir cgi-bin
cd cgi-bin
if [ "$Web_Server_OS" = "Windows" ]; then
 lcd ../cgi_win32
else 
 lcd ../cgi_linux
fi 
mkdir tp
put gpte.css
hash
put gpte.sql
hash
binary
put gptelogo.jpg
if [ "$Web_Server_OS" = "Linux" ]; then
 ascii
 put gptelogo.cgi
 put gptestyle.cgi
 binary
fi 
mput *.cgi
if [ "$Web_Server_OS" = "Windows" ]; then
 ascii
 mput *.bat
else 
 chmod 777 tp
 chmod 666 gpte.sql
 chmod 666 gpteaux.cgi
 chmod 755 gpte00.cgi
 chmod 755 gpte01.cgi
 chmod 755 gpte02.cgi
 chmod 755 gpte03.cgi
 chmod 755 gpte04.cgi
 chmod 755 gpte05.cgi
 chmod 755 gpte06.cgi
 chmod 755 gpte07.cgi
 chmod 755 gpte08.cgi
 chmod 755 gpte09.cgi
 chmod 755 gpte10.cgi
 chmod 755 gpte11.cgi
 chmod 755 gpte12.cgi
 chmod 755 gpte13.cgi
 chmod 755 gpte14.cgi
 chmod 755 gpte15.cgi
 chmod 755 gpte16.cgi
 chmod 755 gpte17.cgi
 chmod 755 gpte18.cgi
 chmod 755 gpte.cgi
 chmod 755 gptebuyit.cgi
 chmod 755 gpteclick.cgi
 chmod 755 gptecntct.cgi
 chmod 755 gptecrdt.cgi
 chmod 755 gptecvtpnt.cgi
 chmod 755 gptelogin.cgi
 chmod 755 gptelogo.cgi
 chmod 755 gptemailer.cgi
 chmod 755 gptemsg.cgi
 chmod 755 gptemsgbox.cgi
 chmod 755 gptenav.cgi
 chmod 755 gptengine.cgi
 chmod 755 gptenvcrdt.cgi
 chmod 755 gptepytreq.cgi
 chmod 755 gptesetup.cgi
 chmod 755 gptesignup.cgi
 chmod 755 gptestyle.cgi
 chmod 644 gptetndig.cgi
 chmod 755 gpteturnbr.cgi
 chmod 755 gpteuninst.cgi
 chmod 755 gpteupgaff.cgi
 chmod 755 gptevac.cgi
fi
quit 
Quit_Uploading
echo "Cleaning temporary files..."
if [ "$Web_Server_OS" = "Linux" ]; then
 rm -f data/cgi_linux/gpteaux.cgi
 rm -f data/cgi_linux/gpte.sql
else
 rm -f data/cgi_win32/gpteaux.cgi
 rm -f data/cgi_win32/gpte.sql
fi
lynx $Site_URL/cgi-bin/gptesetup.cgi
clear
exit 0