#!/bin/sh
################################################
### @package JLive! Chat
### @version 4.3.2
### @copyright (C) Copyright 2008-2010 CMS Fruit, CMSFruit.com. All rights reserved.
### @license GNU/LGPL http://www.gnu.org/licenses/lgpl-3.0.txt
###
### This program is free software; you can redistribute it and/or modify
### it under the terms of the GNU Lesser General Public License as published by
### the Free Software Foundation; either version 3 of the License, or (at your
### option) any later version.
###
### This program is distributed in the hope that it will be useful, but
### WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
### or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
### License for more details.
###
### You should have received a copy of the GNU Lesser General Public License
### along with this program.  If not, see http://www.gnu.org/licenses/.
################################################
################################################
################################################
### Please do not modify this file unless you know what you are doing!
################################################

APP_DISPLAY_NAME="JLive! Chat"
SUDO_BIN=`which sudo`
APTITUDE_BIN=`which apt-get`
YUM_BIN=`which yum`
CURL_BIN=`which curl`
UNZIP_BIN=`which unzip`
APP_PY25_SRC="http://d35d7z8muy0qp0.cloudfront.net/JLive_Chat-python2.5.zip"
APP_PY26_SRC="http://d35d7z8muy0qp0.cloudfront.net/JLive_Chat-python2.6.zip"
APP_PY27_SRC="http://d35d7z8muy0qp0.cloudfront.net/JLive_Chat-python2.7.zip"
TARGET_APP_ARCHIVE="JLive_Chat-python-source.zip"
APP_INSTALL_PATH="/opt/jlivechat"

cd ~

install_prereqs () {
    $SUDO_BIN easy_install jsonlib
    $SUDO_BIN easy_install simplejson
    $SUDO_BIN easy_install sgmlop
    $SUDO_BIN easy_install python-dateutil
    $SUDO_BIN easy_install requests
    $SUDO_BIN easy_install sqlalchemy==0.6.9
    $SUDO_BIN easy_install twisted
    $SUDO_BIN easy_install -U twisted

    $CURL_BIN --max-redirs 3 -L http://miniupnp.free.fr/files/download.php?file=miniupnpc-1.5.20110527.tar.gz --O miniupnpc-1.5.20110527.tar.gz
    tar -zxvf miniupnpc-1.5.20110527.tar.gz
    rm -f miniupnpc-1.5.20110527.tar.gz
    cd miniupnpc-1.5.20110527
    make pythonmodule
    $SUDO_BIN make installpythonmodule

    cd ..

    $SUDO_BIN rm -rf miniupnpc-1.5.20110527
}

install_app () {
    local PYTHON_PATH=`which python`
    local PYTHON_VERSION=`$PYTHON_PATH -V 2>&1 | sed 's/Python[[:space:]]\+\([0-9]\+\.[0-9]\+\).*/\1/'`
    local UNZIP_BIN=`which unzip`
    
    rm -f $TARGET_APP_ARCHIVE

    if [ "$PYTHON_VERSION" = "2.5" ]; then
	echo "Downloading $APP_DISPLAY_NAME for Python 2.5"
	$CURL_BIN $APP_PY25_SRC --O $TARGET_APP_ARCHIVE
    elif [ "$PYTHON_VERSION" = "2.6" ]; then
	echo "Downloading $APP_DISPLAY_NAME for Python 2.6"
	$CURL_BIN $APP_PY26_SRC --O $TARGET_APP_ARCHIVE
    elif [ "$PYTHON_VERSION" = "2.7" ]; then
	echo "Downloading $APP_DISPLAY_NAME for Python 2.7"
	$CURL_BIN $APP_PY27_SRC --O $TARGET_APP_ARCHIVE
    else
	echo "Python Version $PYTHON_VERSION is NOT supported! Cannot continue!"
	exit 0
    fi

    $UNZIP_BIN -o $TARGET_APP_ARCHIVE
    rm -f $TARGET_APP_ARCHIVE

    if [ ! -d "/opt" ]; then
	$SUDO_BIN mkdir /opt/
    fi

    if [ -d "$APP_INSTALL_PATH" ]; then
	$SUDO_BIN rm -rf $APP_INSTALL_PATH
    fi
    
    $SUDO_BIN mv JLive\ Chat/ $APP_INSTALL_PATH
    $SUDO_BIN chown -R root.root $APP_INSTALL_PATH
    $SUDO_BIN chmod -R 755 $APP_INSTALL_PATH

    echo "[Desktop Entry]" > ~/Desktop/jlivechat.desktop
    echo "Encoding=UTF-8" >> ~/Desktop/jlivechat.desktop
    echo "Icon=$APP_INSTALL_PATH/jlivechat_48.xpm" >> ~/Desktop/jlivechat.desktop
    echo "Name=JLive! Chat" >> ~/Desktop/jlivechat.desktop
    echo "Comment=Click to start JLive! Chat" >> ~/Desktop/jlivechat.desktop
    echo "Exec=python $APP_INSTALL_PATH/jlivechat.py" >> ~/Desktop/jlivechat.desktop
    echo "Terminal=false" >> ~/Desktop/jlivechat.desktop
    echo "Type=Application" >> ~/Desktop/jlivechat.desktop
    echo "Categories=Application;Network;" >> ~/Desktop/jlivechat.desktop
    echo "StartupNotify=false" >> ~/Desktop/jlivechat.desktop
    
    chmod -R 755 ~/Desktop/jlivechat.desktop
    
    echo "Successfully installed $APP_DISPLAY_NAME to $APP_INSTALL_PATH"
    echo "You can now launch $APP_DISPLAY_NAME by clicking on the desktop shortcut or by issuing the command $APP_INSTALL_PATH/jlivechat in a shell prompt."
}

if [ -s $YUM_BIN ] && [ $YUM_BIN ]; then
    OS_TYPE="redhat"
elif [ -s $APTITUDE_BIN ] && [ $APTITUDE_BIN ]; then
    OS_TYPE="debian"
else
    OS_TYPE=""
fi

if [ -s $SUDO_BIN ] && [ $SUDO_BIN ]; then
    # Sudo is installed
    echo "Starting $APP_DISPLAY_NAME Linux Desktop Application Installer..."
    echo "Using sudo to install. Please enter your user password below and ensure you have sudo access to write to /opt"
else
    echo "sudo command not found!"
    echo ""
    echo "On Redhat based systems, you can install sudo by issuing the following command as root user:"
    echo "\tyum install sudo"
    echo ""
    echo "On Debian based systems, you can install sudo by issuing the following command as root user:"
    echo "\tapt-get install sudo"
    echo ""
    echo "Please install sudo first and then run this installer again. Cannot Continue!"

    exit 0
fi

# Install python dev
if [ $OS_TYPE = "debian" ]; then
    # Debian System
    $SUDO_BIN $APTITUDE_BIN install -y python-dev
    $SUDO_BIN $APTITUDE_BIN install -y gcc
    $SUDO_BIN $APTITUDE_BIN install -y g++
    $SUDO_BIN $APTITUDE_BIN install -y make
    $SUDO_BIN $APTITUDE_BIN install -y bison
elif [ $OS_TYPE = "redhat" ]; then
    #Redhat System
    $SUDO_BIN $YUM_BIN install -y python-dev
    $SUDO_BIN $YUM_BIN install -y gcc
    $SUDO_BIN $YUM_BIN install -y g++
    $SUDO_BIN $YUM_BIN install -y make
    $SUDO_BIN $YUM_BIN install -y bison
fi

if [ -s $CURL_BIN ] && [ $CURL_BIN ]; then
    # curl is installed
    echo "cURL found. Good!"
else
    echo "curl command not found! Attempting to install it..."
    echo ""

    if [ $OS_TYPE = "debian" ]; then
        # Debian System
        $SUDO_BIN $APTITUDE_BIN install -y curl
    elif [ $OS_TYPE = "redhat" ]; then
        #Redhat System
        $SUDO_BIN $YUM_BIN install -y curl
    else
        echo 'Could not install cURL, please install manually!'
        echo "On Redhat based systems, you can install curl by issuing the following command as root user:"
        echo "\tyum install curl"
        echo ""
        echo "On Debian based systems, you can install curl by issuing the following command as root user:"
        echo "\tapt-get install curl"
        echo ""
        echo "Please install curl first and then run this installer again. Cannot Continue!"

        exit 0
    fi
fi

# Supported OS
if [ $OS_TYPE = "debian" ]; then
    # Debian System
    # First resolve any package conflicts
    $SUDO_BIN $APTITUDE_BIN -f install -y

    if [ -s $UNZIP_BIN ] && [ $UNZIP_BIN ]; then
	echo "Zip binaries found. Good!"
    else
	$SUDO_BIN $APTITUDE_BIN install -y unzip
	UNZIP_BIN=`which unzip`
    fi
    
    $SUDO_BIN $APTITUDE_BIN install -y python python-setuptools python-pycurl python-pygame python-openssl make gcc g++ tar
    $SUDO_BIN $APTITUDE_BIN install -y python-qt4-common
    $SUDO_BIN $APTITUDE_BIN install -y python-qt4

    install_prereqs
    install_app
elif [ $OS_TYPE = "redhat" ]; then
    #Redhat System
    if [ -s $UNZIP_BIN ] && [ $UNZIP_BIN ]; then
	echo "Zip binaries found. Good!"
    else
	$SUDO_BIN $YUM_BIN install -y unzip
	UNZIP_BIN=`which unzip`
    fi

    $SUDO_BIN $YUM_BIN install -y python python-setuptools python-pycurl pygame pyOpenSSL make gcc g++ tar PyQt4 python-twisted
    $SUDO_BIN $YUM_BIN install -y python-setuptools-devel

    install_prereqs
    install_app
else
    echo 'Your operating system is not supported! Cannot continue, exiting!'
fi

exit 0

