#!/bin/bash
################################################################################
# Filename: ./src/base/qrm-install
# 
# The contents of this file are subject to the Qlusters Public License
# Version 1.1 (the License); you may not use this file except in compliance
# with the License.
# You may obtain a copy of the License at
# http://www.openqrm.org/content/view/33/69/.
# 
# Software distributed under the License is distributed on an AS IS basis,
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
# the specific language governing rights and limitations under the License.
# 
# The Original Code is openQRM.
# 
# The Initial Developer of the Original Code is Qlusters Corporation. Portions
# created by Qlusters Corporation are Copyright (C) 2003-2006. All Rights
# Reserved.
# 
# Contributor(s):
# 
# Description:
# 
# Comments:
################################################################################

# a function to handle ctrl-c correctly
function download_docs() {
	local DOCS=$QRM_SERVER_BASE_DIR/qrm/docs

	echo -n "Do you want to get the latest manual? (y/n) "
	read CONFIRM
	while ! expr match "$CONFIRM" "[yYnN]$" > /dev/null; do
        	echo -n "Please answer with (y/n): "
	        read CONFIRM
	done

	if ! expr match "$CONFIRM" "[yY]$" > /dev/null; then
		return 0
	fi

	mkdir -p $DOCS
	pushd $DOCS > /dev/null

	local LOG=$(mktemp $QRM_TMPDIR/wget.XXXXXX)

	# some code here is to prevent bash from printing the Terminated message
    # due to a bug in bash (fixed in 3.1) which makes it ignore 'set +m'
	CLEANUP="popd > /dev/null 2>&1; rm $LOG 2>/dev/null; kill \$PID > /dev/null"
	trap "echo ' Interrupted'; $CLEANUP; return 1" sigint sigkill sigterm

	echo -n "Downloading "
	(while :; do echo -n "."; sleep 1; done) 2>/dev/null&
	PID=$!
	disown
	if wget -k -nv -o $LOG --cut-dirs 2 -nH -r -p -np $DOCS_URL; then
		echo " Done"
		RC=0
	else
		echo " Failed"
		cat $LOG
		RC=1
	fi

	eval $CLEANUP
	trap - sigint sigkill sigterm

	return $RC
}


/usr/bin/clear
# Qrm setup procedure

export QRM_SERVER_BASE_DIR=`pushd \`dirname $0\`/../ 1>/dev/null && pwd && popd 1>/dev/null`

. $QRM_SERVER_BASE_DIR/qrm/etc/qrm-install.conf &> /dev/null 

CONFIG_FILE=$QRM_SERVER_BASE_DIR/qrm/etc/qrm.ini

if [ ! -f "$CONFIG_FILE" ]; then
	echo "ERROR: Could not find the main configuration file $CONFIG_FILE"
	exit 1
fi
if [ -f /root/qrm.ini ]; then
	echo "NOTICE: Using /root/qrm.ini configuration file for the installation"
	/bin/cp -f /root/qrm.ini $CONFIG_FILE
fi

# check if a qrm-server is already installed
if [ -h /etc/init.d/qrm-server ]; then
	echo
	echo "WARNING: Qrm-server is already installed on this system"
	echo -en "Do you want to continue with this installation ? (y/n)"
	read CONFIRM
	while [ "$CONFIRM" != "y" ] && [ "$CONFIRM" != "Y" ]; do
		if  [ "$CONFIRM" == "n" ]  || [ "$CONFIRM" == "N" ]; then
			echo
			echo "NOTICE: QRM setup procedure have been canceled"
			echo
			exit 1
		fi
		echo -en "Do you want to continue with this installation ? (y/n)"
		read CONFIRM
	done
fi

# include functions
. $QRM_SERVER_BASE_DIR/qrm/include/qrm-functions
. $QRM_SERVER_BASE_DIR/qrm/include/qrm-setup-functions
. $QRM_SERVER_BASE_DIR/qrm/include/qrm-server-functions
qrm_get_server_config 1>/dev/null

# log
QRM_INSTALLATION_LOGFILE=$QRM_SERVER_LOG_DIR/installation.log
mkdir -p $QRM_SERVER_LOG_DIR
exec 1> >(tee -a $QRM_INSTALLATION_LOGFILE) 2> >(tee -a $QRM_INSTALLATION_LOGFILE)
START_TIME=`date +"%m.%d.%Y_%H.%M.%S"`
echo "$0 started at $START_TIME" >> $QRM_INSTALLATION_LOGFILE
trap "set_install_log_end_time Installation $QRM_INSTALLATION_LOGFILE" SIGKILL
trap "set_install_log_end_time Installation $QRM_INSTALLATION_LOGFILE" EXIT

echo "#####################################################"
echo "#                                                   #"
echo "#                                                   #"
echo "#               Welcome to QRM 2.1                  #"
echo "#                 setup procedure                   #"
echo "#                                                   #"
echo "#                                  by Qlusters Inc  #"
echo "#                                                   #"
echo "#####################################################"
echo "(logfile for the installation at $QRM_INSTALLATION_LOGFILE)"


# check for user root
WHOAMI=`whoami`
if [ "$WHOAMI" != "root" ]; then
	echo "ERROR: You have to be root to install Qrm"
	exit 1
fi

# display and request confirm on the configuration
echo
echo
echo "Please notice"
echo "QRM will be installed with the following configuration :"
echo
echo "General setup:"
echo "--------------"
echo "QRM server interface           :  $QRM_SERVER_INTERFACE"
echo "QRM server base directory      :  $QRM_SERVER_BASE_DIR"

if [ ! -z $QRM_SERVER_LOG_DIR ]; then
	echo "QRM server log directory       :  $QRM_SERVER_LOG_DIR"
fi
if [ ! -z $QRM_NODES_BASE_DIR ]; then
	echo "QRM nodes base directory       :  $QRM_NODES_BASE_DIR"
fi
if [ ! -z $QRM_NODES_LOG_DIR ]; then
	echo "QRM nodes logs directory       :  $QRM_NODES_LOG_DIR"
fi
if [ ! -z $QRM_DEFAULT_KERNEL_RPM ]; then
	echo "QRM default kernel      :  $QRM_DEFAULT_KERNEL_RPM"
fi
echo
echo "Database setup:"
echo "---------------"
echo "IP                      :  $QRM_DATABASE_SERVER"
echo "name                    :  $QRM_DATABASE_NAME"
echo "port                    :  $QRM_DATABASE_PORT"
if [ ! -z $QRM_DATABASE_SOCK ]; then
	echo "socket                  :  $QRM_DATABASE_SOCK"
fi
if [ -z $QRM_DATABASE_USER ]; then
        QRM_DATABASE_USER=${QRM_DATABASE_USER:-root}
fi
echo "username                :  $QRM_DATABASE_USER"
echo "password                :  $QRM_DATABASE_PASSWORD"
echo
echo "Plugins that will be installed:"
echo "-------------------------------"
for PLUGIN in $INSTALL_PLUGINS; do
	echo -e "\t$PLUGIN"
done
echo
echo
echo
echo -en "Do you want to continue with this configuration ? (y/n)"

read CONFIRM
while [ "$CONFIRM" != "y" ] && [ "$CONFIRM" != "Y" ]; do
	if  [ "$CONFIRM" == "n" ]  || [ "$CONFIRM" == "N" ]; then
		echo
		echo "NOTICE: QRM setup procedure have been canceled"
		echo
		exit 1
	fi
	echo -en "Do you want to continue with this configuration ? (y/n)"
	read CONFIRM
done

echo
echo "Starting the QRM-installation"

# stop in case of just an update
if [ -f /etc/init.d/qrm-server ]; then
	/etc/init.d/qrm-server stop 1>/dev/null 2>&1
	/bin/rm -f /etc/init.d/qrm-server
fi

# create log dir
mkdir -p $QRM_SERVER_BASE_DIR/qrm/logs

# qrm-server init script
echo "Installing Qrm-server init script"
ln -sf $QRM_SERVER_BASE_DIR/qrm/etc/init.d/qrm-server /etc/init.d/qrm-server
chmod +x $QRM_SERVER_BASE_DIR/qrm/etc/init.d/qrm-server
qrm_chkconfig add qrm-server

# init the system
if ! $QRM_SERVER_BASE_DIR/qrm/sbin/qrm-admin init-system; then
	echo "ERROR: Could not start the QRM-server. Please check the error above and in the logfile"
	exit 1
fi


download_docs


echo
echo "########################################"
echo "QRM-server setup finished successfully"
echo "########################################"
echo


