${packaging.scripts.header}

#
# This script is executed in the post-installation phase
#
#   On Debian,
#       $1=configure : is set to 'configure' and if $2 is set, it is an upgrade
#
#   On RedHat,
#       $1=0         : indicates a removal
#       $1=1         : indicates an upgrade



# Sets the default values for elasticsearch variables used in this script
ES_USER="${packaging.elasticsearch.user}"
ES_GROUP="${packaging.elasticsearch.group}"

# Source the default env file
ES_ENV_FILE="${packaging.env.file}"
if [ -f "$ES_ENV_FILE" ]; then
    . "$ES_ENV_FILE"
fi

IS_UPGRADE=false

case "$1" in

    # Debian ####################################################
    configure)

        # If $1=configure and $2 is set, this is an upgrade
        if [ -n $2 ]; then
            IS_UPGRADE=true
        fi
    ;;
    abort-upgrade|abort-remove|abort-deconfigure)
    ;;

    # RedHat ####################################################
    1)
        # If $1=1 this is an install
        IS_UPGRADE=false
    ;;
    2)
        # If $1=1 this is an upgrade
        IS_UPGRADE=true
    ;;

    *)
        echo "post install script called with unknown argument \`$1'" >&2
        exit 1
    ;;
esac

if [ "x$IS_UPGRADE" != "xtrue" ]; then
    if command -v systemctl >/dev/null; then
        echo "### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using systemd"
        echo " sudo systemctl daemon-reload"
        echo " sudo systemctl enable elasticsearch.service"
        echo "### You can start elasticsearch service by executing"
        echo " sudo systemctl start elasticsearch.service"

    elif command -v chkconfig >/dev/null; then
        echo "### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using chkconfig"
        echo " sudo chkconfig --add elasticsearch"
        echo "### You can start elasticsearch service by executing"
        echo " sudo service elasticsearch start"

    elif command -v update-rc.d >/dev/null; then
        echo "### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using chkconfig"
        echo " sudo update-rc.d elasticsearch defaults 95 10"
        echo "### You can start elasticsearch service by executing"
        echo " sudo /etc/init.d/elasticsearch start"
    fi
elif [ "$RESTART_ON_UPGRADE" = "true" ]; then

    echo -n "Restarting elasticsearch service..."
    if command -v systemctl >/dev/null; then
        systemctl daemon-reload
        systemctl restart elasticsearch.service || true

    elif [ -x /etc/init.d/elasticsearch ]; then
        if command -v invoke-rc.d >/dev/null; then
            invoke-rc.d elasticsearch stop || true
            invoke-rc.d elasticsearch start || true
        else
            /etc/init.d/elasticsearch restart || true
        fi

    # older suse linux distributions do not ship with systemd
    # but do not have an /etc/init.d/ directory
    # this tries to start the elasticsearch service on these
    # as well without failing this script
    elif [ -x /etc/rc.d/init.d/elasticsearch ] ; then
        /etc/rc.d/init.d/elasticsearch restart || true
    fi
    echo " OK"
fi

${packaging.scripts.footer}
