#!/bin/bash

# 颜色定义
red='\033[0;31m'
green='\033[0;32m'
yellow='\033[0;33m'
blue='\033[0;34m'
plain='\033[0m'

# 获取系统架构
arch=$(dpkg --print-architecture)

# 根据架构选择对应的下载链接
if [ "$arch" == "arm64" ]; then
  ddns_url="https://cloud.fnmqs.work/ddns-go/ddns-go_linux_arm64.tar.gz"
elif [ "$arch" == "amd64" ]; then
  ddns_url="https://cloud.fnmqs.work/ddns-go/ddns-go_linux_amd64.tar.gz"
elif [ "$arch" == "armhf" ]; then
  ddns_url="https://cloud.fnmqs.work/ddns-go/ddns-go_linux_armhf.tar.gz"
else
  echo -e "${red}不支持当前架构：${arch}，请检查系统是否为支持的架构（arm64, amd64, armhf）${plain}"
  exit 1
fi

# 下载并解压
echo -e "${yellow}正在下载并解压 ddns-go...${plain}"
wget -N --no-check-certificate "${ddns_url}" -O /tmp/ddns-go.tar.gz
if [ $? -ne 0 ]; then
  echo -e "${red}下载失败，请检查网络连接或下载地址${plain}"
  exit 1
fi

tar -xzvf /tmp/ddns-go.tar.gz -C /tmp
if [ $? -ne 0 ]; then
  echo -e "${red}解压失败${plain}"
  exit 1
fi

# 创建安装目录并移动文件
mkdir -p /ddnsgo
mv /tmp/ddns-go /ddnsgo
cd /ddnsgo
chmod 777 ddns-go

# 安装
echo -e "${yellow}正在安装 DDNS-Go...${plain}"
sudo ./ddns-go -s install
if [ $? -ne 0 ]; then
  echo -e "${red}安装失败${plain}"
  exit 1
fi

# 清理临时文件
rm -f /tmp/ddns-go.tar.gz
rm -f /tmp/LICENSE
rm -f /tmp/README.md

# 输出安装完成信息
echo -e "${green}DDNS-Go 安装成功！${plain}"
echo -e "默认登陆地址：${yellow}http://yourip:9876${plain}"
echo -e "卸载命令：${red}cd /ddnsgo && sudo ./ddns-go -s uninstall${plain}"
