#!/bin/sh
#
# Copyright (c) 2018-2021
# Beijing Baolande Software Corporation
# All Rights Reserved
#

PRG="$0"


while [ -h "$PRG" ]; do
    ls=`ls -ld "$PRG"`
    link=`expr "$ls" : '.*-> \(.*\)$'`
    if expr "$link" : '.*/.*' > /dev/null; then
        PRG="$link"
    else
        PRG=`dirname "$PRG"`/"$link"
    fi
done

BASE_HOME=`dirname "$PRG"`
if [ ! -n "$BIN_HOME" ]; then
    BIN_HOME=`cd "$BASE_HOME";pwd`
fi

BES_HOME=`cd "${BIN_HOME}/.."; pwd`
BES_ROOT=$BES_HOME

if [ -r "$BIN_HOME"/setenv.conf ]; then
    . "$BIN_HOME/setenv.conf"
fi

if [ -z "$JAVA_BASE" ]; then
    echo "The JAVA_BASE environment variable is not setted"
    exit 1
fi

if [ ! -f "$BES_HOME"/conf/.passwordfile ]; then
    echo "The default passwordfile is not exist"
    exit 1
fi

LD_LIBRARY_PATH="$BES_HOME"/bin:"$BES_HOME"/lib:/usr/lib/lwp:"$LD_LIBRARY_PATH"
export LD_LIBRARY_PATH

LIBPATH="$BES_HOME"/bin:"$BES_HOME"/lib:/usr/lib/lwp:"$LIBPATH"
export LIBPATH

EXTLIB_PATH="$JAVA_BASE"/lib/ext:"$JAVA_BASE"/jre/lib/ext:"$BES_ROOT"/lib/ext
export EXTLIB_PATH

SET_PATCH_ENV_TYPE=bootstrap
. "${BIN_HOME}/setenv.conf"
. "${BIN_HOME}/setpatchenv.conf"

install_systemd() {
    if [ `id -un` != "root" ]; then
        echo "ERROR: Must be root to install BES Standard service!"
        exit 1
    fi
    cat >> "$BES_HOME"/conf/bes-standard.service << EOF
[Unit]
Description=BES Application Server Standard
After=network-online.target
Wants=network-online.target

[Service]
Type=forking
EnvironmentFile=$BES_HOME/bin/env.properties
Environment="JAVA_HOME=$JAVA_HOME"
ExecStart=$BES_HOME/bin/iastool  start --server --host ###ADMIN_HOST --port ###ADMIN_PORT --user admin --passwordfile $BES_HOME/conf/.passwordfile
ExecStop=$BES_HOME/bin/iastool  stop --server --host ###ADMIN_HOST --port ###ADMIN_PORT --user admin --passwordfile $BES_HOME/conf/.passwordfile
PrivateTmp=true
TimeoutStopSec=60
TimeoutStartSec=600
Restart=on-failure
RestartSec=3

[Install]
WantedBy=multi-user.target

EOF

    if [ $? -ne 0 ]; then
        echo "The bes service wasn't installed fail due to create bes-standard.service fail!"
        return
    fi

    sed -i "s/###/$/g" "$BES_HOME"/conf/bes-standard.service

    if ! cp -af "$BES_HOME"/conf/bes-standard.service /etc/systemd/system/bes-standard.service; then
        rm -f "$BES_HOME"/conf/bes-standard.service
        echo "The bes service wasn't installed fail due to copy bes-standard.service fail!"
        return
    fi
    rm -f "$BES_HOME"/conf/bes-standard.service

    if ! systemctl enable bes-standard.service >/dev/null 2>&1; then
        echo "The bes service wasn't installed fail due to enable bes-standard.service fail!"
        return
    fi

    echo "The bes service was installed into the '/etc/systemd/system/bes-standard.service' successfully."
}

delete() {
    if [ `id -un` != "root" ]; then
        echo "ERROR: Must be root to delete BES Standard service!"
        exit 1
    fi
    if [ -e "/etc/systemd/system/bes-standard.service" ]; then
        systemctl disable bes-standard.service >/dev/null 2>&1
        rm -rf /etc/systemd/system/bes-standard.service
        echo "The bes service was uninstalled from the '/etc/systemd/system/bes-standard.service' successfully."
    fi
}

case "$1" in
    install)
        install_systemd
        ;;

    delete)
        delete
        ;;
    *)
     echo "Usage: $PRG {install|delete}"
     exit 2
esac
exit 0
