#!/bin/sh

if ! which mullvad &> /dev/null
then
	echo "ERROR: command 'mullvad' does not exist"
	exit 1
fi

STATUS="$(mullvad status)"

CONNECTING="Connecting"
CONNECTED="Connected"
DISCONNECTED="Disconnected"

check_status() {
	STRING="$1"
	echo "$STATUS" | grep -iq "$STRING"
}

TOOLTIP="$STATUS"

TEXT="VPN 🔓"


if check_status "$DISCONNECTED"; then
	ON_CLICK="mullvad connect"
	CLASS="disconnected"
elif check_status "$CONNECTED"; then
	ON_CLICK="mullvad disconnect"
	CLASS=""
	TEXT="VPN 🔒"
elif check_status "$CONNECTING"; then
	ON_CLICK="mullvad disconnect"
	CLASS="connecting"
else
	ON_CLICK="mullvad reconnect"
	CLASS="disconnected"
fi

echo '{"text":"'$TEXT'", "tooltip":"'$TOOLTIP'", "class":"'$CLASS'"}'

