#!/bin/bash -x
set -eu

RETVAL=0
dir=`dirname $0`
PATH=$dir:/bin:/sbin:/usr/sbin:/usr/bin:/usr/libexec

PFXDIR=/usr/local/pfx
QBDIR=$PFXDIR/qube
PGSQLDIR=$PFXDIR/pgsql
PGSQL_DATA_DIR=$PGSQLDIR/data

PYTHON=/usr/bin/python3

SYSTEMD_DIR='/lib/systemd/system'

if [ ! -e $QBDIR/sbin/supervisor ]
	then
	if [ "$1" != "--prefix" ]
		then
		echo "Please specify a prefix location for qube."
		echo "ex. install_supervisor --prefix /usr/local/pfx/qube"
		exit 1
	fi

	QBDIR=$2

	if [ ! -e $QBDIR/sbin/supervisor ]
		then
		echo "Unable to locate the supervisor $QBDIR/sbin/supervisor."
		exit 1
	fi
fi


install_db() {
	# create the data dir if it doesn't exist
	# if [ ! -d $PGSQL_DATA_DIR ]; then
	# 	mkdir -p $PGSQL_DATA_DIR
	# fi

	# make a backup of postgresql.conf, if it exists, just in case
	if [ -e $PGSQL_DATA_DIR/postgresql.conf ]; then
		echo "Backing up existing postgresql.conf file..."
		cp $PGSQL_DATA_DIR/postgresql.conf $PGSQL_DATA_DIR/postgresql.conf.qubebak.$$
	fi

	# unpack the postgresql binary tarball
	echo "Installing PostgreSQL Server Software..."
	( cd $PFXDIR && tar zxvf postgresql-9.6.6-3-linux-x64-binaries.tar.gz )

	# If it doesn't exist yet, create an OS user "pfx" that will run the pgsql
	# processes. Make it own the pgsql folders and files.
	if ! id -u pfx > /dev/null 2>&1; then
		/usr/sbin/useradd -d /usr/tmp -p 9cknC54VOGDog pfx
	fi
	chown -R pfx $PGSQLDIR

	# run initdb to initialize the data folder
	set +e
	su pfx -c "$PGSQLDIR/bin/initdb -D $PGSQL_DATA_DIR"
	set -e
	
	# run a script to edit the postgresql.conf file to inject settings that
	# the qube supervisor needs
	su pfx -c "$PYTHON $QBDIR/utils/pgsql/configure_postgresql_conf.py $PFXDIR/pgsql/data/postgresql.conf"

	# run a script to edit the pg_hba.conf file to inject settings that
	# the qube supervisor needs
	su pfx -c "$PYTHON $QBDIR/utils/pgsql/configure_pg_hba_conf.py $PFXDIR/pgsql/data/pg_hba.conf"

	# start the postgresql DB server
	# ./startdb.sh
	su pfx -c "$QBDIR/utils/pgsql/startdb.sh"

	sleep 3

	# run DB initializing scripts
	su pfx -c "$PYTHON $QBDIR/utils/pgsql/init_supe_db.py"

	# stop the postgresql DB server, can confuse the systemctl later
	su pfx -c "$QBDIR/utils/pgsql/stopdb.sh"
	sleep 3

	# OOGA! TODO: do we need something similar to the following for
	# postgresql?

	# if [ -e /etc/security/limits.d ]
	#     then
	# 	echo "Configuring mysql user for a 64K file handles ulimit..."
	#     cp ../etc/limits.d/mysql.conf /etc/security/limits.d
	# fi

	# # echo ">>>> INIT_DIR=$INIT_DIR"
	# # echo ">>>> SYSTEMD_DIR=$SYSTEMD_DIR"


	if [ -e $SYSTEMD_DIR/postgresql-pfx.service ]
	then
		# CentOS/RHEL 7+ uses systemd 
        systemctl daemon-reload
		# systemctl stop postgresql-pfx.service
		# sleep 3
		echo "Configuring postgresql-pfx systemd..."
		systemctl enable postgresql-pfx.service
		systemctl start postgresql-pfx.service
	else
	    chmod 755 $INIT_DIR/postgresql-pfx
		/sbin/chkconfig --add postgresql-pfx
		/sbin/chkconfig postgresql-pfx on
		# /sbin/service postgresql-pfx stop
		# sleep 3
		/sbin/service postgresql-pfx start
	fi
	sleep 3

        echo "Running install_datawarehouse_db.sh ..."
	$QBDIR/datawh/install_datawarehouse_db.sh
}

install_supervisor() {
	# unpack the python lib tarball
	echo "Installing Python libs used by embedded interpreter..."
	(cd $QBDIR/lib && tar zxvf python3.8.tar.gz)

	# delete legacy copy of supervisor.service, if found
    if [ -e /etc/systemd/system/supervisor.service ]
	then
		rm -f /etc/systemd/system/supervisor.service
	fi
	
    if [ -e $SYSTEMD_DIR/supervisor.service ]
	then
		systemctl stop supervisor

		# create the "qubesupe" user, which runs the supervisor daemon
		if ! grep qubesupe /etc/passwd > /dev/null
		then 
		    /usr/sbin/useradd -d /usr/tmp qubesupe
		fi

		systemctl daemon-reload
		systemctl enable supervisor
		systemctl start supervisor

		if [ -e /etc/logrotate.d ]
	    then
			cp ../etc/logrotate.d/supervisor /etc/logrotate.d
		fi
    else
	    if [ -e $INIT_DIR/supervisor ]
		then
			$INIT_DIR/supervisor stop
	    fi

		#    QBDIR=`pwd | sed 's/\(.*\/qube\).*/\1/'`
	    sed "s%QBDIR=%QBDIR=$QBDIR%" < $QBDIR/etc/init.d/supervisor > $INIT_DIR/supervisor
	    chmod 755 $INIT_DIR/supervisor
	    if [ ! -e $INIT_DIR/supervisor ] 
		then
			echo "ERROR: problems installing supervisor init..."
			return 1
	    fi

		# create the "qubesupe" user, which runs the supervisor daemon
	    if ! grep qubesupe /etc/passwd > /dev/null
		then 
			/usr/sbin/useradd -d /usr/tmp qubesupe
		fi

	    /sbin/chkconfig --add supervisor
	    /sbin/chkconfig supervisor on
	    $INIT_DIR/supervisor start

	    if [ -e /etc/logrotate.d ]
	    then
			cp ../etc/logrotate.d/supervisor /etc/logrotate.d
	    fi
    fi
}

make_directories()
{
    [ ! -d /var/spool/qube ] && install -m 0700 -d /var/spool/qube
    [ ! -d /var/spool/qube/backup ] && install -m 0700 -d /var/spool/qube/backup
    [ ! -d /var/spool/qube/job ] && install -m 0700 -d /var/spool/qube/job
    [ ! -d /var/spool/qube/user ] && install -m 0700 -d /var/spool/qube/user
    [ ! -d /var/spool/qube/host ] && install -m 0700 -d /var/spool/qube/host
    return 0
}

if [ -e /etc/rc.d/init.d ]
then
    INIT_DIR='/etc/rc.d/init.d'
elif [ -e /etc/init.d ]
then
    INIT_DIR='/etc/init.d'
fi

echo "Making directories..."
make_directories

echo "Installing db..."
install_db

echo "Installing supervisor service and init scripts..."
install_supervisor

echo "Done!"
exit $RETVAL



