#!/usr/bin/env bash

install() {
    echo mysql-server mysql-server/root_password password | debconf-set-selections
    echo mysql-server mysql-server/root_password_again password | debconf-set-selections

    apt-get update -qq

    DEPENDENCIES=(python python-paramiko apache2 libapache2-mod-php5 php5 php5-gd php5-mysql mysql-server unixodbc unixodbc-dev libmyodbc sendmail g++ build-essential libsqlite3-dev)

    PKGSTOINSTALL=""
    for DEPENDENCY in ${DEPENDENCIES[@]}
    do
        # Ubuntu, Debian, etc (with apt)
        if which dpkg &> /dev/null; then
            if [[ ! `dpkg -l | grep -w "ii  ${DEPENDENCY} "` ]]; then
                PKGSTOINSTALL=$PKGSTOINSTALL" "${DEPENDENCY}
            fi
        # OpenSuse, Mandriva, Fedora, CentOs, etc (with rpm)
        elif which rpm &> /dev/null; then
            if [[ ! `rpm -q ${DEPENDENCY}` ]]; then
                PKGSTOINSTALL=$PKGSTOINSTALL" "${DEPENDENCY}
            fi
        # ArchLinux (with pacman)
        elif which pacman &> /dev/null; then
            if [[ ! `pacman -Qqe | grep "${DEPENDENCY}"` ]]; then
                PKGSTOINSTALL=$PKGSTOINSTALL" "${DEPENDENCY}
            fi
        else
            PKGSTOINSTALL=$PKGSTOINSTALL" "${DEPENDENCY}
        fi
    done

    # If some dependencies are missing, asks if user wants to install
    if [ "$PKGSTOINSTALL" != "" ]; then
        echo -n "Some dependencies are missing. Want to install them? (Y/n): "
        read SURE
        # If user want to install missing dependencies
        if [[ $SURE = "Y" || $SURE = "y" || $SURE = "" ]]; then
            # Debian, Ubuntu and derivatives (with apt-get)
            if which apt-get &> /dev/null; then
                apt-get -qq -y install $PKGSTOINSTALL
            # OpenSuse (with zypper)
            elif which zypper &> /dev/null; then
                zypper --quiet in $PKGSTOINSTALL
            # Mandriva (with urpmi)
            elif which urpmi &> /dev/null; then
                urpmi --quiet $PKGSTOINSTALL
            # Fedora and CentOS (with yum)
            elif which yum &> /dev/null; then
                yum --quiet install $PKGSTOINSTALL
            # ArchLinux (with pacman)
            elif which pacman &> /dev/null; then
                pacman --quiet -Sy $PKGSTOINSTALL
            # Else, if no package manager has been founded
            else
                # Set $NOPKGMANAGER
                NOPKGMANAGER=TRUE
                echo "ERROR: impossible to found a package manager in your system. Please, install manually ${DEPENDENCIES[*]}."
            fi
            # Check if installation is successful
            if [[ $? -eq 0 && ! -z $NOPKGMANAGER ]] ; then
                echo "All dependencies are satisfied."
            # Else, if installation isn't successful
            else
                echo "ERROR: impossible to install some missing dependencies. Please, install manually ${DEPENDENCIES[*]}."
            fi
        # Else, if user don't want to install missing dependencies
        else
            echo "WARNING: Some dependencies may be missing. So, please, install manually ${DEPENDENCIES[*]}."
        fi
    else
        echo "All dependencies previously installed."
    fi

    wget --quiet https://www.python.org/ftp/python/3.3.4/Python-3.3.4.tgz &&
    tar -xzf Python-3.3.4.tgz &&
    cd Python-3.3.4 &&
    ./configure --quiet &&
    make &&
    make install
    cd .. &&
    rm Python-3.3.4.tgz

    wget --quiet https://pyodbc.googlecode.com/files/pyodbc-3.0.7.zip &&
    unzip -qq pyodbc-3.0.7.zip &&
    cd pyodbc-3.0.7 &&
    python3 setup.py install
    cd .. &&
    rm pyodbc-3.0.7.zip

    mysql -u root < etc/mysql_create_db.txt

    #if which apache2 &> /dev/null; then
    #    /etc/init.d/apache2 stop
    #    /etc/init.d/apache2 start -f etc/httpd.conf
    #elif which httpd &> /dev/null; then
    #    httpd -k stop
    #    httpd -k start -f etc/httpd.conf
    #else
    #    echo "Could not start the Apache server."
    #fi

    MULTIARCH=$(dpkg-architecture -qDEB_HOST_MULTIARCH)
    echo "[MySQL]
Description = MySQL driver
Driver = /usr/lib/${MULTIARCH}/odbc/libmyodbc.so
Setup = /usr/lib/${MULTIARCH}/odbc/libodbcmyS.so
UsageCount = 1" > /etc/odbcinst.ini

    mkdir /etc/usm
    cp --no-clobber etc/config.json.example /etc/usm/config.json
    
    echo -e '\nConfiguration file located at /etc/usm/config.json\n'
    
    cp -rf src/www /var/.
    cp -rf src/usm /home/.

    chmod 777 /var/www/reportico/projects
    chmod 777 /var/www/reportico/projects/admin
    chmod 777 /var/www/reportico/templates_c
    chmod +x /var/www/command_processors/*.py
    chmod +x /var/www/*.py

    # TODO: move file to cron folder instead of global install - makes it easy to uninstall
    crontab etc/usm.cron
}

remove() {
    # add warning that this deletes entire /var/www (or add usm sub-folder)
    crontab -r
    
    rm -rf /etc/usm
    rm -f /var/log/usm.log
    
    rm -rf /var/www
    rm -rf /home/usm
    
    rm /etc/odbcinst.ini
}

if [ "$1" == "install" ]
then
    install
elif [ "$1" == "remove" ]
then
    remove
else
    echo "Usage: ./$0 [install|remove]"
fi
