#!/bin/bash

. ${TOOLKIT}-functions.sh

product="egroupware"

repo=`jtconf install.repo_downloaded`	|| exit_missing_define "install.repo_downloaded"

version=`jtconf $product.version`	|| exit_missing_define "$product.version"
tarfile=`jtconf $product.tarfile`	|| exit_missing_define "$product.tarfile"
instdir=`jtconf $product.instdir`	|| exit_missing_define "$product.instdir"

verfile="$instdir/VERSION-$version.txt"

here=`pwd`

# check the installed version
#
[ -f $verfile ] || {	# not installed
	echo -e "\n  installing $product version $version ...\n"
	cd $RUNDIR
	rm -f $tarfile
	wgetfile $repo/$product/$tarfile || exit $?

	echo -e "  extracting files ..."
	tar xfpz $tarfile || exit $?

	[ -d $instdir ] && {
		echo "  WARN: broken installation found, renamed $instdir.broken"
		mv $instdir $instdir.broken || exit $?
	}

	basedir=`dirname $instdir`

	[ -d $basedir ] || {
		create_dir $basedir root:root || exit $?
	}

	echo -e "  installing files in $instdir ..."
	mv $product $instdir	|| exit $?

	# mark version
	#
	echo "$version" > $verfile

	cd $here
}

# sostituisce .htaccess
# (non si puo` fare da file "install" perche` l'install dir e` variabile
#
installfile htaccess $instdir/.htaccess www-data:www-data 660


# fix install dir perms (always)
#
chown -R www-data:www-data $instdir	|| exit $?
chmod -R ug+w $instdir			|| exit $?
chmod -R o-w $instdir			|| exit $?


# installa config files (non si puo` fare usando file 'install' perche`
# la dir di installazione e` una variabile)
#
cd $here

##installfile main.inc.php $instdir/config/ root:root 644	|| exit $?
##installfile db.inc.php   $instdir/config/ root:root 644	|| exit $?
##for plugin in `ls *-config.inc.php 2>/dev/null`
##do
	##plugin=`echo $plugin | sed -e 's/-config.inc.php//'`
	##installfile $plugin-config.inc.php $instdir/plugins/$plugin/config.inc.php \
		##root:root 644 || exit $?
##done



# installa eventuale config mysql client (per maintenance tasks)
# (solo se non c'e` installato il server mysql locale)
#
###is_installed mysql-server || {
	###installfile mysql-client.cnf /etc/mysql/my.cnf root:root 644 || exit $?
###}




$SOMETHING_CHANGED && {
	restart_service apache2 apache2
}

exit 0
