#!/bin/bash
#
# Copyright (C) 2019 OX Software GmbH
# 
# This file is part of OX Automation.
#
# OX Automation is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OX Automation 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OX Automation. If not, see <http://www.gnu.org/licenses/>.
#
#
# Authors:
# Benedikt Kroening <benedikt.kroening@open-xchange.com>
#
set -e
SCRIPT_DIR="$( cd "$(dirname "$0")" ; pwd -P)"
# sourcing some commonly used functions and executing basic checks
source ${SCRIPT_DIR}/../lib/common.bash
source ${SCRIPT_DIR}/../lib/setup.bash
checkRootPrivileges

export INSTALL_GUARD=false
export INSTALL_DOCUMENTS=false

## Help text
show_usage() {
echo -n "
  $0 [OPTIONS]


"
}

### reading config from command line; overriding defaults
POSITIONAL=()
while [[ $# -gt 0 ]]
do
key="$1"

case $key in
    -h|--help)
    show_usage
    exit 0
    shift
    ;;
    --debug)
    echo "WARNING: Debug flag set (--debug)"
    set -x
    shift
    ;;
    --guard)
    INSTALL_GUARD=true
    shift
    ;;
    --documents)
    INSTALL_DOCUMENTS=true
    shift
    ;;
    # -db|--database)
    # DATABASE_TYPE="$2"
    # shift
    # shift
    # ;;
    # --repo-guard)
    # OX_REPO_GUARD="$2"
    # echo "WARNING: Using $OX_REPO_GUARD as guard repository source"
    # shift 
    # shift 
    # ;;
    *)    # unknown option
    POSITIONAL+=("$1") # save it in an array for later
    shift 
    ;;
esac
done
set -- "${POSITIONAL[@]}" # restore positional parameters

# TODO: clean this package list up, some packages are redundant
PACKAGES="open-xchange open-xchange-osgi open-xchange-xerces open-xchange-system open-xchange-core open-xchange-grizzly open-xchange-admin open-xchange-admin-soap open-xchange-authorization-standard open-xchange-authentication-database open-xchange-multifactor open-xchange-passwordchange-database open-xchange-l10n* open-xchange-sessionstorage-hazelcast open-xchange-websockets-grizzly open-xchange-imap open-xchange-pop3 open-xchange-smtp open-xchange-mailfilter open-xchange-push-imapidle open-xchange-dav open-xchange-manage-group-resource open-xchange-subscribe open-xchange-unifiedmail open-xchange-calendar-printing open-xchange-dataretrieval open-xchange-drive open-xchange-drive-comet open-xchange-oauth open-xchange-messaging open-xchange-file-distribution open-xchange-appsuite-manifest open-xchange-appsuite-backend open-xchange-guidedtours open-xchange-appsuite-l10n*"

# nodejs -y \

# # OX Documents packages (including ui backend related)
# open-xchange-realtime-core open-xchange-realtime-events open-xchange-realtime-events open-xchange-realtime-json open-xchange-documents-backend open-xchange-documents-templates open-xchange-documents-ui-common open-xchange-documents-ui-editors open-xchange-pdftool
# # Documentconverter
# open-xchange-documentconverter-api open-xchange-documentconverter-client

if ! [ -x "$(command -v nc)" ]; then
    echo "Adding nc package to list of packages ..."
    PACKAGES="nc ${PACKAGES}"
fi

# Installing nodejs
PACKAGES="nodejs ${PACKAGES}"

# middleware components for guard 
if [ "$INSTALL_GUARD" = true ]; then
    echo "Adding OX Guard packages ..."
    PACKAGES="${PACKAGES} open-xchange-guard-ui open-xchange-rest open-xchange-guard-backend-plugin"
fi

# middleware components for middleware
if [ "$INSTALL_DOCUMENTS" = true ]; then
    echo "Adding OX Documents packages ..."
    PACKAGES="${PACKAGES} open-xchange-realtime-core open-xchange-realtime-events open-xchange-realtime-json open-xchange-documents-backend open-xchange-documents-templates open-xchange-documents-ui-common open-xchange-documents-ui-editors open-xchange-pdftool open-xchange-documentconverter-api open-xchange-documentconverter-client"
fi


installPackages ${PACKAGES}


if [[ "${INSTALL_TYPE}" != "docker" ]]; then


    # Post-install actions
    if [[ "${DIST}" == "DebianJessie" ]]; then
        echo "Nothing to do here..."
        #update-alternatives --set java /usr/lib/jvm/java-8-openjdk-amd64/jre/bin/java 2>/dev/null
    elif [[ "${DIST}" == "CENTOS7" ]] || [[ "${DIST}" == "RHEL7" ]] || [[ "${DIST}" == "SLES12" ]]; then
        chkconfig open-xchange on
    elif [[ "${DIST}" == "CENTOS6" ]] || [[ "${DIST}" == "RHEL6" ]]; then
        chkconfig --level 345 open-xchange on
    fi
fi