#!/bin/sh
# Copyright (C) 2000-2011 Synology Inc. All rights reserved.

PkgName="OpenERP6.1"
ProcessName="Openerp-server"
PkgTopDir="/var/packages/${PkgName}"
DesktopDir="/usr/syno/synoman/webman/3rdparty/$PkgName"
OpenERPTarget="${PkgTopDir}/target"
OpenERPUI="${OpenERPTarget}/ui"
PkgLog="${OpenERPTarget}/debug.log"
DSM_INDEX_ADD="/usr/syno/bin/pkgindexer_add"
DSM_INDEX_DEL="/usr/syno/bin/pkgindexer_del"

# 0: alive, 1: not alive
check_process_alive() {
	local name=$1
	if [ `ps w | grep $name | grep -v -E 'status|grep' | wc -l` -gt 0 ]; then
		return 0
	else
		return 1
	fi
}

wait_process() {
	local process=$1
	local secs=30
	local check_term=0

	if [ $# -gt 1 ]; then
		let check_term=$2
	fi

	if [ $# -gt 2 ]; then
		let secs=$3
	fi


	# check if the process is running, wait for 10 secs by default

	while [ $secs -gt 0 ]
	do
		check_process_alive $process
		if [ $? -eq $check_term ]; then
			break
		fi

		echo "check .... $secs"

		let secs=secs-1
		sleep 1
	done

	check_process_alive $process
	if [ $? -eq $check_term ]; then
		return 0
	else
		return 1
	fi
}

kill_process() {
	local process=$1
	local kill_args="-15"
	local CUR_PID=""

	if [ $# -gt 1 ]; then
		kill_args=$2
	fi

	check_process_alive $process

	if [ $? -eq 0 ]; then
		CUR_PID=`ps w | grep $process | grep -v -E 'stop|grep' | head -1 | awk '{print $1}'`
		if [ -n "$CUR_PID" ]; then
			kill $kill_args $CUR_PID
		fi
	else
		echo "$process is not running"
	fi
}

link_python_package()
{
	# [TODO] link python if it's not linked. Package center will only check dependency in installation, not start-up time.
	# remove it if the dependency of packages is fixed in package center ....
	# If DSM is upgraded, system partition will be cleared, so all the symlink will be removed.
	# Once OpenERP is starting before Python (in alphabet order), it could not fond the python package correctly.
	local PYTHON_LINK_TARGET="/usr/bin/python"
	local PythonBinPath=""
	local PythonPackageFolder=""

	if [ ! -e "$PYTHON_LINK_TARGET" ]; then
		env SYNOPKG_PKGDEST=/var/packages/Python/target /var/packages/Python/scripts/start-stop-status start
	fi

	if [ ! -e "`which python`" ] || [ ! -e "$PYTHON_LINK_TARGET" ]; then
		echo "Python package is not activated correctly, please restart or reinstall it."
		exit 1
	fi

	if [ -e "/var/packages/Python/target/usr/local/lib/python2.7/site-packages" ]; then
		PythonPackageFolder="/var/packages/Python/target/usr/local/lib/python2.7/site-packages/openerp"
        else
		PythonPackageFolder="/var/packages/Python/target/usr/lib/python2.7/site-packages/openerp"
	fi

	# link openerp onto python package path
	if [ ! -e $PythonPackageFolder ]; then
		rm -f $PythonPackageFolder
		ln -s ${OpenERPTarget}/openerp $PythonPackageFolder
	fi
}

unlink_openerp_module()
{
	local PythonBinPath=""
	local PythonPackageFolder=""

	if [ ! -e "`which python`" ] ; then
		exit 1
	fi

	if [ -e "/var/packages/Python/target/usr/local/lib/python2.7/site-packages" ]; then
		PythonPackageFolder="/var/packages/Python/target/usr/local/lib/python2.7/site-packages/openerp"
        else
		PythonPackageFolder="/var/packages/Python/target/usr/lib/python2.7/site-packages/openerp"
	fi

	if [ -e $PythonPackageFolder ]; then
		rm -f $PythonPackageFolder
	fi
}

cd $OpenERPTarget

if [ ! -d $OpenERPTarget/logs ]; then
	mkdir -m 777 $OpenERPTarget/logs
fi

case "$1" in
	start)
		# run openerp-server
		if [ -e "$DesktopDir" ]; then
			echo "OpenERP is already running !"
			exit 0
		fi

		# if old OpenERP 6.0 is installed, show message to notify user while installation
		if [ -e "/var/packages/OpenERP" -a "$SYNOPKG_PKG_STATUS" = "INSTALL" ]; then
				file="$SYNOPKG_PKGDEST/ui/texts/$SYNOPKG_DSM_LANGUAGE/strings"
				if [ -r "$file" ]; then
						message="`/bin/get_key_value $file v60_conflict_message`"
				else
						message="For OpenERP 6.0 database is not compatible for OpenERP 6.1, we will leave OpenERP 6.1 as a standalone package."
				fi

				echo "$message" > $SYNOPKG_TEMP_LOGFILE
		fi

		link_python_package

		echo "run openerp service ..."
		# su -m -c "${OpenERPTarget}/bin/openerp-server -r openerp --logfile=${OpenERPTarget}/logs/server.log" admin
		# [workaround] use root to run openerp, remove it if we can solve it
		${OpenERPTarget}/bin/$ProcessName --config=${OpenERPTarget}/Openerp-server.conf --addons-path=${OpenERPTarget}/openerp/addons/ --logfile=${OpenERPTarget}/logs/server.log &
		rm -f $DesktopDir

		wait_process $ProcessName
		if [ $? -ne 0 ]; then
			exit 1
		fi
		sleep 10	# waiting for web client initialization ....

		if [ -n "$SYNOPKG_DSM_VERSION_MAJOR" -a $SYNOPKG_DSM_VERSION_MAJOR -ge 4 ]; then
			ln -sf ${OpenERPUI} $DesktopDir
			${DSM_INDEX_ADD} ${OpenERPUI}/index.conf
		else
			ln -sf ${OpenERPTarget}/desktop $DesktopDir
		fi
		if [ $? -ne 0 ]; then
			exit 1
		fi
		;;
	stop)
		kill_process $ProcessName "-9"
		rm -f $DesktopDir

		wait_process $ProcessName 1

		if [ -n "$SYNOPKG_DSM_VERSION_MAJOR" -a $SYNOPKG_DSM_VERSION_MAJOR -ge 4 ]; then
			${DSM_INDEX_DEL} ${OpenERPUI}/index.conf
		fi

		unlink_openerp_module

		exit 0
		;;
	status)
		if [ -e ${DesktopDir} ]; then
				exit 0
		else
				exit 1
		fi
		;;
	log)
		echo ${PkgLog}
		;;
	*)
		echo "Usage: $0 {start|stop|status|log}" >&2
		exit 1
		;;
esac

exit 0

