#!/bin/bash

LOG_FILE="${TRIM_PKGVAR}/info.log"
PID_FILE="${TRIM_PKGVAR}/app.pid"

NODE_BIN="/var/apps/nodejs_v22/target/bin/node"
[ ! -x "${NODE_BIN}" ] && NODE_BIN="$(command -v node || echo node)"
SERVER="${TRIM_APPDEST}/server/server.cjs"

# 从 app.env 读取向导持久化的配置(install_callback/config_callback 写入)
CFG_FILE="${TRIM_PKGETC}/app.env"
HY_PORT=""
HY_DB_DIR=""
if [ -f "${CFG_FILE}" ]; then
    while IFS='=' read -r k v; do
        case "$k" in
            HY_PORT)   HY_PORT="$v" ;;
            HY_DB_DIR) HY_DB_DIR="$v" ;;
        esac
    done < "${CFG_FILE}"
fi
[ -z "${HY_PORT}" ] && HY_PORT="${TRIM_SERVICE_PORT:-12701}"

CMD="${NODE_BIN} ${SERVER}"

log_msg() {
    echo "$(date '+%Y-%m-%d %H:%M:%S') - $1" >> ${LOG_FILE}
}

start_process() {
    if status; then
        return 0
    fi

    log_msg "Starting process (port=${HY_PORT} dbdir=${HY_DB_DIR:-默认}) ..."
    # run cmd to start process
    HY_PORT="${HY_PORT}" HY_DB_DIR="${HY_DB_DIR}" HY_CFG="${CFG_FILE}" \
        bash -c "${CMD}" >> ${LOG_FILE} 2>&1 &
    # write pid to pidfile
    printf "%s" "$!" > "${PID_FILE}"
    sleep 2
    # 启动后确认进程仍在,失败时提示日志位置
    if status; then
        return 0
    fi
    echo "会员服务器启动失败,请查看日志: ${LOG_FILE}" >&2
    return 1
}

stop_process() {
    log_msg "Stopping process ..."

    if [ -r "${PID_FILE}" ]; then
        pid=$(head -n 1 "${PID_FILE}" | tr -d '[:space:]')

        log_msg "pid=${pid}"
        if ! check_process "${pid}"; then
            # process not exist, delete pidfile
            rm -f "${PID_FILE}"
            log_msg "remove pid file 1"
            return
        fi

        log_msg "send TERM signal to PID:${pid}..."
        kill -TERM ${pid} >> ${LOG_FILE} 2>&1

        local count=0
        while check_process "${pid}" && [ $count -lt 10 ]; do
            sleep 1
            count=$((count + 1))
            log_msg "waiting process terminal... (${count}s/10s)"
        done

        if check_process "${pid}"; then
            log_msg "send KILL signal to PID:${pid}..."
            kill -KILL ${pid}
            sleep 1
            rm -f "${PID_FILE}"
        else
            log_msg "process killed... "
        fi
    fi

    return 0
}

check_process() {
    local pid=$1
    if kill -0 "$pid" 2>/dev/null; then
        return 0  # process exist
    else
        return 1
    fi
}

status() {
    if [ -f "${PID_FILE}" ]; then
        pid=$(head -n 1 "${PID_FILE}" | tr -d '[:space:]')
        if check_process "${pid}"; then
            return 0
        else
            # Process is not running but pidfile exists - clean it up
            rm -f "${PID_FILE}"
        fi
    fi

    return 1
}

case $1 in
start)
    # run start command. exit 0 if success, exit 1 if failed
    start_process
    ;;
stop)
    # run stop command. exit 0 if success, exit 1 if failed
    stop_process
    ;;
status)
    # check application status command. exit 0 if running, exit 3 if not running
    if status; then
        exit 0
    else
        exit 3
    fi
    ;;
*)
    exit 1
    ;;
esac
