#
# 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

# source the default env file
if [ -f "${path.env}" ]; then
    . "${path.env}"
fi

export OPENSEARCH_PATH_CONF=${OPENSEARCH_PATH_CONF:-${path.conf}}

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
        PACKAGE=deb
    ;;
    abort-upgrade|abort-remove|abort-deconfigure)
        PACKAGE=deb
    ;;

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

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

# to pick up /usr/lib/sysctl.d/opensearch.conf
if command -v systemctl > /dev/null; then
    systemctl restart systemd-sysctl.service || true
fi

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 opensearch service to start automatically using systemd"
        echo " sudo systemctl daemon-reload"
        echo " sudo systemctl enable opensearch.service"
        echo "### You can start opensearch service by executing"
        echo " sudo systemctl start opensearch.service"

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

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

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

    elif [ -x /etc/init.d/opensearch ]; then
        if command -v invoke-rc.d >/dev/null; then
            invoke-rc.d opensearch stop || true
            invoke-rc.d opensearch start || true
        else
            /etc/init.d/opensearch 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 opensearch service on these
    # as well without failing this script
    elif [ -x /etc/rc.d/init.d/opensearch ] ; then
        /etc/rc.d/init.d/opensearch restart || true
    fi
    echo " OK"
fi

# the equivalent code for rpm is in posttrans
if [ "$PACKAGE" = "deb" ]; then
    if [ ! -f "${OPENSEARCH_PATH_CONF}"/opensearch.keystore ]; then
        /usr/share/opensearch/bin/opensearch-keystore create
        chown root:opensearch "${OPENSEARCH_PATH_CONF}"/opensearch.keystore
        chmod 660 "${OPENSEARCH_PATH_CONF}"/opensearch.keystore
        md5sum "${OPENSEARCH_PATH_CONF}"/opensearch.keystore > "${OPENSEARCH_PATH_CONF}"/.opensearch.keystore.initial_md5sum
    else
        if /usr/share/opensearch/bin/opensearch-keystore has-passwd --silent ; then
          echo "### Warning: unable to upgrade encrypted keystore" 1>&2
          echo " Please run opensearch-keystore upgrade and enter password" 1>&2
        else
          /usr/share/opensearch/bin/opensearch-keystore upgrade
        fi
    fi
fi

${scripts.footer}
