#!/usr/bin/env bash

RUNNING_MARKER_FILE="/tmp/coder_desktop_running"
VPN_MARKER_FILE="/tmp/coder_vpn_was_running"

rm $VPN_MARKER_FILE $RUNNING_MARKER_FILE || true

if pgrep 'Coder Desktop'; then
  touch $RUNNING_MARKER_FILE
fi

echo "Turning off VPN"
if scutil --nc list | grep -q "Coder"; then
  echo "CoderVPN found. Stopping..."
  if scutil --nc status "Coder" | grep -q "^Connected$"; then
    touch $VPN_MARKER_FILE
  fi
  scutil --nc stop "Coder"

  # Wait for VPN to be disconnected
  while scutil --nc status "Coder" | grep -q "^Connected$"; do
    echo "Waiting for VPN to disconnect..."
    sleep 1
  done
  while scutil --nc status "Coder" | grep -q "^Disconnecting$"; do
    echo "Waiting for VPN to complete disconnect..."
    sleep 1
  done
else
  echo "CoderVPN not found. Nothing to stop."
fi
echo "Done."

echo "Asking com.coder.Coder-Desktop to quit..."
osascript -e 'if app id "com.coder.Coder-Desktop" is running then' -e 'quit app id "com.coder.Coder-Desktop"' -e 'end if'
echo "Done."

APP="/Applications/Coder Desktop.app"
if [ -d "$APP" ]; then
  echo "Deleting Coder Desktop..."
  rm -rf "$APP"
  echo "Done."
fi

exit 0
