#!/bin/bash

#Install Script for Openmeetings release 1.8 release 4229
#Original idea: Victor Sartori - victor@sartori.eti.br


#Instructions

# The variables: INSTALL_HOME, REP_DEBIAN, SITE and FILE is required
# Adjust the Debian repository for your country
# The variables SITE and FILE works like this:
#  You need to split the URL, cutting the path to file and the file like the example above:
#  the URL: http://openmeetings.googlecode.com/files/openmeetings_1_1_r3087.zip, you need split the path to file, so the
#variable SITE must contain the value: http://openmeetings.googlecode.com/files without the slash at end.
# The variable FILE, will be receive the file will be downloaded like: openmeetings_1_1_r3087.zip

# This is a "Automagic" installer

#This script is designated by DEBIAN 5 USERS! therefore UBUNTU USERS, this script will not work for you, he needs adaptations.

#How to use?
# You need to set the eXecute permission for this script: chmod +x install-om
# and to run: ./install-om simple!


#To Do:
# Make support for others distributions


#ChangeLog
# Added support for the last stable version r3087
# Fixed the issue with videos, now is not needed recompile ffmpeg


#Return codes:

#1 - Without root power
#2 - Variable REP_DEBIAN empty
#3 - Variables SITE and FILE empty
#4 - Variable INSTALL_HOME empty
#5 - Blank passwords for MySQL root user or Openmeetings user
#6 - Unknow Linux system (different of debian)
#7 - SWFTools compile issue

#Questions, suggestions, bugs.... anything, mail-me: victor@sartori.eti.br


# Thats all Folks!!!! enjoy

#--Victor

#--------------------------------------------------------------------------------------------------------------------
# If you have a proxy uncomment the line above
#export http_proxy="http://192.168.27.30:3128/"
BASE_INSTALL=/opt
INSTALL_HOME=/opt/red5
REP_DEBIAN=http://ftp.br.debian.org/debian/
SITE=http://openmeetings.googlecode.com/files
FILE=openmeetings_1_8_0_r4229.zip
LOCAL_INSTALL=$1
LOCAL_FILE=$2

menu() {
        clear
        echo "OpenMeetings \"Automagic\" Install-Script - v0.13 - By Victor"
        echo "-------------------------------------------------------------"
        echo "To use a local install file, use -l <path to openmeetings zip install file>"
        echo "Choose one option:"
        if [ "$LOCAL_INSTALL" = "-l" ]; then
                echo "LOCAL INSTALL. Using OM install zip at: $LOCAL_FILE"
        fi
        echo
        echo "1 - Install"
        echo "2 - Remove (only files, init scripts and database openmeetings)"
        echo "3 - Quit"
        echo
        echo -n "Choose your option: "
        read opcao
        case $opcao in
                1) Install ;;
                2) Remove ;;
                3) exit;;
                *) "Wrong Option" ; sleep 2 ; menu ;;
        esac
}



Install(){
        repositories
        downloads
        copy_files
        database
        mysql_connection
        start_services
}

Remove(){
        rep_original
        del_files
        del_database

}

rep_original() {
        mv -f /etc/apt/sources.list /etc/apt/sources.list.openmeetings
        mv -f /etc/apt/sources.list.orig /etc/apt/sources.list
        aptitude update
}

del_files() {
        /etc/init.d/red5 stop
        /etc/init.d/openoffice stop
        update-rc.d -f red5 remove
        update-rc.d -f openoffice remove
        rm -rf $INSTALL_HOME
}
del_database(){
        echo -n "Type the MySQL root password: "
        read ROOT_MYSQL

        echo "DROP USER openmeetings@localhost;" |mysql -u root -p$ROOT_MYSQL
        echo "DROP DATABASE openmeetings;" |mysql -u root -p$ROOT_MYSQL
}

repositories() {
        if [ -z $REP_DEBIAN ]; then
                echo "The variable REP_DEBIAN is required!"
                echo "Edit this script and fill value with a valid Debian repository."
                echo "The install script will be not completed."
                echo "Error code 2"
                exit 2
        else
                mv -f /etc/apt/sources.list /etc/apt/sources.list.orig
                echo "#Generated by Openmeetings Installer

                deb $REP_DEBIAN stable main contrib non-free
                deb http://security.debian.org/ stable/updates main contrib non-free
                deb-src http://security.debian.org/ stable/updates main contrib non-free
                deb http://ftp.br.debian.org/debian-multimedia/ stable main contrib non-free
                " > /etc/apt/sources.list
                gpg --keyserver subkeys.pgp.net --recv 07DC563D1F41B907
                gpg --export --armor 07DC563D1F41B907 | apt-key add -
                aptitude update
        fi
}

downloads() {

        if [[ -z $SITE || -z $FILE ]]; then
                echo "The variables SITE and FILE is required!"
                echo "Edit this script and fill value."
                echo "At SITE variable, put the only the website, like: site.com/directory (without slash at end)"
                echo "At FILE variable, put the file will be downloaded, like: file.zip"
                echo "The Install Script will be not completed."
                echo "Error code 3"
                exit 3
        else
                aptitude install -y build-essential wget 
                aptitude install -y debian-multimedia-keyring sun-java6-jdk
                aptitude install -y mysql-server openoffice.org-headless openoffice.org-writer openoffice.org-calc openoffice.org-impress openoffice.org-draw openoffice.org-math imagemagick gs-gpl libart-2.0-2 libt1-5 libungif4g 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-0 flvtool2 ffmpeg lame libungif-bin libungif4-dev libungif4g
                aptitude -y full-upgrade

                clear
                wget -q http://www.swftools.org/swftools-0.9.1.tar.gz -O /usr/local/src/swftools-0.9.1.tar.gz
                tar -xzf /usr/local/src/swftools-0.9.1.tar.gz -C /usr/local/src/
                cd /usr/local/src/swftools-0.9.1
                ./configure --prefix=/usr
                make && make install
                if [ $? -ne 0 ]; then
                        echo "Error with swftools compilation - exit code 7"
                        exit 7
                fi
                wget -q http://victor.sartori.eti.br/openmeetings/red5.tar.bz2 -O /tmp/red5.tar.bz2

                if [[ "$LOCAL_INSTALL" == "-l" && -z "$LOCAL_FILE" ]]; then
                        wget $SITE/$FILE
                        copy_files
                else
                        FILE=$LOCAL_FILE
                        copy_files
                fi
        fi
}

copy_files() {
        if [[ -z $INSTALL_HOME || -z $BASE_INSTALL ]]; then
                echo "The INSTALL_HOME and BASE_INSTALL variables, is required, you need to define where the OM will be installed. One tip: /opt/red5 in INSTALL_HOME and /opt in BASE_INSTALL"
                echo "The Install Script will be not completed."
                echo "Error code 4"
                exit 4
        else
                mkdir -p $INSTALL_HOME
                unzip -o $FILE -d $INSTALL_HOME

                chown nobody $INSTALL_HOME -R
                chmod +x $INSTALL_HOME/*.sh
                chmod +x $INSTALL_HOME/webapps/openmeetings/jod/*.sh
                tar -xjf /tmp/red5.tar.bz2 -C /etc/init.d
                update-rc.d red5 defaults
                update-rc.d openoffice defaults
        fi
}


database() {
        clear
        echo -n "Type the password that will be used by user openmeetings at mysql: "
        read MYSQL_OM_USER_PASSWORD
        echo
        echo -n "Type the MySQL root's password: "
        read ROOT_MYSQL

        if [[ -z $MYSQL_OM_USER_PASSWORD || -z $ROOT_MYSQL ]]; then
                echo "Blank passwords for MySQL root is not allowed."
                echo "Is not allowed typing a blank password for the user openmeetings at MySQL."
                echo "The Install Script will be not completed."
                echo "Error code 5"
                exit 5
        else
                echo "CREATE USER openmeetings@localhost;" |mysql -u root -p$ROOT_MYSQL
                echo "CREATE DATABASE openmeetings DEFAULT CHARACTER SET 'utf8';" |mysql -u root -p$ROOT_MYSQL
                echo "GRANT ALL PRIVILEGES ON openmeetings.* TO 'openmeetings'@'localhost' IDENTIFIED BY '$MYSQL_OM_USER_PASSWORD' WITH GRANT OPTION;" |mysql -u root -p$ROOT_MYSQL
                echo "FLUSH PRIVILEGES;" |mysql -u root -p$ROOT_MYSQL
        fi
}



mysql_connection() {
        cp $INSTALL_HOME/webapps/openmeetings/WEB-INF/classes/META-INF/mysql_persistence.xml $INSTALL_HOME/webapps/openmeetings/WEB-INF/classes/META-INF/persistence.xml
        sed -i s/Username=root/Username=openmeetings/g $INSTALL_HOME/webapps/openmeetings/WEB-INF/classes/META-INF/persistence.xml
        sed -i s/Password=/Password=$MYSQL_OM_USER_PASSWORD/g $INSTALL_HOME/webapps/openmeetings/WEB-INF/classes/META-INF/persistence.xml


}


start_services() {
chown nobody $INSTALL_HOME -R
/etc/init.d/openoffice start
sleep 5
/etc/init.d/red5 start
sleep 2
clear
echo "Done!! open the website at your browser: http://$(ip addr |grep inet |grep eth |awk '{print $2;}'|cut -d"/" -f 1):5080/openmeetings/install"
}

check() {
if [ "$(id -u)" -ne "0" ]; then
        echo "You don't have root power!"
        exit 1
else

        if [ -f /etc/debian_version ]; then
                if [ $(cat /etc/debian_version| cut -d . -f 1) == 6 ]; then
                        menu
                else
                        VERSION=$(cat /etc/debian_version)
                        echo "You are not using Debian Squeeze 6"
                        echo "Your version is: "$VERSION
                        echo "Error code 1"
                        exit 1
                fi
        else
                echo "You are not using Debian!"
                echo "Error code 6"
                exit 6
        fi
fi
}
check