#!/bin/bash
# OpenMeetings 2.0 Automatic Installer
# Version: 2.1
# Date: 02/08/2012
# Author: Stephen Cottham
# Modified by: Jakub Skory <kuba@ukw.edu.pl>
#
# Report any problems to the usual mail-list

function install {
	echo "Backing up sources.list and adding new repos...";
	mv /etc/apt/sources.list /etc/apt/sources.old;
	echo "deb http://security.debian.org/ squeeze/updates main contrib non-free" >> /etc/apt/sources.list;
	echo "deb-src http://security.debian.org/ squeeze/updates main contrib non-free" >> /etc/apt/sources.list;
	echo "deb http://ftp.debian.org/debian/ squeeze main contrib non-free" >> /etc/apt/sources.list;
	echo "deb-src http://ftp.debian.org/debian/ squeeze main contrib non-free" >> /etc/apt/sources.list;
	echo "deb-src http://ftp.debian.org/debian/ squeeze-updates main contrib non-free" >> /etc/apt/sources.list;
	echo "deb http://ftp2.de.debian.org/debian squeeze main non-free" >> /etc/apt/sources.list;
	echo "deb http://deb-multimedia.org squeeze main" >> /etc/apt/sources.list;
	apt-get update || exit 1;
	
	# debconf
	debconf-set-selections <<< 'sun-java6-jdk shared/accepted-sun-dlj-v1-1 select true';
	debconf-set-selections <<< 'mysql-server-5.1 mysql-server/root_password password password';
	debconf-set-selections <<< 'mysql-server-5.1 mysql-server/root_password_again password password';
	
	# dependencies
	for pkg in expect sun-java6-jdk openoffice.org-writer openoffice.org-calc openoffice.org-impress \
		openoffice.org-draw openoffice.org-math imagemagick gs-gpl \
		libgif-dev xpdf libfreetype6 libfreetype6-dev libjpeg8 libjpeg62 libjpeg8-dev \
		g++ libjpeg-dev libdirectfb-dev libart-2.0-2 libt1-5 zip unzip bzip2 \
		subversion git-core checkinstall yasm texi2html \
		libfaac-dev libfaad-dev libmp3lame-dev libsdl1.2-dev libx11-dev libxfixes-dev libxvidcore4-dev zlib1g-dev \
		libogg-dev sox libvorbis0a libvorbis-dev libgsm1 libgsm1-dev libfaad2 subversion flvtool2 lame mysql-server; do
	echo "Installing $pkg...";
	apt-get install $pkg -y --force-yes || exit 1;
	done

	# creating database (php was 0verkill)
	mysql -u root -ppassword -e "CREATE DATABASE openmeetings DEFAULT CHARACTER SET 'utf8';";
	mysql -u root -ppassword -e "GRANT ALL PRIVILEGES ON openmeetings.* TO \"openmeetings\"@\"localhost\" IDENTIFIED BY \"password\" WITH GRANT OPTION;";
	
	# creating playground
	if ! [ -d "/usr/adm" ]; then
		mkdir /usr/adm;
	fi
	
	# count processors to run as many threads as processors + 1 (make -j ...)
	cpu_count=$((`grep processor /proc/cpuinfo | wc -l`+1));
	
	echo "Compiling and installing SWFTools...";
	cd /usr/adm;
	wget http://www.swftools.org/swftools-2012-04-08-0857.tar.gz;
	tar -zxf swftools-2012-04-08-0857.tar.gz;
	cd swftools-2012-04-08-0857;
	./configure;
	make -j $cpu_count;
	checkinstall -y;

	echo "Compiling and installing FFMpeg...";
	cd /usr/adm;
	wget http://ffmpeg.org/releases/ffmpeg-0.11.1.tar.gz;
	tar -zxf ffmpeg-0.11.1.tar.gz;
	cd ffmpeg-0.11.1;
	./configure --enable-libmp3lame --enable-libxvid --enable-libvorbis --enable-libgsm \
		--enable-libfaac --enable-gpl --enable-nonfree;
	make -j $cpu_count;
	checkinstall -y;
	
	echo "Installing ApacheAnt...";
	cd /usr/adm;
	wget http://mirror.catn.com/pub/apache//ant/binaries/apache-ant-1.8.4-bin.tar.gz;
	tar -zxf apache-ant-1.8.4-bin.tar.gz;
	
	echo "Checking out OM2.0...";
	cd /usr/adm;
	svn checkout http://svn.apache.org/repos/asf/incubator/openmeetings/branches/2.0/;
	cd /usr/adm/2.0;
	
	echo "Compiling and Installing OM 2.0...";
	/usr/adm/apache-ant-1.8.4/bin/ant clean.all;
	/usr/adm/apache-ant-1.8.4/bin/ant -Ddb=mysql;
	
	cd /usr/adm/2.0/dist;
	
	echo '#!/bin/sh
### BEGIN INIT INFO
# Provides: red5
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Starts red5 server for Openmeetings.
### END INIT INFO
# For RedHat and cousins:
# chkconfig: 2345 85 85
# description: Red5 flash streaming server for OpenMeetings
# processname: red5
# Created By: Sohail Riaz (sohaileo@gmail.com)
# Modified by Alvaro Bustos
PROG=red5
RED5_HOME=/usr/lib/red5
DAEMON=$RED5_HOME/$PROG.sh
PIDFILE=/var/run/$PROG.pid
[ -r /etc/sysconfig/red5 ] && . /etc/sysconfig/red5
RETVAL=0
case "$1" in
	start)
		cd $RED5_HOME
		start-stop-daemon --start -c nobody --pidfile $PIDFILE \
			--chdir $RED5_HOME --background --make-pidfile \
			--exec $DAEMON >/dev/null 2>/dev/null &
		RETVAL=$?
		if [ $RETVAL -eq 0 ]; then
			echo $! > $PIDFILE
		fi
		echo
		;;
	stop)
		start-stop-daemon --stop --quiet --pidfile $PIDFILE \
			--name java
		rm -f $PIDFILE
		echo
		[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$PROG
		;;
	restart|force-reload)
		$0 stop
		$0 start
		;;
	status)
		# Debian and Ubuntu 10 status check
		ps aux | grep -f $PIDFILE >/dev/null 2>/dev/null && RETVAL=0 || RETVAL=3
		# Ubuntu 12 status check using improved "start-stop-daemon" status query
		# (use the above command, or comment out above command and uncomment the two
		below commands.
		# start-stop-daemon --status --pidfile $PIDFILE
		# RETVAL=$?
		[ $RETVAL -eq 0 ] && echo "$PROG is running"
		[ $RETVAL -eq 1 ] && echo "$PROG is not running and the pid file exists"
		[ $RETVAL -eq 3 ] && echo "$PROG is not running"
		[ $RETVAL -eq 4 ] && echo "$PROG - unable to determine status"
		;;
	checkports)
		netstat -anp | grep soffice
		netstat -anp | grep java
		;;
	*)
		echo $"Usage: $0 {start|stop|restart|force-reload|status|checkports}"
		RETVAL=1
esac
exit $RETVAL' > red5.initscript;

	chmod +x red5.initscript;
	chown -R nobody red5;
	chmod +x red5/red5.sh;
	chmod +x red5/red5-debug.sh;
	
	mv red5/webapps/openmeetings/WEB-INF/classes/META-INF/persistence.xml \
		red5/webapps/openmeetings/WEB-INF/classes/META-INF/persistence.xml-ori;
	mv red5/webapps/openmeetings/WEB-INF/classes/META-INF/mysql_persistence.xml \
		red5/webapps/openmeetings/WEB-INF/classes/META-INF/persistence.xml;
	
	sed -i 's/Username=root/Username=openmeetings/g' red5/webapps/openmeetings/WEB-INF/classes/META-INF/persistence.xml;
	sed -i 's/Password=/Password=password/g' red5/webapps/openmeetings/WEB-INF/classes/META-INF/persistence.xml;
	
	checkinstall --pkgname red5-openmeetings --pkgversion 2.0 -y \
		sh -c "cp -R /usr/adm/2.0/dist/red5/ /usr/lib/; cp /usr/adm/2.0/dist/red5.initscript /etc/init.d/red5";
	
	echo "Compiling and installing JODConverter...";
	cd /usr/adm;
	wget http://jodconverter.googlecode.com/files/jodconverter-core-3.0-beta-4-dist.zip;
	unzip jodconverter-core-3.0-beta-4-dist.zip;
	checkinstall --pkgname jodconverter-core --pkgversion 3.0-beta-4 -y \
		cp -R /usr/adm/jodconverter-core-3.0-beta-4 /usr/lib/red5/webapps/openmeetings;

	# postinstall 
	update-rc.d red5 defaults;
	/etc/init.d/red5 start;

	echo "Cleaning up after install...";
	apt-get autoremove -y;

	echo "Openmeetings is now installed please open a browser to:";
	echo "http://<OMServerIPaddress>:5080/openmeetings/install";
	echo "press ENTER to continue"
	read;
}

function update {
	echo "Updating Openmeetings to latest Build...";
	echo "Stopping running OM Services..................................................................."
	/etc/init.d/red5 stop
	
	echo "Backing up current OM configuration............................................................"
	mv /usr/adm/backup_om.zip /usr/adm/backup_om.$(date -u +\%Y\%m\%d\%H\%M\%S).zip
	cd /usr/lib/red5
	./admin.sh -b -file /usr/adm/backup_om.zip
	
	# database
	echo "Dropping current Openmeetings Database........................................................."
	mysql -u root -ppassword -e "DROP DATABASE openmeetings;";
	echo "Creating DB and setting Permissions......................................................."
	mysql -u root -ppassword -e "CREATE DATABASE openmeetings DEFAULT CHARACTER SET 'utf8';";
	mysql -u root -ppassword -e 'GRANT ALL PRIVILEGES ON openmeetings.* TO \"openmeetings\"@\"localhost\" IDENTIFIED BY \"password\" WITH GRANT OPTION;';

	echo "Archiving old OM Instance......................................................."
	mv /usr/lib/red5 /usr/lib/red5.$(date -u +\%Y\%m\%d\%H\%M\%S)

	echo "Checking out Latest Build......................................................."
	cd /usr/adm
	rm -Rf /usr/adm/singlewebapp
	svn checkout http://svn.apache.org/repos/asf/incubator/openmeetings/trunk/singlewebapp/

	echo "Installing ApacheAnt............................................................"
	cd /usr/adm
	wget http://mirror.catn.com/pub/apache//ant/binaries/apache-ant-1.8.4-bin.tar.gz
	tar -zxvf apache-ant-1.8.4-bin.tar.gz

	echo "Compiling and Installing latest OpenMeetings Build.............................."
	cd /usr/adm/singlewebapp
	/usr/adm/apache-ant-1.8.4/bin/ant clean.all
	/usr/adm/apache-ant-1.8.4/bin/ant -Ddb=mysql

	mv /usr/adm/singlewebapp/dist/red5/ /usr/lib/
	cp -R /usr/adm/jodconverter-core-3.0-beta-4 /usr/lib/red5/webapps/openmeetings

	clear
	echo "Setting permissions on OM Files................................................."
	echo
	sleep 1
	chown -R nobody /usr/lib/red5
	chmod +x /usr/lib/red5/red5.sh
	chmod +x /usr/lib/red5/red5-debug.sh

	echo "Setting up Om to use MYSQL backend.............................................."
	mv /usr/lib/red5/webapps/openmeetings/WEB-INF/classes/META-INF/persistence.xml \
	/usr/lib/red5/webapps/openmeetings/WEB-INF/classes/META-INF/persistence.xml-ori
	mv /usr/lib/red5/webapps/openmeetings/WEB-INF/classes/META-INF/mysql_persistence.xml \
	/usr/lib/red5/webapps/openmeetings/WEB-INF/classes/META-INF/persistence.xml
	sed -i 's/Username=root/Username=openmeetings/g' /usr/lib/red5/webapps/openmeetings/WEB-INF/classes/META-INF/persistence.xml
	sed -i 's/Password=/Password=password/g' /usr/lib/red5/webapps/openmeetings/WEB-INF/classes/META-INF/persistence.xml
	
	echo "Restoring OM Configuration....................................................."
	cd /usr/lib/red5
	./admin.sh -i -file /usr/adm/backup_om.zip
	sleep 5
	
	echo "starting OM...................................................................."
	/etc/init.d/red5 start
	echo "Cleaning up after install......................................................"
	apt-get autoremove -y

	echo "OpenMeetings has now been updated, press ENTER.................................";
	read;
}

while true; do
	clear
	echo "**************************"
	echo "* OpenMeetings Installer *"
	echo "**************************"
	echo "* [I] Install"
	echo "* [U] Upgrade"
	echo "* [X] Exit"
	echo "**************************"
	echo -n "Enter your menu choice [I, U or X]: "
	read yourch
	case $yourch in
		I) install;;
		U) upgrade;;
		X) exit 0;;
		*) echo "Oopps!!! Please select choice I, U or X";
			echo "Press Enter to continue. . ." ; read ;;
	esac
done
