#!/bin/bash

# Exit on command errors and treat unset variables as an error
set -eu

# Arguments from manifest
readonly APP=$YNH_APP_INSTANCE_NAME
readonly DOMAIN=$YNH_APP_ARG_DOMAIN
readonly APP_URI=$YNH_APP_ARG_PATH
readonly APP_ADMIN=$YNH_APP_ARG_ADMIN
readonly APP_IS_PUBLIC=$YNH_APP_ARG_IS_PUBLIC
readonly APP_LANGUAGE=$YNH_APP_ARG_LANGUAGE

readonly APP_INSTALL_PATH="/var/www/wekan"
readonly SYSTEMD_CONF_TEMPLATE="$PWD/../conf/systemd.conf"
readonly NGINX_CONF_TEMPLATE="$PWD/../conf/nginx.conf"

readonly METEOR_INSTALL_DIR="/opt/meteor"
readonly NVM_INSTALL_DIR="/opt/nvm"
readonly METEOR_BIN="/usr/local/bin/meteor"

# Source YunoHost helpers
source /usr/share/yunohost/helpers

function configure_app()
{
    # Save app settings
    ynh_app_setting_set "$APP" admin     "$APP_ADMIN"
    ynh_app_setting_set "$APP" is_public "$APP_IS_PUBLIC"
    ynh_app_setting_set "$APP" language  "$APP_LANGUAGE"

    # Check domain/path availability
    sudo yunohost app checkurl "${DOMAIN}${APP_URI}" -a "$APP" \
        || ynh_die "Path not available: ${DOMAIN}${APP_URI}"
}
    
function create_user_wekan()
{
    if [[  -z $(sudo getent passwd wekan) ]]
    then
        sudo useradd wekan
    fi
}

function install_node()
{
    local nvm_install_script="https://raw.githubusercontent.com/creationix/nvm/v0.32.1/install.sh"
  
    if [ ! -d "$NVM_INSTALL_DIR" ];
    then
        sudo mkdir $NVM_INSTALL_DIR
    fi
    sudo chown -R wekan $NVM_INSTALL_DIR
    cd $NVM_INSTALL_DIR

    # Install nvm
    sudo curl -o- $nvm_install_script | sudo NVM_DIR=$NVM_INSTALL_DIR bash

    # Install latest nodejs
    sudo su -c ". $NVM_INSTALL_DIR/nvm.sh && nvm install 0.10"
}

function add_swap_if_needed()
{
    local available_swap=$(free | tail -n 1 | awk '{print $2}')
    local tmp_swap_file=/tmp/wekan_swapfile

    if [ $available_swap -lt 1000000 ];
    then
        # It is NOT possible to setup a swap file on a tmpfs filesystem
        if [[ ! -z $(mount | grep /tmp | grep tmpfs) ]];
        then
            tmp_swap_file=/var/cache/wekan_swapfile
        fi

        sudo dd if=/dev/zero of=$tmp_swap_file bs=1M count=1024
        sudo chmod 600 $tmp_swap_file
        sudo mkswap $tmp_swap_file
        sudo swapon $tmp_swap_file
    fi
}

function install_meteor()
{
    # Install meteor
    if [ ! -d "$METEOR_INSTALL_DIR" ];
    then
        sudo mkdir $METEOR_INSTALL_DIR
    fi
    sudo chown -R wekan $METEOR_INSTALL_DIR
    cd $METEOR_INSTALL_DIR
    sudo su -c "curl https://install.meteor.com/ | sh"
}

function install_and_build_wekan()
{
    local npm_bin=`sudo su -c ". $NVM_INSTALL_DIR/nvm.sh && nvm use 0.10 >/dev/null && which npm"`
    
    # Give all permissions to app dir to user wekan
    if [ -d "$APP_INSTALL_PATH" ];
    then
        sudo rm -rf $APP_INSTALL_PATH
    fi
    sudo mkdir -p $APP_INSTALL_PATH
    sudo chown -R wekan $APP_INSTALL_PATH
    cd $APP_INSTALL_PATH
    
    # Clone wekan github repo
    sudo su wekan -c "git clone https://github.com/wekan/wekan.git ."

    # Install dependencies with npm
    sudo su wekan -c "$npm_bin install"

    # Build with meteor
    sudo rm -rf .build
    sudo su wekan -c "$METEOR_BIN build .build --directory | tee /tmp/meteor_build.log"

    # Install dependencies .. again ?
    cd .build/bundle/programs/server
    sudo su wekan -c "$npm_bin install"
}

function remove_swap()
{
    local file1="/tmp/wekan_swapfile"
    local file2="/var/cache/wekan_swapfile"

    if [ -f $file1 ]; then
	    sudo swapoff $file1
	    sudo rm -f $file1
    fi
    if [ -f $file2 ]; then
        sudo swapoff $file2
        sudo rm -f $file2
    fi
}

function install_mongodb()
{
    # Install mongodb
    sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv EA312927
    echo "deb http://repo.mongodb.org/apt/ubuntu xenial/mongodb-org/3.2 multiverse" | sudo tee /etc/apt/sources.list.d/mongodb-org-3.2.list
    sudo apt-get update
    sudo apt-get install -y mongodb-org=3.2.11 mongodb-org-server=3.2.11 mongodb-org-shell=3.2.11 mongodb-org-mongos=3.2.11 mongodb-org-tools=3.2.11
    sudo systemctl start mongod
    sudo systemctl enable mongod
}

function setup_systemd_service()
{
    # Install systemd conf
    local node_bin=`sudo su -c ". $NVM_INSTALL_DIR/nvm.sh && nvm use 0.10 >/dev/null && which node"`
    sed -i "s@WEKAN_INSTALL_PATH@$APP_INSTALL_PATH/.build/bundle@g" $SYSTEMD_CONF_TEMPLATE
    sed -i "s@WEKAN_NODEJS_PATH@$node_bin@g"                     $SYSTEMD_CONF_TEMPLATE
    sed -i "s@WEKAN_DOMAIN@$DOMAIN@g"                            $SYSTEMD_CONF_TEMPLATE
    sed -i "s@WEKAN_URI@$APP_URI@g"                              $SYSTEMD_CONF_TEMPLATE
    sed -i "s@WEKAN_DB_NAME@wekan@g"                             $SYSTEMD_CONF_TEMPLATE
    sed -i "s@WEKAN_PORT@8081@g"                                 $SYSTEMD_CONF_TEMPLATE
    sudo cp $SYSTEMD_CONF_TEMPLATE /etc/systemd/system/wekan.service

    # Start service
    sudo systemctl daemon-reload
    sudo systemctl start wekan
    sudo systemctl enable wekan
}

function configure_nginx_and_ssowat()
{
    # Modify Nginx configuration file and copy it to Nginx conf directory
    sed -i "s@YNH_WWW_PATH@$APP_URI@g" $NGINX_CONF_TEMPLATE
    sed -i "s@YNH_WWW_ALIAS@$APP_INSTALL_PATH/@g" $NGINX_CONF_TEMPLATE
    sudo cp $NGINX_CONF_TEMPLATE /etc/nginx/conf.d/$DOMAIN.d/$APP.conf

    # If app is public, add url to SSOWat conf as skipped_uris
    if [[ $APP_IS_PUBLIC -eq 1 ]]; then
        # unprotected_uris allows SSO credentials to be passed anyway.
        ynh_app_setting_set "$APP" unprotected_uris "/"
    fi

    sudo service nginx reload
}


configure_app
create_user_wekan
install_node
add_swap_if_needed
install_meteor
install_and_build_wekan
remove_swap
install_mongodb
setup_systemd_service
configure_nginx_and_ssowat