#!/bin/bash

echo -e ''
echo -e "\033[32m============ NetBird for pfSense一键安装脚本 ==============\033[0m"
echo -e ''

# 定义颜色变量
GREEN="\033[32m"
YELLOW="\033[33m"
RED="\033[31m"
CYAN="\033[36m"
RESET="\033[0m"

# 定义目录变量
ROOT="/usr/local"
WWW_DIR="$ROOT/www"
BIN_DIR="$ROOT/bin"
MENU_DIR="$ROOT/share/pfSense/menu"

# 定义日志函数
log() {
    local color="$1"
    local message="$2"
    echo -e "${color}${message}${RESET}"
}

# 安装netbird
log "$YELLOW" "安装netbird..."
pkg add -f bin/netbird.pkg
cp -f bin/netbird "$BIN_DIR/" || log "$RED" "bin 文件复制失败！"
echo ""

# 复制文件
log "$YELLOW" "复制文件..."

log "$YELLOW" "生成菜单..."
cp -f www/* "$WWW_DIR/" || log "$RED" "www 文件复制失败！"
cp -R menu/* "$MENU_DIR/" || log "$RED" "menu 文件复制失败！"

# 添加服务启动项
log "$YELLOW" "配置系统服务..."
sysrc netbird_enable="YES"
echo ""

# 添加执行权限
log "$YELLOW" "添加执行权限..."
chmod +x /usr/local/bin/netbird


# 注册节点
log "$YELLOW" "注册节点..."
log "$GREEN" "根据提示，在浏览器输入认证链接，点击确认，完成认证。"
sh bin/initialup.sh

# 设置开机启动脚本
log "$YELLOW" "添加开机命令："
log "$GREEN" "shellcmd插件里添加[service netbird start]"
echo ""

# 完成提示
log "$YELLOW" "安装完成..."
echo ""
log "$GREEN" "导航到VPN > NetBird 查看连接状态。"
log "$GREEN" "为了让节点互访，还需要将wt0虚拟网卡添加为接口，输入节点分配的IP地址，并在接口上添加防火墙规则。"
echo ""