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

. `dirname $0`/common

DataDir="$TargetDir/data"
AddonsDir="$TargetDir/addons"
UIDir="$TargetDir/ui"
LogDir="$TargetDir/log"
Log="$LogDir/server.log"
PythonPackageDir="/usr/local/lib/python2.7/site-packages"
PythonPackageFiles="openerp openerp-8.0-py2.7.egg-info"
PidFile="/run/odoo8.pid"
SystemFontPath="/usr/share/fonts"

server_alive() {
	[ -e "$PidFile" ] || return 1

	local PID=$(cat "$PidFile")
	if [ -z "$PID" ]; then
		return 1
	fi

	if [ -e "/proc/$PID" ]; then
		return 0
	fi

	return 1
}

server_ready() {
	wget "http://127.0.0.1:8069/" -T300 -q -O/dev/null
	return $?
}

wait_for_condition() {
	local condition="$@"
	local times=180

	# check if the condition satisfied, try 30 times by default

	while [ $times -gt 0 ]; do
		if eval $condition; then
			break
		fi

		let times=times-1
		sleep 1
	done

	eval $condition
	return $?
}

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 PythonBinPath="/usr/bin/python"

	if [ ! -e "$PythonBinPath" ]; then
		synopkg start Python

		if [ ! -e "$PythonBinPath" ]; then
			echo "Python package is not activated correctly, please restart or reinstall it." > $SYNOPKG_TEMP_LOGFILE
			exit 1
		fi
	fi

	# link openerp onto python package path
	for i in $PythonPackageFiles; do
		rm -f "$PythonPackageDir/$i"
		ln -s "$TargetDir/usr/lib/python2.7/site-packages/$i" "$PythonPackageDir/$i"
	done
}

unlink_python_package()
{
	for i in $PythonPackageFiles; do
		rm -f "$PythonPackageDir/$i"
	done
}

link_files()
{
	local srcdir="$1"
	local dstdir="$2"

	if [ -z "$srcdir" -o -z "$dstdir" ]; then
		exit 1
	fi

	mkdir -p "$dstdir"
	for file in $(ls "$srcdir"); do
		if [ ! -e "$dstdir/$file" ]; then
			ln -sf "$srcdir/$file" "$dstdir/$file"
		fi
	done
}

rm_links()
{
	local srcdir="$1"
	local dstdir="$2"

	if [ -z "$srcdir" -o -z "$dstdir" ]; then
		exit 1
	fi

	for file in $(ls "$srcdir"); do
		if [ -L "$dstdir/$file" ]; then
			rm -f "$dstdir/$file"
		fi
	done
}

link_wkhtmltox()
{
	link_files "$TargetDir/lib" "/usr/lib"
	link_files "$TargetDir/bin" "/usr/local/bin"
}

unlink_wkhtmltox()
{
	rm_links "$TargetDir/lib" "/usr/lib"
	rm_links "$TargetDir/bin" "/usr/local/bin"
}

link_fonts()
{
	link_files "$TargetDir/fonts" "$SystemFontPath"
}

unlink_fonts()
{
	rm_links "$TargetDir/fonts" "$SystemFontPath"
}

case "$1" in
	start)
		if server_alive; then
			echo "OpenERP is already running !" > $SYNOPKG_TEMP_LOGFILE
			exit 0
		fi

		link_python_package
		link_wkhtmltox
		link_fonts

		touch "$PidFile"
		chown ODOO8:ODOO8 "$PidFile"
		su -s /bin/sh -m -c "\"$TargetDir\"/usr/bin/openerp-server --config=\"$TargetDir\"/config/openerp-server.conf &" ODOO8

		if ! wait_for_condition server_alive; then
			exit 1
		fi

		if ! wait_for_condition server_ready; then
			exit 1
		fi

		exit 0
		;;
	stop)
		su -s /bin/sh -m -c "kill $(cat "$PidFile")" ODOO8

		if ! wait_for_condition ! server_alive; then
			exit 1
		fi

		rm -f "$PidFile"
		unlink_fonts
		unlink_wkhtmltox
		unlink_python_package

		exit 0
		;;
	status)
		server_alive
		exit $?
		;;
	log)
		echo "$Log"
		exit 0
		;;
	*)
		echo "Usage: $0 {start|stop|status|log}" >&2
		exit 1
		;;
esac
