#!/bin/bash

echo '----ARDI Linux Installer Script----'

echo "Checking for lsb_release"
if ! type "lsb_release" > /dev/null; then
   echo "Your system needs to have lsb_release installed."
   yum install redhat-lsb-core
fi

echo 'Checking for Linux Distribution...'
linux="debian"
distro=`lsb_release -a | awk '/Distributor ID/ { print $3; exit; }'`
codename=`lsb_release -a | awk '/Codename/ { print $2; exit; }'`
apachename='apache2'
pmanager='apt-get'
apachedeps='apache2'
phpdeps='phpXXX phpXXX-gd phpXXX-simplexml phpXXX-curl phpXXX-zip phpXXX-mbstring'
preupdate='apt-get update'
pythonpackage='python3.8'
pythonpip='python3-pip'
mysqldeps='mariadb-server mariadb-client phpXXX-mysql'
ioncubeurl='http://downloads3.ioncube.com/loader_downloads/ioncube_loaders_lin_x86-64.tar.gz'
phpconfigfolder='/etc/php'
phpmodtool='phpenmod'
miscextras=''
quietinstall='-y'
phpversion='7.4'
phpversionsub='7.4'
pythonversion='3.8'
pythonversionsub='3.8'
webuser='www-data'
apacheconfigdir="/etc/apache2/sites-available"
redirect='2>&1 >> install.log'

echo "Checking for package manager (yum)"
if ! type "yum" > /dev/null; then
   echo "Debian-Based Linux"
else
   echo "Redhat-Based Linux"
   pmanager="yum"
   quietinstall="-y"
fi

echo "Installing for $distro / $codename"

version="release"
NEEDRESTART_SUSPEND=1
DEBIAN_FRONTEND=noninteractive
#distro=echo "$distro" | awk '{print tolower($0)}'

#if [ $# -eq 0 ]
#then
#   echo "Which version of ARDI would you like to install?"
#   select rb in "Release" "Beta"; do
#      case $rb in
#        Release ) version="release"; folder=""; break;;
#        Beta ) version="beta"; folder=""; break;;
#      esac
#   done
#else
#   version=$1
#fi

#if [[ wget -S --spider $1  2>&1 | grep 'HTTP/1.1 404 Not Found' ]]; then echo "WARNING: This version of Linux is not specifically recognised."; fi

echo "Upgrading System"
$pmanager update &>>install.log

echo "Downloading Scripts for This Distribution"
wget http://repo.optrix.com.au/linux/$distro/$codename -O config.sh &>>install.log
source config.sh
#./instscript $version

phprequired=$(echo "$phpdeps" | sed "s/XXX/$phpversionsub/g")
pythonrequired=$(echo "$pythonpackage" | sed "s/XXX/$pythonversionsub/g")
mysqlrequired=$(echo "$mysqldeps" | sed "s/XXX/$phpversionsub/g")

echo "Requires PHP Modules: $phprequired" 
echo "Requires Python Package: $pythonpackage"
echo "Requires MariaDB Package: $mysqlrequired"

preinst=$codename"_preinst"

echo "Checking for any required platform pre-installation ($codename_preinst)"
wget http://repo.optrix.com.au/linux/$distro/$preinst -O preinst.sh &>> install.log
if [ -f "preinst.sh" ]; then
  chmod u+x preinst.sh 
  ./preinst.sh
fi

#echo "Checking for Apache installation"
#if ! pidof "$apachename" > /dev/null
#then
   echo "Installing Apache web-server"
   "$pmanager" install $quietinstall "$apachedeps" &>>install.log
#fi

#echo "Checking for PHP"
#if ! type "php" > /dev/null; then
   echo "Installing PHP"
   "$pmanager" install $quietinstall $phprequired &>>install.log
#fi

#echo "Checking for installation of Python"
#if ! type "python" > /dev/null; then
   echo "Installing Python"
   "$pmanager" install $quietinstall $pythonpackage &>>install.log
#fi

#echo "Checking for installation of Python PIP Module"
#if ! type "pip3" > /dev/null; then
   echo "Installing PIP Python Package Manager"
   "$pmanager" install $quietinstall $pythonpip &>>install.log
#fi

#echo "Checking for installation of MySQL"
if ! pidof "mysqld" > /dev/null
then
   echo "Installing MySQL"
   "$pmanager" install $quietinstall $mysqlrequired &>>install.log
   echo "Setting MySQL Root Password"
   echo "SET PASSWORD FOR 'root'@'localhost'=PASSWORD('');" | mysql -u "root"
   mysql --user=root -e "UPDATE mysql.user SET plugin='mysql_native_password'; FLUSH PRIVILEGES";
fi

echo "Installing Utility Packages"
if ! type "unzip" > /dev/null; then
   "$pmanager" install $quietinstall unzip dos2unix $miscextras &>>install.log
fi

echo "Checking for installation of IonCube Loader"

phpversion=`php -v | awk '/PHP/ {print $2;exit;}'`
phpvera="`echo "$phpversion" | cut -d. -f1`"
phpverb="`echo "$phpversion" | cut -d. -f2`"

echo "Detected PHP Version $phpvera.$phpverb from $phpversion"

fromver="7.0"
tover="$phpvera.$phpverb"

phpdeps=${phpdeps//$fromver/$tover}

echo "Installing Python Modules"
pip3 install -U wheel &>>install.log
pip3 install -U xmltodict pytz tzlocal pycurl &>>install.log

echo "Installing IonCube Loader"

phpextdir=`php -i | awk '/extension_dir/ { print $3; exit; }'`

loaderversion="ioncube_loader_lin_$phpvera.$phpverb.so"

if [ "$phpconfigfolder" == "/etc/php" ]; then
   phpmoddir="/etc/php/$phpvera.$phpverb/mods-available"
else
   phpmoddir="$phpconfigfolder"
fi

ioncubefile="icube.tar.gz"

#mkdir /usr/src
#cd /usr/src

echo "PHP Mods are located in $phpmoddir"

if test -f "$ioncubefile"; then
   echo "IonCube Loaders Already Downloaded"
else
  wget $ioncubeurl -O $ioncubefile &>>install.log
  tar -xvf $ioncubefile &>>install.log
fi
tmp="`cp ioncube/"$loaderversion" "$phpextdir"/ioncube.so`"
printf "; ioncube configuration\n; priority=01\nzend_extension=ioncube.so\n" > ioncube.ini
#tmp="`cp ioncube.ini "$phpmoddir"/ioncube.ini`"
$phpmodtool ioncube

cp ioncube.ini "$phpconfigdir/01-ioncube.ini"

echo "Restarting Apache"
service "$apachedeps" restart

echo "Running Environment Setup"
envfile=$codename"_env"
wget http://repo.optrix.com.au/linux/$distro/$envfile -O environment.sh &>>install.log
if [ -f "environment.sh" ]; then
  chmod u+x environment.sh
  ./environment.sh
fi

echo "Downloading Core Installation Script"
wget http://repo.optrix.com.au/linux/inst_ardi -O install_ardi &>>install.log
chmod u+x install_ardi
./install_ardi "$phpversion" "$pythonversion" "$webuser"

postinst=$codename"_postinst"

echo "Checking for any required platform post-installation ($codename_preinst)"
wget http://repo.optrix.com.au/linux/$distro/$postinst -O postinst.sh &>>install.log
if [ -f "postinst.sh" ]; then
  chmod u+x postinst.sh
  ./postinst.sh
fi

echo 'Installation Complete - Visit http://localhost in your web browser'