#!/bin/bash

. ${TOOLKIT}-functions.sh

set -u
set -e

pg_admin=`jtconf pgsql.admin_user`		|| exit_missing_define "pgsql.admin_user"
pg_admin_pw=`jtconf pgsql.admin_pw`		|| exit_missing_define "pgsql.admin_pw"

db_server=`jtconf openerp.db_server`		|| exit_missing_define "openerp.db_server"
db_port=`jtconf openerp.db_port`		|| exit_missing_define "openerp.db_port"
db_name=`jtconf openerp.db_name`		|| exit_missing_define "openerp.db_name"
db_user=`jtconf openerp.db_user`		|| exit_missing_define "openerp.db_user"
db_pass=`jtconf openerp.db_password`		|| exit_missing_define "openerp.db_password"

admin_passwd=`jtconf openerp.admin_passwd`	|| exit_missing_define "openerp.admin_passwd"

run_sql()
{
	local sqlfile=$RUNDIR/pgsql.tmp
	local outfile=$RUNDIR/pgsql.out
	local stat=

	echo "$@" >$sqlfile
	cp /dev/null $outfile

	(
	  export PGHOST="$db_server"
	  export PGPORT="$db_port"
	  export PGUSER="$pg_admin"
	  export PAGER="/bin/cat"

	  (sleep 2 ; echo "$pg_admin_pw" ; sleep 2) \
		| socat - EXEC:"psql -A -W -n -q template1 -f $sqlfile",pty,setsid,ctty 2>&1 \
		| sed -e '/^Password: /d' \
		> $outfile
	)
	if grep 'FATAL:' $outfile
	then
		echo "--pgsql-error-------------------------------------------------------------------------"
		stat=1
	else
		stat=0
	fi
	cat $outfile
	return $stat
}


# install
#
version=$(jtconf openerp.version) || exit $?
instdir=$(jtconf openerp.instdir) || exit $?

owner="openerp:openerp"

create_dir $instdir			$owner 2775
create_dir $instdir/etc			$owner 2775
create_dir $instdir/bin			$owner 2775
create_dir $instdir/local_addons	$owner 2775

[ -s $instdir/etc/desc ] || {
	echo "OpenErp v.$version" >$instdir/etc/desc
}


# install / update config file
#
file="$instdir/etc/openerp-server.conf"

if [ -f $file ]
then
	# first database creation occurred, the file was overwritten
	# by openerp itselft, so we must update, not replace, it
	#
	echo "  check for updates for $file"

	sed	-e "s/^db_server = .*/db_server = $db_server/" \
		-e "s/^db_port = .*/db_port = $db_port/" \
		-e "s/^db_name = .*/db_name = $db_name/" \
		-e "s/^db_user = .*/db_user = $db_user/" \
		-e "s/^db_password = .*/db_password = $db_pass/" \
		-e "s/^admin_passwd = .*/admin_passwd = $admin_passwd/" \
	    $file > $RUNDIR/openerp-server.conf

	installfile $RUNDIR/openerp-server.conf $file openerp:openerp 660 || exit $?
else
	echo "  fresh installation, install basic config file"
	installfile openerp-server-BASE.conf $file openerp:openerp 660 || exit $?
fi


# install openerp package(s)
#
here=`pwd`
cd $instdir
ku-oerp-install
cd $here



# check for user existence / create user
#
echo -n "  create / update db user: $db_user "

echo -n "."
run_sql "SELECT rolname FROM pg_roles WHERE rolname = '$db_user';" \
	| fgrep -q '(0 rows)' && {
		run_sql "CREATE USER $db_user;"
}

echo -n "."
run_sql "ALTER ROLE $db_user WITH LOGIN CREATEDB PASSWORD '$db_pass';" || exit $?

echo -n "."
run_sql "SELECT rolname FROM pg_roles WHERE rolname = '$db_user';" \
	| fgrep -q '(0 rows)' && {
		echo -e "\nerror, something goes wrong with psql"
		exit 1
}
echo " ok"




# init default file
#
file="/etc/default/openerp-server"
[ -s $file ] || {
	installfile openerp-server.default $file root:root 664 || exit $?
}

# install pgsql backup cronjob
file="/etc/cron.d/ku-oerp-backup"
[ -f $file ] || {
	installfile ku-oerp-backup.cron $file root:root 600 || exit $?
}


# populate init.d directory
#
add_init_script 21 openerp-server

$SOMETHING_CHANGED && {
	restart_service openerp-server openerp-server	|| exit $?
}


# cleanup
#
rm -f /usr/sbin/ku-oerp-install

exit 0
