#!/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'
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'
pythonversion='3.8'


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"
#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

$pmanager update

wget http://repo.optrix.com.au/linux/$distro/$codename -O config.sh
source config.sh
#./instscript $version

phprequired=$(echo "$phpdeps" | sed "s/XXX/$phpversion/g")
pythonrequired=$(echo "$pythonpackage" | sed "s/XXX/$pythonversion/g")
mysqlrequired=$(echo "$mysqldeps" | sed "s/XXX/$phpversion/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
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 "Apache needs to be installed"
   "$pmanager" install $quietinstall "$apachedeps" 
#fi

echo "Checking for installation of PHP"
#if ! type "php" > /dev/null; then
   echo "Your system needs to have PHP installed."
   "$pmanager" install $quietinstall $phprequired
#fi

echo "Checking for installation of Python"
#if ! type "python" > /dev/null; then
   echo "Your system needs to have Python installed."
   "$pmanager" install $quietinstall $pythonpackage
#fi

echo "Checking for installation of Python PIP Module"
#if ! type "pip3" > /dev/null; then
   echo "Your system needs to have PIP installed."
   "$pmanager" install $quietinstall $pythonpip
#fi

echo "Checking for installation of MySQL"
if ! pidof "mysqld" > /dev/null
then
   echo "No MySQL server detected on this system"
   "$pmanager" install $quietinstall $mysqlrequired
   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

if ! type "unzip" > /dev/null; then
   "$pmanager" install $quietinstall unzip
fi

if ! type "dos2unix" > /dev/null; then
   "$pmanager" install $quietinstall dos2unix
fi

if [ "$miscextras" != "" ]; then
  "$pmanager" install $quietinstall $miscextras
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 xmltodict pytz tzlocal pycurl

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
  tar -xvf $ioncubefile
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"

service apache2 restart

wget http://repo.optrix.com.au/linux/inst_ardi -O install_ardi
chmod u+x install_ardi
./install_ardi "$phpversion" "$pythonversion"

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