#!/bin/bash
#
# ::copy::
# ::maintainer::
#
CMD=$(basename $0)
PACKAGE="ocsng"
PKGDIR="$PRJ/${PACKAGE}"

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 file=
	local target=

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

	echo "  unpacking $src_file ... "
	$src_unpack $src_file || return $?

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

	fix_perms

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

	fix_perms

	return 0
}



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

	# perl modules installation
	#
	echo "  installing / updating perl modules ..."
	cp /dev/null $tmpfile
	cd $instdir/Apache
	echo -e "\npwd=$(pwd)\n" >>$tmpfile

	local prefix="/perl"	# perl modules installed in 'perl' subdir

	perl Makefile.PL PREFIX=$prefix INSTALLPRIVLIB=$prefix \
		INSTALLSITELIB=$prefix INSTALLSITEARCH=$prefix \
		>>$tmpfile 2>&1

	echo -e "\npwd=$(pwd)\nmake\n\n" >>$tmpfile
	make >>$tmpfile 2>&1 || {
		cat $tmpfile
		exit 1
	}
	echo -e "\nmake isntall DESTDIR=$instdir\n\n" >>$tmpfile
	make install DESTDIR=$instdir >>$tmpfile 2>&1 || {
		cat $tmpfile
		exit 1
	}
	make clean >>$tmpfile 2>&1
	rm -f $tmpfile

	jtmkpath $dir_log	$user:$group 770
	jtmkpath $dir_plugins	$user:$group 770

	cd $instdir

	install_file --parse $PKGDIR/dbconfig.inc.php 	$instdir/
	install_file --parse $PKGDIR/dbconfig.inc.php 	$instdir/ocsreports/
	install_file --parse $PKGDIR/ipdiscover-util.pl	$instdir/ocsreports/
	#rm -f $instdir/ocsreports/install.php #?????

	##rm -f *htaccess.txt

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

	fix_perms
}


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


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

# CUSTOMIZABLE DEFINES

dir_log='$instdir/log'


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

src_url='$src_url'
src_release='$src_release'
src_file='$src_file'
src_dir='$src_dir'
src_unpack='$src_unpack'

# 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 ocsreports/index.php ]
then
	is_new_install=false
else
	is_new_install=true
fi

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

echo

# sanity checks
#
check_installdir $instdir ocsng || exit $?
echo	"  instdir	$instdir (OK)"
echo	"  new_install	$is_new_install"
echo	"  user_group	$user:$group"


# defaults
#
src_url=${src_url:-$(kusa-conf $PACKAGE.src_url)}
src_release=${src_release:-$(kusa-conf $PACKAGE.release)}
src_file=${src_file:-$(kusa-conf $PACKAGE.src_file)}
src_dir=${src_dir:-$(kusa-conf $PACKAGE.src_dir)}
src_unpack=${src_unpack:-$(kusa-conf $PACKAGE.src_unpack)}

dir_log=${dir_log:-$instdir/log}
dir_plugins=${dir_plugins:-$instdir/plugins}

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

for val in "$src_url" "$src_release" "$src_file" "$src_unpack" "$src_dir"
do
	[ "$val" == "" ] && {
		error "some install infos are missing"
		exit 1
	}
done
echo	"  src_url	$src_url"
echo	"  src_release	$src_release"
echo	"  src_file	$src_file"
echo	"  src_dir	$src_dir"
echo	"  src_unpack	$src_unpack"
echo	"  dir_log	$dir_log"
echo	"  dir_plugins	$dir_plugins"


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


##export foswiki_datadir="$HERE/data"
##export foswiki_pubdir="$HERE/pub"

##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:    $src_release
  user/group:	     $user:$group
"

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



post_install_tasks

##install_overwrite	"$PKGDIR/Main" "data/Main"
##install_if_not_present	"$PKGDIR/Main-ro" "data/Main"


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

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

echo	"done"
exit 0

