#!/usr/bin/bash

# Загрузить конф и парам
yes | cp -i /etc/wireguard/wg0.conf /etc/wireguard/wg0.conf.0
[ ! -f /etc/wireguard/wg0.conf ] && yes | cp -i ${target_in}/wireguard/* /etc/wireguard
#yes | cp -i ${target_in}/wireguard/* /etc/wireguard

#wg-quick up wg0
systemctl enable wg-quick@wg0.service
#systemctl restart wg-quick@wg0.service
systemctl status wg-quick@wg0.service

cat > /root/ping.sh << 'EOF'
#!/usr/bin/bash
serverIP=172.30.0.254 #change server1 ip or name 

if /bin/ping -c 3 $serverIP >/dev/null 2>&1; then
        sleep 1
#       echo $?
else
        systemctl restart wg-quick@wg0
        echo "restart wg0"
fi
EOF
chmod +x /root/ping.sh

crontab -l > /root/current_cron
cat /root/current_cron
cat >> /root/current_cron << EOF
*/5 * * * * /root/ping.sh
EOF
crontab < /root/current_cron
rm -f /root/current_cron

