#!/bin/bash

#
# SAXON-HE installator for PHP on 64bit debian based system
#
# @author Petr Zak(zak@cyberdeck.cz)
#

#basic variables used in this script (possible future feature - override from parametrs)
sourceDir=$(pwd);
phpConfDir=/etc/php5/

#try to detect, if this package is already installed
if [ -f /usr/lib/libsaxoneec.so ]; then
  echo "It seems that this package is already installed, run uninstall.sh first!"
  exit 1
fi

#detect if system is 64bit, 'cause this package is designed for 64bit systems
if [ $(uname -m | grep "64" | wc -l) == 0 ]; then
  echo "This package is used for 64bit system, but your system is only 32bit. Download saxon version for 32bit system."
  exit 1
fi



#detect if our apache webserver is called apache2 (deb based systems) or apache2 (rpm based systems)
if [ $(service apache2 status | grep "apache2 is" | wc -l) == 1  ]; then
  apacheServerName="apache2"
elif [ $(service httpd status |grep "httpd" | grep "is" | wc -l) == 1 ]; then
  apacheServerName="httpd"
else
  echo "Apache webserver (package apache2 or httpd) not found. Terminating"
  exit 1
fi

#notify user that script is running
echo ''
echo "libsaxon PHP installer. ";
echo ""

#test if is running under root user, it is required 'cause system service (webserver) handling, and some /usr/lib and /lib directories manipulation
if [[ $EUID -ne 0 ]]; then
  echo "This installator must be executed as a root.";
  exit 1;
fi

#function that detect id $param1 package is installed
#
# @param $1 package name
# @return void
#
function testPackageIsPresent {
  echo -ne "Checking if \"$1\" is installed............."
  #installed software detection depends on package system (rpm/deb)...
  if [ -f /bin/rpm ]; then
    inst=$(rpm -qa $1 $2 $3 $4 $5 $6| wc -l)
  elif [ -f /usr/bin/dpkg ]; then
    inst=$(dpkg -l $1 $2 $3 $4 $5 $6| grep "ii" | wc -l)
  else 
    echo "unknown package system, this installer needs dpkg or rpm";
    exit 1
  fi
  
  if [ $inst != 1 ]; then
    echo "failed"
    echo ""
    echo "Package \"$1 $2 $3 $4 $5 $6\" must be installed. Please install it manually and rerun this installer"
    echo ""
    exit 1
  else
    echo "OK"
  fi
}

#checking prerequeisites - installed packages, environment variables (JAVA_HOME...)
echo "Checking prerequesities... "

testPackageIsPresent make
testPackageIsPresent php-devel php5-dev php55-dev php55w-devel
testPackageIsPresent apache2 httpd
testPackageIsPresent gcc-c++ g++

 if [ -z ${JAVA_HOME} ]; then
  echo "JAVA_HOME not set, please set it and rerun this installer"
  exit 1
fi
  

#copying module libraries into system directories
echo ""
echo "Compiling module for php"
cp -r $sourceDir/rt/ /usr/lib/
cp libsaxon.so /usr/lib/
cd $sourceDir/Saxon-C-API/

#compilation of php module
phpize
./configure --enable-saxon
export LD_LIBRARY_PATH=/usr/lib/rt/amd64:/usr/lib/jvm/java-6-openjdk-amd64/include/linux:/usr/lib/jvm/java-6-openjdk-amd64/include:$LD_LIBRARY_PATH

#in my case, c++ compiler requires include path to java header files during compilation, so according to http://www.network-theory.co.uk/docs/gccintro/gccintro_23.html,
# I add these dirs in my JAVA_HOME directory to this environment variable used by compiler.
CPLUS_INCLUDE_PATH=/usr/lib/rt/amd64:$JAVA_HOME/include/linux:$JAVA_HOME/include
export CPLUS_INCLUDE_PATH

make
make install

#add tell php/apache to load saxon module
echo ""
echo "Adding PHP module to PHP configuration"

if [ -d $phpConfDir ]; then #possible deb based systems, modules ini are symlinks
  echo "extension=saxon.so" >$phpConfDir/mods-available/saxon.ini
  ln -s $phpConfDir/mods-available/saxon.ini $phpConfDir/apache2/conf.d/90_saxon.ini
elif [ -d /etc/php.d ]; then
  echo "extension=saxon.so" > /etc/php.d/saxon.ini
else 
  echo "PHP configuration file not found, please add 'extension=saxon.so' to your php ini file manually"
fi

#create symlink to java virtual machine, it is not required to install/compile, but it's requirement to use it from PHP code.
if [ -d /lib ]; then
  ln -s  $JAVA_HOME/jre/lib/amd64/server/libjvm.so /lib/libjvm.so
fi

if [ -d /lib64 ]; then
  ln -s  $JAVA_HOME/jre/lib/amd64/server/libjvm.so /lib64/libjvm.so
fi

#finally restart webserver 'cause adding new php module
service $apacheServerName restart

#finally DONE! time for beer :-)
echo "Saxon for PHP finally installed. Done."
