#! /bin/bash
case "$1" in
-s|-e)
	Option="$1"
	shift
	;;
*)
	Option="-s"
	;;
esac
case "$1" in
125|126)
	ServiceIp="10.0.0.$1"
	shift
	;;
*)
	echo "-s or -e then 125 or 126  and [ services list ] ?"
	exit 1
	;;
esac

cd /home/sisalpuser/xoe.services
if [ -z "$*" ] ; then
	ServiceList=`ls | xargs`
else
	ServiceList="$*"
fi
for Service in $ServiceList ; do
	if [ -d $Service ] && [ ! -d $Service/specific ] && xoe --config | grep "^directory:server:$Service:" && ! xoe --config | grep "^directory:specific:$Service:" ; then
		if xoe --config | grep "^ports:$Service:" ; then
			ServicePort=`xoe --config | grep "^ports:$Service:" | cut -f2 | cut -d" " -f1`
		else
			if  xoe --config | grep "^service:$Service:" ; then
				ServicePort=`xoe --config | grep "^service:$Service:" | cut -f6 | cut -d" " -f1`
			else
				echo "$Service port is NOT found"
				ServicePort="not_found"
			fi
		fi
		Report="not_found"
		if xoe --config | grep -q "^directory:server:$Service:.*odoo-server-9.0.uptodate.tar.gz" ; then
			ServiceVersion="9.0"
			if [ -f "$Service/odoo/openerp/addons/report/models/report.py" ] || [ -f "$Service/server/openerp/addons/report/models/report.py" ] ; then
				Report="found"
				if cat $Service/odoo/openerp/addons/report/models/report.py $Service/server/openerp/addons/report/models/report.py 2>/dev/null | grep "http://10.0.0." ; then
					Report="patched"
				fi
			fi
		fi
		if xoe --config | grep -q "^directory:server:$Service:.*odoo-server-10.0.uptodate.tar.gz" ; then
			ServiceVersion="10.0"
			if [ -f "$Service/odoo/addons/report/models/report.py" ] || [ -f "$Service/server/addons/report/models/report.py" ] ; then
				Report="found"
				if cat $Service/odoo/addons/report/models/report.py $Service/server/addons/report/models/report.py 2>/dev/null | grep "http://10.0.0." ; then
					Report="patched"
				fi
			fi
		fi
		case "$Report" in
		found)
			echo "$Service report file is found - $ServiceVersion"
			case "$Option" in
			-e)
				case "$ServicePort" in
				not_found)
					;;
				*)
					echo "EXECUTE FOR $Service"
					#xoe --config -new -directory specific $Service wget http://download.sisalp.net/specific-$ServiceVersion.uptodate.tar.gz specific
					#xoe --config -new -profile $Service report-base-url $ServiceIp:$ServicePort
					#xoe --directory -download $Service specific
					;;
				esac
				;;
			*|-s)
				echo "xoe --config -new -directory specific $Service wget http://download.sisalp.net/specific-$ServiceVersion.uptodate.tar.gz specific"
				echo "xoe --config -new -profile $Service report-base-url $ServiceIp:$ServicePort"
				echo "xoe --directory -download $Service specific"
				;;
			esac
			;;
		patched)
			echo "$Service report file is already patched"
			;;
		*)
			echo "$Service report file is NOT found - $ServiceVersion"
			;;
		esac
	else
		if [ -d $Service/specific ] ; then
			echo "$Service/specific exists"
		fi
		echo "$Service is skipped"
	fi
	echo "----------------------------------------------------------------------------"
done
exit 0

