#!/bin/bash
# 根据 /etc/environment 中的 DevSN 设置设备标识
# 包括: hostname, WiFi SSID

set -e

ENV_FILE="/etc/environment"
HOSTNAME_FILE="/etc/hostname"
HOSTS_FILE="/etc/hosts"
HOSTAPD_CONF="/etc/myir-hostapd.conf"

log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a /var/log/aethex_set_identity.log
}

# 读取环境变量
read_env_var() {
    local var_name="$1"
    if [[ -f "${ENV_FILE}" ]]; then
        grep "^${var_name}=" "${ENV_FILE}" 2>/dev/null | cut -d'=' -f2 | tr -d '"' | head -1
    fi
}

# 设置 hostname
set_hostname() {
    local dev_sn="$1"
    local new_hostname="AethEx-${dev_sn}"

    # 获取当前 hostname
    local current_hostname
    current_hostname=$(hostname)

    if [[ "${current_hostname}" == "${new_hostname}" ]]; then
        log "Hostname 已是 ${new_hostname}，无需修改"
        return 0
    fi

    log "设置 hostname: ${current_hostname} -> ${new_hostname}"

    # 修改 /etc/hostname
    echo "${new_hostname}" > "${HOSTNAME_FILE}"

    # 修改 /etc/hosts
    sed -i "s/127.0.1.1.*/127.0.1.1\t${new_hostname}/" "${HOSTS_FILE}" 2>/dev/null || true

    # 立即应用
    hostname "${new_hostname}"

    log "Hostname 已更新为 ${new_hostname}"
}

# 设置 WiFi SSID
set_wifi_ssid() {
    local dev_sn="$1"
    local new_ssid="AethEx-${dev_sn}"

    if [[ ! -f "${HOSTAPD_CONF}" ]]; then
        log "警告: ${HOSTAPD_CONF} 不存在，跳过 WiFi SSID 设置"
        return 1
    fi

    # 获取当前 SSID
    local current_ssid
    current_ssid=$(grep "^ssid=" "${HOSTAPD_CONF}" 2>/dev/null | cut -d'=' -f2 | head -1)

    if [[ "${current_ssid}" == "${new_ssid}" ]]; then
        log "WiFi SSID 已是 ${new_ssid}，无需修改"
        return 0
    fi

    log "设置 WiFi SSID: ${current_ssid} -> ${new_ssid}"

    # 修改配置文件
    sed -i "s/^ssid=.*/ssid=${new_ssid}/" "${HOSTAPD_CONF}"

    # 如果 WiFi 正在运行，需要重启
    if pgrep hostapd &>/dev/null; then
        log "重启 WiFi 服务以应用新 SSID"
        systemctl restart aethex_gpio 2>/dev/null || {
            # 备用方案：直接重启 WiFi AP
            /usr/lib/aethex_gpio/wifi_ap.sh restart 2>/dev/null || true
        }
    fi

    log "WiFi SSID 已更新为 ${new_ssid}"
}

# 设置 WiFi 密码
set_wifi_passwd() {
    local wlan_passwd
    wlan_passwd=$(read_env_var "WLANPasswd")

    if [[ -z "${wlan_passwd}" ]]; then
        log "未设置 WLANPasswd，跳过密码同步"
        return 0
    fi

    if [[ ! -f "${HOSTAPD_CONF}" ]]; then
        log "警告: ${HOSTAPD_CONF} 不存在，跳过 WiFi 密码设置"
        return 1
    fi

    local current_passwd
    current_passwd=$(grep "^wpa_passphrase=" "${HOSTAPD_CONF}" 2>/dev/null | cut -d'=' -f2 | head -1)

    if [[ "${current_passwd}" == "${wlan_passwd}" ]]; then
        log "WiFi 密码已是最新，无需修改"
        return 0
    fi

    log "同步 WiFi 密码"
    sed -i "s/^wpa_passphrase=.*/wpa_passphrase=${wlan_passwd}/" "${HOSTAPD_CONF}"

    log "WiFi 密码已更新"
}

# 主流程
main() {
    log "=== 设置设备标识 ==="

    # 读取 DevSN
    local dev_sn
    dev_sn=$(read_env_var "DevSN")

    if [[ -z "${dev_sn}" ]]; then
        log "错误: 未在 ${ENV_FILE} 中找到 DevSN"
        exit 1
    fi

    log "设备序列号: ${dev_sn}"

    # 设置 hostname
    set_hostname "${dev_sn}"

    # 设置 WiFi SSID
    set_wifi_ssid "${dev_sn}"

    # 设置 WiFi 密码
    set_wifi_passwd

    log "=== 完成 ==="
}

main "$@"
