#!/bin/sh set -eu APP_NAME="led-manager" APP_DIR="/opt/led-manager" PORT="${PORT:-8088}" HOST="${HOST:-0.0.0.0}" RAW_BASE="${RAW_BASE:-https://gitea.chickliu.fun/Hermes/led-manager-panel/raw/branch/main}" need_root() { if [ "$(id -u)" != "0" ]; then echo "请用 root 运行:su - 或 sudo sh install.sh" >&2 exit 1 fi } has_cmd() { command -v "$1" >/dev/null 2>&1; } download() { url="$1" out="$2" if has_cmd curl; then curl -fsSL "$url" -o "$out" elif has_cmd wget; then wget -qO "$out" "$url" else echo "缺少 curl/wget,无法下载 $url" >&2 exit 1 fi } install_python_if_possible() { if has_cmd python3; then return 0; fi echo "未发现 python3,尝试安装..." if has_cmd opkg; then opkg update opkg install python3 elif has_cmd apt-get; then apt-get update apt-get install -y python3 else echo "未发现 opkg/apt-get,请先手动安装 python3" >&2 exit 1 fi } install_openwrt_service() { echo "检测到 OpenWrt/procd,安装 /etc/init.d/$APP_NAME" download "$RAW_BASE/led-manager.openwrt.init" "/etc/init.d/$APP_NAME" chmod +x "/etc/init.d/$APP_NAME" # Patch host/port in init script if env overrides were provided. sed -i "s/^HOST=.*/HOST=$HOST/" "/etc/init.d/$APP_NAME" || true sed -i "s/^PORT=.*/PORT=$PORT/" "/etc/init.d/$APP_NAME" || true "/etc/init.d/$APP_NAME" enable "/etc/init.d/$APP_NAME" restart || "/etc/init.d/$APP_NAME" start "/etc/init.d/$APP_NAME" status || true } install_systemd_service() { echo "检测到 systemd,安装 led-manager.service" download "$RAW_BASE/led-manager.service" "/etc/systemd/system/$APP_NAME.service" # Patch host/port in service if env overrides were provided. sed -i "s/--host [^ ]* --port [0-9]*/--host $HOST --port $PORT/" "/etc/systemd/system/$APP_NAME.service" || true systemctl daemon-reload systemctl enable --now "$APP_NAME" systemctl restart "$APP_NAME" systemctl status "$APP_NAME" --no-pager || true } need_root install_python_if_possible mkdir -p "$APP_DIR" download "$RAW_BASE/led_panel.py" "$APP_DIR/led_panel.py" chmod +x "$APP_DIR/led_panel.py" if [ -x /sbin/procd ] || [ -d /etc/rc.d ] && has_cmd procd; then install_openwrt_service elif has_cmd systemctl && [ -d /run/systemd/system ]; then install_systemd_service elif [ -d /etc/init.d ] && [ -f /etc/openwrt_release ]; then install_openwrt_service else echo "未检测到 systemd/procd,已安装到 $APP_DIR。手动启动:" echo "python3 $APP_DIR/led_panel.py --host $HOST --port $PORT" fi ipaddr="$(hostname -I 2>/dev/null | awk '{print $1}' || true)" echo echo "安装完成。" echo "访问地址: http://${ipaddr:-设备IP}:$PORT/" echo "如需修改端口:PORT=8090 sh install.sh"