#!/bin/bash

EXIT_NODE=tailscale-jumphost
STATE=$(tailscale status | grep "$EXIT_NODE" | awk -F ';' '{print $2}' | tr -d ' ')

if [ "$STATE" == "offersexitnode" ]; then
    sudo tailscale set --exit-node=tailscale-jumphost.tailf6c9a.ts.net.
    if ping -c 192.168.0.1 > /dev/null 2>&1; then
        notify-send -i ${HOME}/.scripts/icons/tailscale.png "Tailscale Exit Node" "Tunnel Activated: $EXIT_NODE"
    else
        notify-send --urgency=critical -i ${HOME}/.scripts/icons/tailscale.png "Tailscale Exit Node" "Error"
    fi
fi

if [ "$STATE" == "exitnode" ]; then
    sudo tailscale set --exit-node=
    if ping -c 1 1.1.1.1 > /dev/null 2>&1; then
        notify-send -i ${HOME}/.scripts/icons/tailscale.png "Tailscale Exit Node" "Tunnel Deactivated"
    else
        notify-send --urgency=critical -i ${HOME}/.scripts/icons/tailscale.png "Tailscale Exit Node" "Error"
    fi
fi
