#!/bin/bash

local env=$1
local zip_file="frpc_0.59.0_linux_arm64.zip"   
local extract_dir="${zip_file%.zip}"
echo "frpc驱动程序初始化.... "
if [ ! -d "$env" ]; then
    echo "错误: 指定的环境目录 '$env' 不存在。"
    return 1
fi
local zip_path="${env}/${zip_file}"
if [ ! -f "$zip_path" ]; then
    echo "错误: ZIP文件 '$zip_path' 不存在。"
    return 1
fi

local extract_path="${env}/${extract_dir}"
if [ ! -d "$extract_path" ]; then
    echo "解压缩frpc配置文件..."
    unzip "$zip_path" -d "$env"
fi 
chmod 777 "${extract_path}/frpc"
sudo cp "${extract_path}/frpc" /usr/bin/
chmod 777 "${extract_path}/mqtt_frpc"
sudo cp "${extract_path}/mqtt_frpc" /usr/bin/
sudo mkdir /etc/frp
sudo cp "${extract_path}/frpc.toml" /etc/frp/
sudo cp "${extract_path}/config.json" /etc/frp/  
sudo cp "${extract_path}/mqtt_frpc.service" "/usr/lib/systemd/system/"
# sudo systemctl enable mqtt_frpc
rm -rf "$extract_path"
echo "设置完成."