#!/bin/bash

# Variables
fail_count=0
max_fails=10
interval=12   # 12-second interval to fit 10 attempts in 2 minutes

while true; do
    # Ping youtube.com and check if it is reachable
    if ping -c 1 youtube.com &> /dev/null; then
        # If successful, reset fail_count
        fail_count=0
    else
        # If failed, increase the fail_count
        ((fail_count++))
    fi

    # If fail_count reaches max_fails, run the VPN command
    if [ "$fail_count" -ge "$max_fails" ]; then
        vpn start
        # Optional: exit or break after starting VPN
        exit 0
    fi

    # Wait for the interval before the next check
    sleep $interval
done
