#!/bin/bash -vx

#  Usage: apc_install.sh <APC version found on S3>"
#  Example: apc_install.sh apc2.2.380

# Machine setup
export DEBIAN_FRONTEND=noninteractive 
HOSTNAME=`hostname`

if [ $# -ne 1 ]
	then
    echo "APC Install Error: APC Version not supplied."
    echo "Usage: apc_install.sh <APC version found on S3>"
    echo "Example: apc_install.sh apc2.2.380"
    exit -1
fi


# On AWS, you have to add hostname to /etc/hosts else APC-Functional test fails because 'hostname!="localhost"' 
# you have to make /etc/hosts writable by 'ubuntu' first
sudo chmod go+w /etc/hosts
sudo echo "127.0.0.1 $HOSTNAME" >> /etc/hosts
# reset permission back to 'root' 
sudo chmod go-w /etc/hosts

# APC Installation Script
sudo apt-get -y update
sudo apt-get install -y wget
sudo apt-get install -y zip

# install jdk 1.7
sudo mkdir –p /usr/local/java
cd /usr/local/java
JDK_FILE="jdk-7u67-linux-x64.tar.gz"
sudo wget -O $JDK_FILE "https://s3-us-west-2.amazonaws.com/common-provisioning-files/$JDK_FILE"
sudo tar zxf $JDK_FILE
sudo update-alternatives --install "/usr/bin/javac" "javac" "/usr/local/java/jdk1.7.0_67/bin/javac" 1
sudo update-alternatives --install "/usr/bin/java" "java" "/usr/local/java/jdk1.7.0_67/bin/java" 1
sudo update-alternatives --install "/usr/bin/javaws" "javaws" "/usr/local/java/jdk1.7.0_67/bin/javaws" 1
java -version

# set up APC
APC_VER=$1
sudo mkdir -p /opt/$APC_VER
cd /opt/$APC_VER
sudo wget -O $APC_VER.zip "https://s3.amazonaws.com/appvancebuilds/$APC_VER.zip"
if [ ! -e $APC_VER.zip ] 
then 
	echo "APC Install Error: Download of Appvance installation failed! Aborting script."
	exit -1
fi

sudo unzip $APC_VER.zip
sudo chmod -R 0755 .
cd ..
sudo rm -f apc
sudo ln -s $APC_VER apc
sudo wget -O /opt/apc/sahi/userdata/config/browser_types.xml  "https://s3-us-west-2.amazonaws.com/common-provisioning-files/browser_types.xml.linux"

# install mysql

# save mysql configuration inside Debian database via 'debconf-set-selections' command
MYSQL_ROOT_PASSWORD="yeaFI20msB"
DEBIAN_DB_MYSQL_CONFIG="/opt/apc/debconf_config.txt"
sudo touch $DEBIAN_DB_MYSQL_CONFIG
sudo chmod go+w $DEBIAN_DB_MYSQL_CONFIG
sudo echo "mysql-server mysql-server/root_password password $MYSQL_ROOT_PASSWORD" > $DEBIAN_DB_MYSQL_CONFIG
sudo echo  "mysql-server mysql-server/root_password_again password $MYSQL_ROOT_PASSWORD" >> $DEBIAN_DB_MYSQL_CONFIG
sudo debconf-set-selections $DEBIAN_DB_MYSQL_CONFIG
sudo rm -f $DEBIAN_DB_MYSQL_CONFIG

# The mysql config in the Debian Database will be automatically used when mysql-server is setup
sudo apt-get -y install mysql-server
MYSQL_APC_INIT_FILE="/opt/apc/TestNode/webapps/AppvanceServer/WEB-INF/classes/database/MySQLInitial.sql"
if [ ! -e $MYSQL_APC_INIT_FILE ]; then
	echo "APC Install Error: MYSQL Config file for APC not found. Installation failed."
	exit -1
fi
mysql -u root -p$MYSQL_ROOT_PASSWORD < /opt/apc/TestNode/webapps/AppvanceServer/WEB-INF/classes/database/MySQLInitial.sql
mysql -u root -p$MYSQL_ROOT_PASSWORD < /opt/apc/TestNode/webapps/AppvanceServer/WEB-INF/classes/database/MySQLAppvance.sql

#Install firefox
sudo apt-get -y install firefox

# install xvfb for headless FF
sudo apt-get -y install xvfb

# install init.d scripts
cd /etc/init.d
INIT_D_SCRIPTS="init_d_scripts.tgz"
sudo wget -O $INIT_D_SCRIPTS "https://s3-us-west-2.amazonaws.com/common-provisioning-files/$INIT_D_SCRIPTS"
sudo tar zxf $INIT_D_SCRIPTS

# Check
if [ ! -e /etc/init.d/appvance ]; then
	echo "APC Install Error: APC scripts missing in /etc/init.d missing. Installation failed."
	exit -1
fi

# Start the Services
APC_START="sudo service appvance start"
XVFB_START="sudo service appvance_xvfb start"
`$APC_START`
`$XVFB_START`

# append to rc.local so that APC is started automatically on reboot in the future
RC_LOCAL='/etc/rc.local'
EXIT_CMD_PRESENT=`cat $RC_LOCAL| grep -n exit | grep -v '#'`
if [ -z "$EXIT_CMD_PRESENT" ];
then
	# exit cmd not found. append APC start services to rc.local
	sudo chmod o+w $RC_LOCAL
	sudo echo "$APC_START" >> $RC_LOCAL
	sudo echo "$XVFB_START" >> $RC_LOCAL
	sudo chmod o-w $RC_LOCAL
else
	# exit cmd found. insert APC start services before exit cmd in /etc/rc.local
	LINE_NUMBER=`echo $EXIT_CMD_PRESENT|cut -f1 -d':'`
	EXIT_CMD=`echo $EXIT_CMD_PRESENT|cut -f2 -d':'`
	CMDS="$APC_START\n$XVFB_START\n$EXIT_CMD" 
	sudo chmod o+w $RC_LOCAL
	sudo sed -i "$LINE_NUMBER s/.*/$CMDS/" $RC_LOCAL
	sudo chmod o-w $RC_LOCAL
fi
