#!/bin/bash
#
# Author  : Gaston Gonzalez
# Date    : 4 June 2025
# Update  : 7 June 2025
# Purpose : Wrapper script around ADS-B integration
#
# Preconditions:
# 1. RTL-SDR v2 is connected and detected
#
# Postconditions:
# 1. dump1099 systemd service is launched
# 2. Web browser is launched with dump1090 map

# TODO: Check for running service instead of using sleep before Min
# TODO: Use icon and add to launcher
# TODO: Increse size of aircraft
# TODO: Research why RTL-SDR v4 does not work
# TODO: Test RTL-SDR v3 
# TODO: Use ETC offline map engine

source /opt/emcomm-tools/bin/et-common

cleanup() {
  et-log "Stopping dump1099 service"
  systemctl --user stop et-service-dump1090
}

# Stop dump1099 on exit
trap cleanup EXIT

if [[ ! -e ${ET_DEVICE_SDR} ]]; then
  notify-user "No ${ET_DEVICE_SDR} detected. Reconnect your SDR."
  exit 1
fi

systemctl --user restart et-service-dump1090
if [[ $? -eq 0 ]]; then
  sleep 10
  min http://localhost:1090 2>/dev/null
fi
