#!/bin/bash
#2011-now by SISalp http://sisalp.fr contact@sisalp.fr
#license : do what you want with this script
#echo "Installation of xoe"
# adapt parameters below
SCRIPT_VERSION="23_jan_2021-3 UNTESTED"
unix_user="sisalpuser"
erp_name=""
erp_version=""
database_to_save_prefix="pro"


case "$1" in
-*)
	OPTION="$1"
	shift
	;;
esac
#-------------------------------------------------------------------------
CHECK_GET_IN_LIST ()
#-------------------------------------------------------------------------
{
QUESTION="$1"
shift
RESULT="$1"
shift
case "$RESULT" in
:)
	RESULT=""
	;;
*)
	shift
	;;
esac
DEFAULT="$1"
LIST=" $* "
if [ -z "$RESULT" ] ; then
	echo -n "$QUESTION <supported list is $LIST>, default is $DEFAULT : "
	read RESULT
	echo ""
fi
if [ -z "$RESULT" ] ; then
	#echo "$LINENO| $QUESTION is set to default value $DEFAULT"
	RESULT="$DEFAULT"
else
	case "$LIST" in
	*" $RESULT "*)
		;;
	*)
		echo "$QUESTION : answer should be in <$LIST>"
		exit 1
		;;
	esac
fi
}
#-------------------------------------------------------------------------
CHOOSE_VERSION ()
#-------------------------------------------------------------------------
{
if [ ! -f /etc/install_xoe_supported_versions.txt ] ; then
	current_dir=`pwd`
	cd /etc
	wget -q http://download.sisalp.net/scripts/install_xoe_supported_versions.txt
	cd $current_dir
fi
if [ -f /etc/install_xoe_supported_versions.txt ] ; then
	erp_list=`cat /etc/install_xoe_supported_versions.txt | grep "^erp:" | cut -f2- | xargs`
else
	echo "Cannot get last version information for supported erp from the web"
	exit 1
fi
if [ -f /etc/xoe_installed_version.txt ] ; then
	erp_version=`cat /etc/xoe_installed_version.txt | cut -f2`
	erp_name=`cat /etc/xoe_installed_version.txt | cut -f1`
	erp_list="$erp_name"
else
	case "$1" in
	-list)
		;;
	*)
		echo "Please select the ERP family you are looking for and its version"
		CHECK_GET_IN_LIST "Choose ERP family" : $erp_list
		erp_name="$RESULT"
		erp_versions=`cat /etc/install_xoe_supported_versions.txt | grep "^$erp_name:" | cut -f2- | xargs`
		CHECK_GET_IN_LIST "Select $erp_name version" : $erp_versions
		erp_version="$RESULT"
		echo "This script installs erp version : $erp_name $erp_version, <Y> to continue, any key to abort"
		read -n 1 answer
		echo ""
		case "$answer" in
		y|Y)
			;;
		*)
			exit 0
			;;
		esac
		echo "$erp_name	$erp_version" > /etc/xoe_installed_version.txt
		erp_list="$erp_name"
		;;
	esac
fi

}
#-------------------------------------------------------------------------
ONLY ()
#-------------------------------------------------------------------------
{
	#echo "ONLY $*"
	conditions=`echo "$*" | cut -d: -f1`
	action=`echo "$*" | cut -d: -f2- |cut -d" " -f1`
	parameters=`echo "$*" | cut -d: -f2- |cut -d" " -f2-`
	for case_only in $conditions ; do
		case "$erp_name-$erp_version" in
		$case_only*)
			#echo "execute $action : $parameters"
			case "$action" in 
			echo)
				$action "$parameters"
				;;
			*)
				$action $parameters
				;;
			esac
			;;
		esac
	done
}
#-------------------------------------------------------------------------
SET_SERVICE ()
#-------------------------------------------------------------------------
{
	case `whoami` in
	$unix_user)
		echo "`date` $*" >> /tmp/install_xoe.tmp
		echo "$*"
		echo "Automatic installation of $erp_name-server $service_name"
		echo "-------------------------------------------------------"
		case "$erp_name" in
		openerp)
			brand_name="OpenERP"
			;;
		odoo)
			brand_name="Odoo"
			;;
		tryton)
			brand_name="Tryton"
			;;
		esac
		service_password=`/usr/local/bin/xoe  password 7 7 7`
		/usr/local/bin/xoe contract -download
		/usr/local/bin/xoe database -createuser-service $service_name
		/usr/local/bin/xoe config -new -comment $service_name $erp_name added by $0
		/usr/local/bin/xoe contract -new $service_name -
		/usr/local/bin/xoe config -new -role $service_name production
		/usr/local/bin/xoe config -new -documentation $service_name Installation by $0
		/usr/local/bin/xoe config -new -url $service_name $service_name.domain.fqdn
		/usr/local/bin/xoe config -new -pg_user $service_name pg-$service_name
		/usr/local/bin/xoe config -new -database $service_name $database_to_save_prefix save -
		/usr/local/bin/xoe config -new -ports $service_name $ports
		/usr/local/bin/xoe config -new -password $service_name $service_password
		/usr/local/bin/xoe contract -new -configuration $service_name ${brand_name}_$erp_version
		/usr/local/bin/xoe database -createuser-service $service_name
		/usr/local/bin/xoe --environment -set $service_name
		/usr/local/bin/xoe directory -download $service_name
		/usr/local/bin/xoe directory -setup $service_name
		/usr/local/bin/xoe service -signature -set $service_name

		ONLY tryton : /usr/local/bin/xoe database -createdb $service_name ${service_name}_exemple_fr $service_password

		/usr/local/bin/xoe config -set -auto -service $service_name Auto
		/usr/local/bin/xoe start $service_name
		ONLY odoo : /usr/local/bin/xoe --service -cfg -password $service_name		# Print super user password
		echo "`date` Automatic installation of ERP-server $service_name is completed, your new installation is now up and running" >> /tmp/install_xoe.tmp
		;;
	*)
		echo "you must be root or sudo to $unix_user to succeed with `basename $0`"
		echo "use `basename $0` --help to read documentation"
		;;
	esac
}
#-------------------------------------------------------------------------

CHOOSE_VERSION -list
case "$OPTION" in
-v|--version)
	echo "version of $0 : $SCRIPT_VERSION"
	if [ -f /etc/xoe_installed_version.txt ] ; then
		erp_version=`cat /etc/xoe_installed_version.txt | cut -f2`
		erp_name=`cat /etc/xoe_installed_version.txt | cut -f1`
		echo "This script installs $erp_name version : $erp_version"
	fi
	exit 0
	;;
-a|--alternate)
	case `whoami` in
	root)
		if [ -f /etc/xoe_installed_version.txt ] ; then
			rm -f /etc/xoe_installed_version.txt
		fi
		if [ -f /etc/install_xoe_supported_versions.txt ] ; then
			rm -f /etc/install_xoe_supported_versions.txt
		fi
		CHOOSE_VERSION
		;;
	*)
		echo "you must be root or sudo to succeed with `basename $0` $OPTION"
		echo "use `basename $0` --help to read documentation"
		;;
	esac
	exit 0
	;;
-m|--migrate)
	case `whoami` in
	root)
		cd /usr/local/bin
		if [ ! -e /usr/local/bin/xoe ] ; then					# get a copy of xoe script
			wget -q http://download.sisalp.net/scripts/xoe
		else
			/usr/local/bin/xoe --update
		fi
		echo "Previous installation is desactivated"
		;;
	*)
		echo "you must be root or sudo to succeed with `basename $0` -$OPTION"
		echo "use `basename $0` --help to read documentation"
		;;
	esac
	exit 0
	;;
-u|--update)
	case `whoami` in
	root)
		cd /usr/local/bin
		script_source=`basename $0`
		rm -f $0
		wget -q http://download.sisalp.net/scripts/$script_source
		chmod 755 $script_source
		/usr/local/bin/xoe --update
		if [ -f /etc/xoe_installed_version.txt ] ; then
			rm -f /etc/xoe_installed_version.txt
		fi
		if [ -f /etc/install_xoe_supported_versions.txt ] ; then
			rm -f /etc/install_xoe_supported_versions.txt
		fi
		$0 --version
		;;
	*)
		echo "you must be root or sudo to succeed with `basename $0` -$OPTION"
		echo "use `basename $0` --help to read documentation"
		;;
	esac
	exit 0
	;;
-r|--replay)
	case `whoami` in
	root)
		echo "WARNING : You should not replay installation on an already in-use server, this may break your databases, <Y> to continue, any key to abort"
		read -n 1 answer
		echo ""
		case "$answer" in
		y|Y)
			;;
		*)
			exit 0
			;;
		esac

		echo "Stopping ERP services"
		su $unix_user -c "/usr/local/bin/xoe stop" ;

		;;
	*)
		echo "you must be root or sudo to succeed with `basename $0` --replay"
		echo "use `basename $0` --help to read documentation"
		exit 1
		;;
	esac
	;;
-p|--panel)
	case `whoami` in
	root)
		echo "After a first service installation, you can use a graphical web page for basic administration"
		echo "Create an administration account"

		admin_password=`/usr/local/bin/xoe --password 7 14`
		/usr/local/bin/xoe --install -panel $admin_password business
		echo "Panel admin password is : $admin_password"
		exit 0
		;;
	*)
		echo "you must be root or sudo to succeed with `basename $0` --panel"
		echo "use `basename $0` --help to read documentation"
		exit 1
		;;
	esac
	;;
esac
CHOOSE_VERSION
case "$OPTION" in
-h|--help)
	echo "Documentation of `basename $0`
If you are using this script for the first time, please read carefully this documentation :
type q to quit this help
`$0 --version`

Disclaimer: Server administration requires specific skills. Even if this script makes initial installation for you, you need to understand Linux commands and $erp_list components to fix potential problems and manage server maintenance.

This script `basename $0` provides an automatic installation of $erp_list services with the help of xoe.
xoe stands for Extended Open Erp servers management.
xoe is both an initial setup utility and an execution manager for OpenERP, Odoo and Tryton.
xoe can be used both from command line interface or through a web page called administration panel or goe.

`basename $0` is a local linux command, which can be invoked alone or with following options
Options:
-h|--help              print this help
-v|--version           print version
-r|--replay            replay the installation
-t|--training          add a second $erp_list service for training purpose
-s|--test              add another $erp_list service for test purpose
-a|--alternate         choose another version of $erp_list for next steps
-p|--panel             setup a web administration page
-u|--update            auto update code of this script


1_Instructions for setup and start:
***********************************
- login as root or sudoer user. [sudo] means sudo is to be used by sudoer user.
cd /usr/local/bin
[sudo] wget http://download.sisalp.net/scripts/`basename $0`  # Download this script
[sudo] chmod 755 `basename $0`                        # Allow to execute this script
[sudo] `basename $0`                                  # Execute

2_Answer to following questions:
********************************" > /tmp/install_xoe.documentation.txt
	if [ -z "$erp_name" ] ; then
		echo "
Please select the ERP family you are looking for and its version
Choose ERP family <supported list is  $erp_list > : 
Then you will be asked for a supported version" >> /tmp/install_xoe.documentation.txt
	else
		echo "
This script installs erp version : $erp_name $erp_version" >> /tmp/install_xoe.documentation.txt
	fi

	echo "
You will be asked for confirmations during full installation process.

3_Your system is now ready for use
**********************************
[sudo] su $unix_user               # substitute user to $erp_name admin user
xoe --status                         # check the status of services

4_Add an other $erp_name service for training and/or test (optional)
******************************************************************
[sudo] `basename $0` --training             # add a separate training service
[sudo] `basename $0` --test                 # add a separate test service

FAQ:
****
Which platform is required?
This script is designed for servers and virtual servers.
It installs a local postgresql service and cannot use a remote postgresql service.
Directly attached disks are recommended. Network attached storage may result in slow performance.

Which operating system is required?
This script is designed for a fresh install on:
- Ubuntu server 18.04
- Ubuntu server 20.04
before xoeinstallation process, update the system by the commands (recommanded)
[sudo] apt-get update
[sudo] apt-get upgrade

Can I use a desktop version of Debian or Ubuntu?
Yes, nevertheless desktop machines get IP addresses dynamically on the network, while
a static IP address is easier to manage for a server. 

I already have $erp_name installed by another method, can I use this script?
No, this script will probably create conflicts with other installations of $erp_name.

I failed with other installation procedures, can this script repair my server?
This script does not reinstall existing components, and will not repair a broken server.

Which $erp_name server version is installed?
This script currently installs the erp chosen in $erp_list at first execution
You can change this with --alternate option
" >> /tmp/install_xoe.documentation.txt
	echo "I'm an Odoo user, where can I find the super admin password for $erp_name, to create a database?
You can get it by the following commands:
su $unix_user
xoe --service -cfg -password $service_name

Will $erp_name servers restart on boot?
Yes they restart on reboot

Will the system save my $erp_name databases on production service?
Yes, if their names begin with the letters '$database_to_save_prefix'

Can I adapt some parameters to my own needs?
Yes, just edit this script and modify the parameter values but update may eraze your modifications

Will I need this script after this first installation?
No, further administration of you server is done by the xoe script or administration panel

How long will the installation time be?
Approximately 20 minutes (hardware dependent)

Do I have to fill in the IP address of my server?
No, the IP address of the server will be detected and used in this script.

Do I need to create usernames or passwords during installation?
No, those usernames and passwords are created automatically:
- Username and the password of the Linux user that will be used to manage the server
- The super administrator password for Odoo and the administration panel

Do I need a second service for training?
Training service is optional.

How can I use training service?
Your production database is automatically copied to training service every night.
You then can connect to training service and validate your procedures at no risk.

Do I need an other service for tests?
Test service is optional.

How can I use test service?
You can create any database, make any modification to modules, download new versions of code at little risk for your production service.

How can I use another version for my service?
--alternate option allows you to choose your preferred version.

How can I use the graphical administration interface ?
After a first service installation, you can use a graphical web page for basic administration
You can setup this environment with the option --panel with root rights and provide a password.
You can then use a browser and connect to http://my_server_ip_address with user name 'admin' and the password you just created

How tested and safe this script is ?
This script is NOT used by SISalp for its Saas services (xoe is and panel administration is also)
New version is usually tested by users who provide feed-backs.
Nevertheless, install_xoe is only used once for easing initial setup. It cannot prevent other tools to
operate correctly, on a living server.


More on xoe : xoe --help
More on gui engine : goe --help
More on gui user guide : http://sisalp.fr/index.php/post/Hebergement-OpenERP-et-Tryton-par-SISalp-Le-panneau-d-administration-du-serveur-VDS
You can get a fresh copy of this script from http://download.sisalp.net/scripts/`basename $0`
q to quit this help
" >> /tmp/install_xoe.documentation.txt
 	less /tmp/install_xoe.documentation.txt
	rm -f /tmp/install_xoe.documentation.txt
	exit 0
	;;

-t|--training|-s|--test)
	if [ ! -e /etc/xoe_installed_version.txt ] ; then
		echo "You need to complete production service installation before adding a test service"
		echo "History of this installation is not found, maybe because the server was rebooted"
		echo "If you do have a running production service previously installed, <Y> to continue, any key to abort"
		read -n 1 answer
		echo ""
		case "$answer" in
		y|Y)
			;;
		*)
			exit 0
			;;
		esac
	fi
	case `whoami` in
	root)
		su $unix_user -c "$0 $OPTION"			# Switch to new user login
		exit 0
		;;
	esac
	;;
esac
case `whoami` in
root)
	$0 --help
	echo "---------------------------------------------------------"
	echo "Do you think you read carefully this documentation, <Y> to continue, any key to abort"
	read -n 1 answer
	echo ""
	case "$answer" in
	y|Y)
		;;
	*)
		exit 0
		;;
	esac
	echo "`date` The documentation is displayed at the first use of this script" > /tmp/install_xoe.tmp
	chmod 777 /tmp/install_xoe.tmp
	echo "Step 1 _ installation process logs in as root user"
	cd /usr/local/bin					# move to script directory
	if [ ! -e /usr/local/bin/xoe ] ; then					# get a copy of xoe script
		wget -q http://download.sisalp.net/scripts/xoe	
	fi
	chmod 755 /usr/local/bin/xoe						# allow execution of xoe script	
	echo "Check xoe is correct with a basic command"
	/usr/local/bin/xoe --version -blabla					# Print xoe version in verbose mode
	echo "Updates, dependencies, configuration utilities and environment"
	echo "xoe installs all the dependencies and creates the user $unix_user"
	apt-get update
	apt-get upgrade

	echo "`date` Login as root user and install $erp_name dependencies" >> /tmp/install_xoe.tmp
	# Installation of server dependencies
	/usr/local/bin/xoe install -check
	/usr/local/bin/xoe install -commons $unix_user
	/usr/local/bin/xoe install -server -$erp_name $unix_user

	/usr/local/bin/xoe --verbose -blabla
	chown -R $unix_user:$unix_user /home/$unix_user/xoe.conf
	echo "From now on, installation process logs in as user $unix_user"
	echo "---------------------------------------------"
	echo "`date` Step 1 is completed, installation continues connected as $unix_user now" >> /tmp/install_xoe.tmp
	su $unix_user -c "$0 -set"			# Switch to new user login
	/usr/local/bin/xoe --verbose
	;;
$unix_user|*)
	case "$OPTION" in
	-t|--training)
		echo "`date` Configure the system to download and start a training $erp_name service" >> /tmp/install_xoe.tmp
		production_service="production"
		service_name="training"
		case "$$erp_name" in
		tryton)
			ports="8002 8003"
			;;
		*)
			ports="8074 8075"
			;;
		esac
		#production databases are copied every night to training
		/usr/local/bin/xoe --config -new -database $production_service $database_to_save_prefix copy $service_name copy_of_
		SET_SERVICE Configure, download and start $service_name $erp_name service
		;;
	-s|--test)
		echo "`date` Configure the system to download and start a test $erp_name service" >> /tmp/install_xoe.tmp
		service_name="test"
		case "$$erp_name" in
		tryton)
			ports="8004 8005"
			;;
		*)
			ports="8077 8078"
			;;
		esac
		SET_SERVICE Configure, download and start $service_name $erp_name service
		;;
	-set)
		service_name="production"
		case "$$erp_name" in
		tryton)
			ports="8000 8001"
			;;
		*)
			ports="8071 8072"
			;;
		esac
		SET_SERVICE Configure, download and start $service_name $erp_name service
		echo "A database whose name begins with the letters $database_to_save_prefix is automatically saved at night or when $erp_name $service_name service is stopped."
		echo ""
		echo ""
		echo "If you need to replay this script, use : `basename $0` --replay"
		;;
	esac
	;;
esac
exit 0
