#!/bin/sh
#
# Perform necessary firezone setup steps
# prior to uninstalling package.
#

PROGNAME=`basename $0`

capture () {
  if type curl > /dev/null; then
    if [ -e /var/opt/firezone/cache/telemetry_id ]; then
      telemetry_id=`cat /var/opt/firezone/cache/telemetry_id`
      if [ ! -z "$telemetry_id" ]; then
        curl -s -XPOST \
          -m 5 \
          -H 'Content-Type: application/json' \
          -d "{
            \"api_key\": \"phc_ubuPhiqqjMdedpmbWpG2Ak3axqv5eMVhFDNBaXl9UZK\",
            \"event\": \"prerm\",
            \"properties\": {
              \"distinct_id\": \"$telemetry_id\"
            }
          }" \
          https://t.firez.one/capture/ > /dev/null
      fi
    fi
  fi
}

if [ ! -e /var/opt/firezone/.disable_telemetry ]; then
  capture || true
fi

error_exit()
{
  echo "${PROGNAME}: ${1:-"Unknown Error"}" 1>&2
  exit 1
}

exit 0
