#!/bin/sh

# 1. 禁用 "遇到错误即退出" 的模式，确保脚本即使报错也继续向下执行
set +e

# 2. 初始化日志目录和文件
LOG_DIR="/lzcapp/var/logs/upsmon"
LOG_FILE="$LOG_DIR/upsmon-notify.log"

if [ ! -d "$LOG_DIR" ]; then
  mkdir -p "$LOG_DIR"
fi

if [ ! -f "$LOG_FILE" ]; then
  echo "$(date): 初始化日志文件..." >"$LOG_FILE"
  chmod 777 "$LOG_FILE"
fi

# Display the contents of /etc/passwd
cat /etc/passwd

# Check if the secrets directory exists
if [ -d /run/secrets ]; then
  # Check and retrieve API password if the secret file exists
  if [ -s /run/secrets/$API_SECRET ]; then
    API_PASSWORD=$(cat /run/secrets/$API_SECRET)
  fi

  # Check and retrieve Admin password if the secret file exists
  if [ -s /run/secrets/$ADMIN_SECRET ]; then
    ADMIN_PASSWORD=$(cat /run/secrets/$ADMIN_SECRET)
  fi
fi

# Initial setup if not already done
if [ ! -e /etc/nut/.setup ]; then
  # Copy local UPS configuration if available
  if [ -e /etc/nut/local/ups.conf ]; then
    cp /etc/nut/local/ups.conf /etc/nut/ups.conf
  else
    # Check for SERIAL when using usbhid-ups driver
    if [ -z "$SERIAL" ] && [ "$DRIVER" = "usbhid-ups" ]; then
      echo "** This container may not work without setting SERIAL **"
    fi

    # Generate UPS configuration
    cat <<EOF >>/etc/nut/ups.conf
[$NAME]
  driver = $DRIVER
  port = $PORT
  desc = "$DESCRIPTION"
EOF

    # Append additional configurations if set
    [ -n "$SERIAL" ] && echo "  serial = \"$SERIAL\"" >>/etc/nut/ups.conf
    [ -n "$POLLINTERVAL" ] && echo "  pollinterval = $POLLINTERVAL" >>/etc/nut/ups.conf
    [ -n "$VENDORID" ] && echo "  vendorid = $VENDORID" >>/etc/nut/ups.conf
    [ -n "$SDORDER" ] && echo "  sdorder = $SDORDER" >>/etc/nut/ups.conf
  fi

  # Adjust MAXAGE if not set to 15
  [ "$MAXAGE" -ne 15 ] && sed -i -e "s/^[# ]*MAXAGE [0-9]\+/MAXAGE $MAXAGE/" /etc/nut/upsd.conf

  # Copy local UPS daemon configuration if available
  if [ -e /etc/nut/local/upsd.conf ]; then
    cp /etc/nut/local/upsd.conf /etc/nut/upsd.conf
  else
    # Generate default UPS daemon configuration
    cat <<EOF >>/etc/nut/upsd.conf
LISTEN 0.0.0.0
EOF
  fi

  # Retrieve API password
  if [ -z "$API_PASSWORD" ]; then
    # 加上 || true 防止文件不存在时报错退出
    API_PASSWORD=$(cat /run/secrets/api_password 2>/dev/null || echo "")
  fi

  # Retrieve Admin password
  if [ -z "$ADMIN_PASSWORD" ]; then
    ADMIN_PASSWORD=$(cat /run/secrets/admin_password 2>/dev/null || echo "")
  fi

  # Prepare the user configuration
  UPS_USERS_CONFIG="/etc/nut/upsd.users"

  # Initialize the config file
  cat >$UPS_USERS_CONFIG <<EOF
[$ADMIN_USER]
  password = $ADMIN_PASSWORD
  actions = set
  actions = fsd
  instcmds = all
EOF

  # Append API user configuration only if API_PASSWORD is set
  if [ -n "$API_PASSWORD" ]; then
    cat >>$UPS_USERS_CONFIG <<EOF
[$API_USER]
  password = $API_PASSWORD
  upsmon master
EOF
  fi

  # Copy local UPS monitor configuration if available
  if [ -e /etc/nut/local/upsmon.conf ]; then
    cp /etc/nut/local/upsmon.conf /etc/nut/upsmon.conf
  else
    # Generate default UPS monitor configuration
    cat <<EOF >>/etc/nut/upsmon.conf
MONITOR $NAME@localhost 1 $API_USER $API_PASSWORD $SERVER
RUN_AS_USER $USER
EOF
  fi

  # Mark setup as complete
  touch /etc/nut/.setup
fi

# Set permissions and ownership
chgrp $GROUP /etc/nut/*
chmod 640 /etc/nut/*
mkdir -p -m 2750 /dev/shm/nut
chown $USER:$GROUP /dev/shm/nut
[ -e /var/run/nut ] || ln -s /dev/shm/nut /var/run

# Issue #15 - Change pid warning message from "No such file" to "Ignoring"
echo 0 >/var/run/nut/upsd.pid && chown $USER:$GROUP /var/run/nut/upsd.pid
echo 0 >/var/run/upsmon.pid

echo "=== 正在尝试启动 NUT 服务 ==="

# Start UPS driver and services
# 关键修改：加上 || echo 处理报错，确保脚本继续运行
/usr/sbin/upsdrvctl -u root start || echo "警告: UPS 驱动启动失败（USB 可能未插入）"
/usr/sbin/upsd -u $USER || echo "警告: upsd 启动失败"

# 关键修改：不再使用 exec，而是以后台模式启动 upsmon
/usr/sbin/upsmon -D &
echo "=== 启动指令已完成，容器进入保活状态 ==="

# 终极保活循环
while true; do
  # 检查 upsmon 是否还在，不在了可以尝试重启（可选），或者单纯 tail
  tail -f /dev/null
done
