#!/bin/bash
#
# Copyright (C) 2024 MOXA Inc. All rights reserved.
# This software is distributed under the terms of the MOXA SOFTWARE NOTICE.
# See the file LICENSE for details.
#
# Authors:
#       2024  Elvis Yao  <elviscw.yao@moxa.com>

source "${INSTL_SCRIPT_DIR}/lib/util.sh"
source "${INSTL_SCRIPT_DIR}/lib/logger.sh"
log::init "${INSTL_LOG_FILE}" "${INSTL_LOG_LEVEL}"

set -e

REPO_NAME="moxa-app-wdt-utility"
SRC_PATH_PREFIX="${INSTL_TOP_DIR}/src/${REPO_NAME}"

main() {
        local src_tag
        local src_ver
        src_tag="$1"
        src_ver="$2"

        log::info "Uninstall ${REPO_NAME} ($src_ver)"

        {
                systemctl stop mx-app-wdtd.service
                systemctl stop mx-disable-app-wdt.service
                systemctl disable mx-app-wdtd.service
                systemctl disable mx-disable-app-wdt.service
                rm -f /lib/systemd/system/mx-app-wdtd.service
                rm -f /usr/lib/systemd/system/mx-app-wdtd.service
                rm -f /lib/systemd/system/mx-disable-app-wdt.service
                rm -f /usr/lib/systemd/system/mx-disable-app-wdt.service
                systemctl daemon-reload

                pushd "${SRC_PATH_PREFIX}-${src_tag}" >/dev/null

                make clean
                make uninstall

                popd
        } >>"${INSTL_LOG_FILE}" 2>&1
}

main "$@"
