#!/bin/bash

while true; do
	# Check both Mullvad connection and internet connectivity
	if ! mullvad status | grep -q "Connected"; then
		echo "$(date '+%H:%M:%S') - Mullvad reports as disconnected. Reconnecting..."
		mullvad disconnect
		sleep 2
		mullvad connect

		# Wait a moment for connection to establish
		sleep 2

		# Verify and report the new connection status
		if mullvad status | grep -q "Connected"; then
			echo "$(date '+%H:%M:%S') - Successfully reconnected to Mullvad"
		fi
	elif ! ping -c 1 google.com &>/dev/null; then
		echo "$(date '+%H:%M:%S') - Ping to google.com failed. Reconnecting..."
		mullvad disconnect
		sleep 2
		mullvad connect

		# Wait a moment for connection to establish
		sleep 2

		# Verify and report the new connection status
		if mullvad status | grep -q "Connected" && ping -c 1 google.com &>/dev/null; then
			echo "$(date '+%H:%M:%S') - Successfully reconnected to Mullvad with internet access"
		fi
	fi
	sleep 1
done
