#!/bin/sh

# 检查参数数量
if [ $# -lt 1 ] || [ $# -gt 2 ]; then
    echo "Usage: $0 <DDNSTO_TOKEN> [DEVICE_NAME]"
    echo "  DDNSTO_TOKEN: Required - Your DDNSTO token"
    echo "  DEVICE_NAME:  Optional - Custom device name (default: auto-generated)"
    echo ""
    echo "Examples:"
    echo "  $0 your_token_here"
    echo "  $0 your_token_here MyRouter"
    exit 1
fi

TOKEN=$1
DEVICE_NAME=$2

# 如果没有提供设备名称，则自动生成一个
if [ -z "$DEVICE_NAME" ]; then
    # 尝试获取主机名作为设备名
    HOSTNAME=$(uci get system.@system[0].hostname 2>/dev/null)
    if [ -n "$HOSTNAME" ]; then
        RANDOM_NUM=$(date +%s | tail -c 4)
        DEVICE_NAME="$HOSTNAME-$RANDOM_NUM"
    else
        # 如果获取不到主机名，使用默认名称加上随机数
        RANDOM_NUM=$(date +%s | tail -c 4)
        DEVICE_NAME="OpenWrt-$RANDOM_NUM"
    fi
    echo "Device name not provided, using auto-generated name: $DEVICE_NAME"
else
    echo "Using provided device name: $DEVICE_NAME"
fi

echo "Configuration:"
echo "  Token: ${TOKEN}..." # 只显示token的前8位
echo "  Device Name: $DEVICE_NAME"
echo ""

echo "Step 1: Installing DDNSTO..."
if command -v curl >/dev/null 2>&1; then
    echo "Using curl to download installer..."
    sh -c "$(curl -sSL https://fw.koolcenter.com/binary/ddnsto/openwrt/ddnstox/install_ddnstox.sh)"
elif command -v wget >/dev/null 2>&1; then
    echo "Using wget to download installer..."
    sh -c "$(wget --no-check-certificate -qO- https://fw.koolcenter.com/binary/ddnsto/openwrt/ddnstox/install_ddnstox.sh)"
else
    echo "Error: Neither curl nor wget is available!"
    exit 1
fi

# 检查安装是否成功
if [ ! -f "/usr/bin/ddnstox" ]; then
    echo "Error: DDNSTO installation failed - ddnstox binary not found!"
    exit 1
fi

echo "Step 2: Configuring DDNSTO..."
uci set ddnstox.config.user_token="$TOKEN"
uci set ddnstox.config.device_name="$DEVICE_NAME"
uci set ddnstox.config.enabled='1'

# 提交配置
if uci commit ddnstox; then
    echo "Configuration saved successfully"
else
    echo "Warning: Failed to save configuration"
fi

# 重启服务
echo "Restarting DDNSTO service..."
if /etc/init.d/ddnstox restart; then
    echo "Service restarted successfully"
else
    echo "Warning: Failed to restart service"
fi

echo "Step 3: Verifying DDNSTO status..."
sleep 5

# 检查进程是否运行
if pgrep -x "ddnstox" >/dev/null; then
    echo "✓ DDNSTO process is running"
else
    echo "✗ Warning: DDNSTO process not found!"
    echo "Checking service status..."
    /etc/init.d/ddnstox status
    exit 1
fi

# 检查配置
echo ""
echo "Current DDNSTO configuration:"
echo "  Enabled: $(uci get ddnstox.config.enabled 2>/dev/null || echo 'unknown')"
echo "  Device Name: $(uci get ddnstox.config.device_name 2>/dev/null || echo 'unknown')"
echo "  Token: $(uci get ddnstox.config.user_token 2>/dev/null | cut -c1-8)..." 2>/dev/null || echo "  Token: unknown"

echo ""
echo "DDNSTO version and device info:"
if [ -f "/usr/sbin/ddnsto" ]; then
    /usr/sbin/ddnsto -w
elif [ -f "/usr/bin/ddnstox" ]; then
    /usr/bin/ddnstox -w 2>/dev/null || echo "Unable to get version info"
else
    echo "DDNSTO binary not found in expected locations"
fi

echo ""
echo "✓ DDNSTO installation and configuration completed successfully!"
echo "You can check the status anytime with: /etc/init.d/ddnstox status"

exit 0