#!/bin/bash

. ${TOOLKIT}-functions.sh

appname="glpi"
appnick="GLPI"
module="srv-$appname"

version=$(jtconf $module.version)
tarfile=$(jtconf $module.tarfile)
repourl=$(jtconf $module.repourl)
instdir=$(jtconf $module.instdir)

user=$(jtconf apache.user)
group=$(jtconf apache.group)
owner="$user:$group"

tardir="$appname"


config_dir=$(jtconf $module.config_dir)
var_dir=$(jtconf $module.var_dir)
log_dir=$(jtconf $module.log_dir)

db_host=$(jtconf $module.db_host)
db_port=$(jtconf $module.db_port)
db_name=$(jtconf $module.db_name)
db_user=$(jtconf $module.db_user)
db_pass=$(jtconf $module.db_pass)


here=$(pwd)

php_get_define()
{
	# grep can be tricky, since quotes can be ' or " and shell
	# doesn't escape well on quotes, so we preprocess input file
	# replacing all " with ', and only after we use grep
	#
	# more, you can have extra spaces in various places (php syntax, yeeee!!)
	#
	local match=$1
	local file=$2
	local out=

	out=$(tr '"' "'" <$file | grep "define\s*(\s*'$match'")
	echo "$out" | sed -e "s/.*,\s*'//" -e "s/'.*//"
}


# check the installed version
#
file="$instdir/inc/define.php"
cur_version=
need_install=false

[ -f $file ] && {
	# define('GLPI_VERSION', '10.0.10');
	cur_version=$(php_get_define "GLPI_VERSION" $file)
}

case $cur_version in
  $version)	echo "  $appnick: up-to-date (version=$cur_version)" ;;
  "")		need_install=true; echo "  $appnick: need to be installed" ;;
  *)		need_install=true; echo "  $appnick: must be upgraded (cur=$cur_version, new=$version)" ;;
esac

###cd $RUNDIR
[ -d /tmp/glpi ] || mkdir /tmp/glpi ###
cd /tmp/glpi ###

$need_install && {
	rm -f $tarfile
	wgetfile $repourl/$tarfile

	echo -e "  $appnick: extracting files ..."
	tar xfpz $tarfile

	[ -d $tardir ] || {
		echo "  ERROR: can't find '$tardir' dir in $tarfile"
		exit 1
	}
	[ -f $tardir/index.php ] || {
		echo "  ERROR: can't find '$tardir/index.php' file in $tarfile (wrong tarfile?)"
		exit 1
	}

	echo -n "  $appnick: installing $appname $version -> $instdir ... "
	(cd $tardir; find . | cpio -pdum $instdir/)

	$var_dir/fix_perms
}

# copy datadir structure
#
echo -n "  updating datadir structure in '$var_dir' ... "
cd $instdir/files
find * -type d | cpio -pdum "$var_dir/"


cd $instdir
[ -d install ] && {

	# setup db (must be created before)
	#
	echo -e "\n  fresh install, create default database, please wait ... \n"

	php bin/console db:install \
		-H $db_host \
		-P $db_port \
		-d $db_name \
		-u $db_user \
		-p $db_pass \
		-v || {
		echo -e "\n   WARNING: db:install returned status=$?\n"
	}

	# rename install directory (for security reasons, too)
	now=$(date '+%Y-%m-%d')
	mv install install_$now
	echo "   'install' directory rename to 'install_$now'"
}


# install plugins
#
cd $MODRUNDIR

plugins=$(jtconf $module.plugins)
pluginsdir="$instdir/plugins"

for plugin in $plugins
do
	tarfile="$plugin.tar.bz2"
	name=$(echo $plugin | sed -e 's/^glpi-//' -e 's/-[0-9].*//')
	ucname=$(echo $name | tr '[a-z]' '[A-Z]')
	version=$(echo $plugin | sed -e "s/glpi-$name-//")
	must_install=

	if [ -d $pluginsdir/$name ]
	then
		curversion=$(php_get_define "PLUGIN_${ucname}_VERSION" $pluginsdir/$name/setup.php)
		[ "$version" != "$curversion" ] && must_install="DIFFERENT VERSION (cur: $curversion, new: $version)"
	else
		must_install="NOT INSTALLED"
	fi

	if [ "$must_install" = "" ]
	then
		echo "   plugin $name:  UP TO DATE ($version)"
	else
		echo "   plugin $name:  $must_install"
		echo -n "   plugin $name:"
		wgetfile $repourl/$tarfile
		echo -n "   plugin $name:  installing $plugin ... "
		(cd $instdir/plugins; tar xfp $MODRUNDIR/$tarfile)
		echo "ok"
	fi
done

$var_dir/fix_perms

$SOMETHING_CHANGED && {
	restart_service apache2 apache2
}

exit 0
