#!/bin/bash 
# Check if the new remote_port value is provided as an argument
if [ $# -ne 3 ]; then
    echo "Usage: $0 <new_remote_port> <new_remote_group> <new_remote_user>"
    exit 1
fi

rm -rf frpc frpc.ini frpc.service
wget http://resource.sw-motion.cn/tools/frpc_tools/frpc
wget http://resource.sw-motion.cn/tools/frpc_tools/frpc.ini
wget http://resource.sw-motion.cn/tools/frpc_tools/frpc.service

# Assign the new remote_port value from the command line argument
new_remote_port=$1
new_remote_group=$2
new_remote_user=$3

# Use sed to replace the remote_port value in the frpc.ini file
sed -i "s/remote_port = [0-9]*/remote_port = $new_remote_port/" frpc.ini
# Check if the update was successful
if [ $? -eq 0 ]; then
    echo "Configuration updated successfully. New remote_port is: $new_remote_port"
else
    echo "frpc.ini : Failed to update configuration:New remote_port."
fi

# Use sed to replace the remote_frpc value in the frpc.ini file
sed -i "s/\[frpc\]/[$RANDOM]/" frpc.ini

# Check if the update was successful
if [ $? -eq 0 ]; then
    echo "Configuration updated successfully. New remote_frpc is: $RANDOM"
else
    echo "frpc.ini : Failed to update configuration:New remote_frpc."
fi

# Use sed to replace the remote_user value in the frpc.service file
sed -i "s/User=ubt20/User=$new_remote_user/" frpc.service

# Check if the update was successful
if [ $? -eq 0 ]; then
    echo "Configuration updated successfully. New remote_user is: $new_remote_user"
else
    echo "frpc.service : Failed to update configuration:New remote_user."
fi

# Use sed to replace the remote_group value in the frpc.service file
sed -i "s/Group=ubt20/Group=$new_remote_group/" frpc.service

# Check if the update was successful
if [ $? -eq 0 ]; then
    echo "Configuration updated successfully. New remote_user is: $new_remote_user"
else
    echo "frpc.service : Failed to update configuration:New remote_user."
fi

sudo chmod +x frpc
sudo cp -f frpc /usr/bin/
sudo mkdir -p /etc/frpc/
sudo cp -f frpc.ini /etc/frpc/
sudo cp -f frpc.service /etc/systemd/system/frpc.service 
sudo systemctl  enable frpc
sudo systemctl start frpc
