#!/bin/sh

# load all the dependancies
apt-get install vim  bison  libcurl4-gnutls-dev libxml2-dev   -y --force-yes
apt-get install byobu debhelper cdbs lintian build-essential fakeroot devscripts -y    # postfix asks for config setup
apt-get install ipsec-tools pbuilder dh-make debootstrap dpatch flex libmysqlclient15-dev ant docbook-to-man bind9 tmux -y
sudo apt-get install subversion -y
 
# work around to get oracle's (sun) JDK not in the repository
cd /home/student/Desktop/
wget --no-cookies --header "Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com" "http://download.oracle.com/otn-pub/java/jdk/7u40-b43/jdk-7u40-linux-x64.tar.gz"  
tar -zxvf jdk-7u40-linux-x64.tar.gz
sudo mv -v jdk1.7.0_* /usr/lib/jvm

# set the Java path - New for LINUX MINT
sudo bash -c "echo JAVA_HOME=/usr/lib/jvm/jdk1.7.0_40 >> /etc/environment"  #Set environment after reboot but won't help right now, so must also use export
sudo chmod ugo=rx /usr/lib/jvm/jdk1.7.0_40/bin/java
export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_40/bin/java  # Set environemental variable right now

# make all the directories
sudo mkdir /opt
sudo mkdir /opt/OpenIMSCore
sudo mkdir /opt/OpenIMSCore/ser_ims
sudo mkdir /opt/OpenIMSCore/FHoSS

# load the SER code into the ser_ims directory, and the HSS managment code into the FHoSS directory
sudo svn checkout http://svn.berlios.de/svnroot/repos/openimscore/ser_ims/trunk   /opt/OpenIMSCore/ser_ims  # Takes some time
sudo svn checkout http://svn.berlios.de/svnroot/repos/openimscore/FHoSS/trunk /opt/OpenIMSCore/FHoSS

cd /opt/OpenIMSCore/ser_ims
make install-libs all

cd /opt/OpenIMSCore/FHoSS
ant compile
ant deploy
cd /opt/OpenIMSCore/FHoSS/deploy
chmod -R 777 .            # Make this directory writable


cd /opt/OpenIMSCore
sudo apt-get install mysql-server -y			# User must press enter 3 times (blue screens - don't set a MySQL password).
echo "You will be asked three times for a password, use arg"
mysql -u root -p -h localhost < ser_ims/cfg/icscf.sql	# When prompted for a password at the terminal CLI, enter your sudo password
mysql -u root -p -h localhost < FHoSS/scripts/hss_db.sql  
mysql -u root -p -h localhost < FHoSS/scripts/userdata.sql

#Make sure student login can access database rather than "hssAdmin"
mysql -uroot -parg <<QUERY_INPUT
GRANT ALL PRIVILEGES ON *.* TO "student"@"localhost" IDENTIFIED BY "arg";
FLUSH PRIVILEGES;
QUERY_INPUT

#Copy startup files to /opt/OpenIMSCore
sudo cp /opt/OpenIMSCore/ser_ims/cfg/*.cfg . 
sudo cp /opt/OpenIMSCore/ser_ims/cfg/*.xml . 
sudo cp /opt/OpenIMSCore/ser_ims/cfg/*.sh .
sudo cp /opt/OpenIMSCore/ser_ims/cfg/open-ims.dnszone /etc/bind
echo 'zone "open-ims.test"  { type master; file "/etc/bind/open-ims.dnszone"; };' >> /etc/bind/named.conf.local 
/etc/init.d/bind9 restart
mkdir /etc/studentconf  
mkdir /etc/studentconf/openimscore

# Copy launchers, scripts, and menu files to where they belong assuming source is in MINT folder on desktop
sudo cp /home/student/Desktop/MINT/OpenIMS/imsCore.sh /etc/studentconf/openimscore
chmod 777 /etc/studentconf/openimscore/imsCore.sh 
chgrp student /etc/studentconf/openimscore/imsCore.sh
sudo cp /home/student/Desktop/MINT/OpenIMS/kill_tmux.desktop  /usr/share/applications
sudo cp /home/student/Desktop/MINT/OpenIMS/StartIMScore.desktop  /usr/share/applications
# sudo cp alta3.menu /etc/xdg/    # Edit this to target correct file once it is discovered

echo " "
echo "OpenIMSCore installation complete! You will need to reboot your machine before booting OpenIMSCore for the first time."
echo " "
read -p "<Press 'Enter' to exit>" waiting

