#!/bin/bash
#
# ::copy::
# ::maintainer::
#
CMD=$(basename $0)
PACKAGE="glpi"
TPLDIR="/usr/share/ku-${PACKAGE}-installer"

default_file_mode="664"
default_dir_mode="2770"
default_chmod="ug+rw"




usage()
{
	echo "usage: $CMD" >&2
	exit 1
}

cleanup()
{
	rm -f $tmpfile
}

fix_perms()
{
	set -e

	echo -n " fixing perms ($user:$group) ..."
	$CHOWN -R $user:$group $HERE
	echo -n "."
	$CHMOD -R $default_chmod $HERE
	echo -n "."
	find $HERE/. -type d -print0 | xargs -0 $CHMOD $default_dir_mode
	echo -n "."
	# at least these must be writeable
	##for dir in $foswiki_datadir $foswiki_pubdir $foswiki_workdir
	##do
		##[ -d $dir ] && {
			##$CHMOD -R $default_chmod $dir
		##}
	##done

	##[ -f lib/LocalSite.cfg ] && {
		##$CHMOD $default_chmod lib/LocalSite.cfg
	##}
	##echo -n "."
	echo " done"
	set +e
}

get_and_install()
{
	local stat=
	local swdir="${PACKAGE}-$software_release"
	local file=
	local target=

	cd $instdir
	[ -s $software_file ] || {
		wgetfile $software_url/$software_file $software_file || {
			stat=$?
			rm -f $software_file
			return $stat
		}
	}

	echo "  unpacking $software_file ... "
	$software_unpack $software_file || return $?

	[ -d $swdir ] || {
		error "directory '$swdir' not found after unpack"
		return 1
	}

	fix_perms

	echo -n "  moving files ... "
	cd $swdir
	find | cpio -pduml .. >$tmpfile 2>&1 || {
		stat=$?
		tail -10 $tmpfile
		error "during file copy ..."
		return $?
	}
	cd ..
	rm -rf $swdir
	echo "ok"

	fix_perms

	return 0
}



# post install
#
post_install_tasks()
{
	local tag="XXX-DO-NOT-EDIT-XXX"

	rm -f *htaccess.txt

	cktag $tag bin/.htaccess	&& installfile bin-htaccess.txt bin/.htaccess
	cktag $tag pub/.htaccess	&& installfile pub-htaccess.txt pub/.htaccess

	cktag $tag .htaccess && {
		if [ "X$redirect_index" == "Xtrue" ]
		then
			installfile redirected-htaccess.txt .htaccess
		else
			installfile root-htaccess.txt .htaccess
		fi
	}

	file="subdir-htaccess.txt"
	for dir in `ls`
	do
		[ -d "$dir" ] || continue
		[ "$dir" == "bin" -o $dir == "pub" ] && continue

		cktag $tag $dir/.htaccess && installfile $file $dir/.htaccess
	done

	# this one only if not already present
	#
	[ -f lib/LocalSite.cfg ] || {
		installfile "LocalSite.cfg" "lib/"
	}

	# initial default .htpasswd for "configure", contains
	# user 'admin' with the same password
	#
	[ -f bin/.htpasswd ] || {
		installfile cfg-default-htpasswd bin/.htpasswd
	}

	# custom addons
	#
	##installfile PAM.pm	lib/Foswiki/Users/
	##installfile PAMsudo.pm	lib/Foswiki/Users/

	fix_perms
}


cktag()
{
	[ -f $2 ] || return 0
	grep -q "$1" $2
}


save_config()
{
	# records install infos
	#
	echo "# config saved on `date`

# CUSTOMIZABLE DEFINES

# this is the name of your company wiki web, eg: MyCompany
# set to empty value if you want to use the default Main web
#
foswiki_company_webname='$foswiki_company_webname'

# the webmaster (or wiki admin) email
#
foswiki_webmaster_email='$foswiki_webmaster_email'


# password manager, use one of:
#
#  none
#  htpasswd	expanded in Foswiki${jtescape}HtPasswd${jtescape}User
#  apache	expanded in Foswiki${jtescape}ApacheHtpasswd${jtescape}User
#  pam		expanded in Foswiki${jtescape}Users${jtescape}PAMsudo
#		(kusa custom PAM module)
#
foswiki_password_manager='$foswiki_password_manager'

# true/false: set true to enable redirection of index.html (the default
# 	provided by installation) to wiki/view (the main wiki page)
#
redirect_index='$redirect_index'


# USUALLY YOU DON'T NEED TO CHANGE THE DEFINES BELOW

foswiki_defaulturlhost='$foswiki_defaulturlhost'

software_url='$software_url'
software_release='$software_release'
software_file='$software_file'
software_unpack='$software_unpack'

foswiki_datadir='$foswiki_datadir'
foswiki_pubdir='$foswiki_pubdir'
foswiki_workdir='$foswiki_workdir'
foswiki_scripturlpath='$foswiki_scripturlpath'
foswiki_admins_list='$foswiki_admins_list'
foswiki_config_allowed_ips='$foswiki_config_allowed_ips'

foswiki_session_permission='$foswiki_file_permission'

# end of file
"
}

install_file()
{
	TOOLKIT=kusa jtinstall --sudo "$@" $user:$group $default_file_mode
}


install_overwrite()
{
	local dir=$1
	local tdir=$2
	local file=

	for file in $( (cd $dir; ls *.txt) )
	do
		install_file $dir/$file $tdir/ || return $?
	done
	return 0
}

install_if_not_present()
{
	local dir=$1
	local tdir=$2
	local file=

	for file in $( (cd $dir; ls *.txt) )
	do
		$is_new_install && rm -f $tdir/$file $tdir/$file,v

		[ -f $tdir/$file ] && continue
		install_file $dir/$file $tdir/ || return $?
	done
	return 0
}





# (MAIN)

export VERBOSE=true
export DEBUG=false

tmpfile=`mktemp /tmp/$CMD-XXXXXXX`
release_file=".release"
install_file=".install"
installused_file=".install_used"

jtescape="_"">""_"	# note the tricky use of " to avoid jtconf parsing

trap "echo '*INTR*'; cleanup; exit 127" 1 2 3
trap "cleanup" EXIT


# get common www install functions
#
. www-install-lib.sh

instdir=`pwd`

eval $(grep 'APACHE_RUN_USER=' /etc/apache2/envvars)
eval $(grep 'APACHE_RUN_GROUP=' /etc/apache2/envvars)

user=$(stat --format '%U' ..)
group=$(stat --format '%G' ..)

##if [ -f data/Main/WikiUsers.txt ]
##then
	##is_new_install=false
##else
	is_new_install=true
##fi

exit 0

# sanity checks
#
check_installdir $instdir wiki foswiki || exit $?

# get install infos
#
for file in $release_file $install_file
do
	[ -f $file ] && {
		echo "  reading config from '$file'"
		. $file
	}
done

# defaults
#
software_url=${software_url:-`kusa-conf $PACKAGE.repo_url`}
software_release=${software_release:-`kusa-conf $PACKAGE.release`}
software_file=${software_file:-`kusa-conf $PACKAGE.file`}
software_unpack=${software_unpack:-`kusa-conf $PACKAGE.unpack_cmd`}

installed_release=${installed_release:-""}
redirect_index=${redirect_index:-"false"}

for val in "$software_url" "$software_release" "$software_file" "$software_unpack"
do
	[ "$val" == "" ] && {
		error "some install infos are missing"
		exit 1
	}
done


# env for parsing
#
host=$(basename $(pwd | sed -e 's#/docs##' -e 's#/wiki##'))
[ "$host" == "default" ] && host=`uname -n`


export foswiki_datadir="$HERE/data"
export foswiki_pubdir="$HERE/pub"
export foswiki_workdir="$HERE/working"
export foswiki_scripturlpath="$BASEURL/bin"
export foswiki_admins_list=${foswiki_admins_list:-"admin"}
export foswiki_config_allowed_ips=${foswiki_config_allowed_ips:-`kusa-conf $PACKAGE.config_allowed_ips`}
export foswiki_defaulturlhost=${foswiki_defaulturlhost:-"http://$host"}
export foswiki_webmaster_email=${foswiki_webmaster_email:-""}
export foswiki_session_permission=${foswiki_session_permission:-"384"}
export foswiki_dir_permission=${foswiki_dir_permission:-"1528"}
export foswiki_file_permission=${foswiki_file_permission:-"432"}
export foswiki_password_manager=${foswiki_password_manager:-"pam"}
export foswiki_company_webname=${foswiki_company_webname:-$(kusa-conf customer.wikiname 2>/dev/null)}

if [ "$foswiki_config_allowed_ips" != "" ]
then
	export tmp_config_allowed_ips="	Allow from $foswiki_config_allowed_ips"
else
	export tmp_config_allowed_ips=""
fi

export tmp_allow_login_name=0
export tmp_password_manager=

# install infos
#
[ -f $install_file ] || {
	echo "No installation directives found, I will create"
	echo "the file '$install_file', edit it and relaunch $CMD"
	save_config >$install_file
	exit 0
}


echo "
  installed release: $installed_release
  latest release:    $software_release
  user/group:	     $user:$group
"

[ "$installed_release" != $software_release ] && {
	echo "  installation needed"
	get_and_install
	rm -f $software_file
}


post_install_tasks

install_overwrite	"$TPLDIR/Main" "data/Main"
install_if_not_present	"$TPLDIR/Main-ro" "data/Main"

#[ "$foswiki_company_webname" != "" ] && {
	#web="$foswiki_company_webname"
	#if [ -d "data/$web" ]
	#then
		#install_overwrite	"$TPLDIR/company" "data/$web"
		#install_if_not_present	"$TPLDIR/company-ro" "data/$web"
	#else
		#echo " WARNING: skipping web '$web' customization"
		#echo " create the web from the wiki itself, and then relaunch"
		#echo " this command to complete the customization"
	#fi
#}


echo "  saving configs in '$installused_file'"
save_config >$installused_file

echo "  saving release in '$release_file'"
echo "installed_release=$software_release" >.release

echo "done"
exit 0
