#!/bin/sh

NAME=dump1090-mutability
CONFIGFILE=/etc/default/$NAME
set -e
. /usr/share/debconf/confmodule

db_set_yn() {
  if [ "x$2" = "xyes" ]; then db_set $1 true; else db_set $1 false; fi
}

# Load config file, if it exists.
if [ -e $CONFIGFILE ]; then
  . $CONFIGFILE || true

  # Store values from config file into
  # debconf db.

  db_set_yn $NAME/auto-start "$START_DUMP1090"
  db_set $NAME/run-as-user "$DUMP1090_USER"
  db_set $NAME/log-file "$LOGFILE"

  db_set $NAME/rtlsdr-device "$DEVICE"
  db_set $NAME/rtlsdr-gain "$GAIN"
  db_set $NAME/rtlsdr-ppm "$PPM"
  db_set_yn $NAME/rtlsdr-oversample "$OVERSAMPLE"

  db_set_yn $NAME/decode-fixcrc "$FIX_CRC"
  db_set_yn $NAME/decode-phase-enhance "$PHASE_ENHANCE"
  db_set_yn $NAME/decode-aggressive "$AGGRESSIVE"
  db_set $NAME/decode-lat "$LAT"
  db_set $NAME/decode-lon "$LON"
  db_set $NAME/decode-max-range "$MAX_RANGE"

  db_set $NAME/net-http-port "$HTTP_PORT"
  db_set $NAME/net-ri-port "$RAW_INPUT_PORT"
  db_set $NAME/net-ro-port "$RAW_OUTPUT_PORT"
  db_set $NAME/net-bi-port "$BEAST_INPUT_PORT"
  db_set $NAME/net-bo-port "$BEAST_OUTPUT_PORT"
  db_set $NAME/net-sbs-port "$SBS_OUTPUT_PORT"
  db_set $NAME/net-fatsv-port "$FATSV_OUTPUT_PORT"
  db_set $NAME/net-heartbeat "$NET_HEARTBEAT"
  db_set $NAME/net-out-size "$NET_OUTPUT_SIZE"
  db_set $NAME/net-out-interval "$NET_OUTPUT_INTERVAL"
  db_set $NAME/net-buffer "$NET_BUFFER"
  db_set $NAME/net-bind-address "$NET_BIND_ADDRESS"

  db_set $NAME/stats-interval "$STATS_INTERVAL"
  db_set $NAME/json-dir "$JSON_DIR"
  db_set $NAME/json-interval "$JSON_INTERVAL"
  db_set $NAME/json-location-accuracy "$JSON_LOCATION_ACCURACY"
  db_set_yn $NAME/log-decoded-messages "$LOG_DECODED_MESSAGES"
  db_set $NAME/extra-args "$EXTRA_ARGS"
fi

# Ask questions.

db_input_verify() {
  # $1 = priority
  # $2 = db key
  # $3 = verification function, should return 0 if OK
  PRI=$1; KEY=$2; VERIFY=$3

  set +e
  db_input $PRI $KEY; RESULT=$?
  db_go
  set -e
  ASKED=0
  while :
  do
    db_get $KEY
    if $VERIFY $RET; then return 0; fi
    if [ $RESULT -ne 0 ]; then
      # db_input failed, and the existing value does not validate
      if [ $RESULT = 30 ] && [ $ASKED = 0 ]
      then
       # question was skipped, but existing value is invalid
       # bump priority and try again (once)
       PRI=high
       ASKED=1
      else
       # give up, use the default value
       db_reset $KEY
       return 0
      fi
    else 
     # db_input was OK, but the value did not verify.
     # show an error message
     db_input high dump1090-mutability/invalid-$VERIFY || true
    fi

    # try again
    set +e
    db_fset $KEY seen false
    db_input high $KEY; RESULT=$?
    db_go
    set -e
  done
}
       
is_unsigned_int() {
  if echo "$1" | grep -Eq '^(0|+?[1-9][0-9]*)$'; then return 0; else return 1; fi
}

is_unsigned_int_or_empty() {
  if [ -z "$1" ]; then return 0
  elif is_unsigned_int "$1"; then return 0
  else return 1; fi
}

is_positive_int() {
  if echo "$1" | grep -Eq '^(+?[1-9][0-9]*)$'; then return 0; else return 1; fi
}

is_signed_int() {
  if echo "$1" | grep -Eq '^(0|[+-]?[1-9][0-9]*)$'; then return 0; else return 1; fi
}

is_signed_int_or_empty() {
  if [ -z "$1" ]; then return 0
  elif is_signed_int "$1"; then return 0
  else return 1; fi
}

is_ipaddrish() {
  if echo "$1" | grep -Eq '^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$'; then return 0; else return 1; fi
}

is_ipaddrish_or_empty() {
  if [ -z "$1" ]; then return 0
  elif is_ipaddrish "$1"; then return 0
  else return 1; fi
}

is_number() {
  if echo "$1" | grep -Eq '^(0|[+-]?[1-9][0-9]*)(\.[0-9]+)?$'; then return 0; else return 1; fi
}

is_number_or_empty() {
  if [ -z "$1" ]; then return 0
  elif is_number "$1"; then return 0;
  else return 1; fi
}

is_valid_gain() {
  if is_number "$1"; then return 0;
  elif [ "$1" = "max" ]; then return 0;
  elif [ "$1" = "agc" ]; then return 0;
  else return 1; fi
}

is_not_empty() {
  if [ -z "$1" ]; then return 1; else return 0; fi
}    

is_port_number() {
  if is_unsigned_int "$1"; then
    if [ "$1" -eq 0 ]; then return 0; fi
    if [ "$1" -lt 1024 ]; then return 1; fi
    if [ "$1" -gt 65535 ]; then return 1; fi
    return 0
  else
    return 1
  fi
}

db_input high $NAME/auto-start || true

db_go || true; db_get $NAME/auto-start; if [ "$RET" = "true" ]; then
 # all of these are only relevant if the init script is enabled

 db_input_verify low $NAME/run-as-user is_not_empty || true
 db_input_verify low $NAME/log-file is_not_empty || true

 db_input medium $NAME/rtlsdr-device || true

 db_go || true; db_get $NAME/rtlsdr-device; if [ "x$RET" != "xnone" ]; then
  # only if a real device was chosen:
  db_input_verify medium $NAME/rtlsdr-gain is_valid_gain || true
  db_input_verify medium $NAME/rtlsdr-ppm is_signed_int || true
  db_input low $NAME/rtlsdr-oversample || true
 fi

 db_input low $NAME/decode-fix-crc || true
 db_input low $NAME/decode-aggressive || true
 db_input_verify medium $NAME/decode-max-range is_number || true
 db_input_verify medium $NAME/decode-lat is_number_or_empty || true

 db_go || true; db_get $NAME/decode-lat; if [ -n "$RET" ]; then
  # only if latitude was given:
  db_input_verify medium $NAME/decode-lon is_number_or_empty || true
 fi

 db_input_verify medium $NAME/net-http-port is_port_number || true
 db_input_verify low $NAME/net-ri-port is_port_number || true
 db_input_verify low $NAME/net-ro-port is_port_number || true
 db_input_verify low $NAME/net-bi-port is_port_number || true
 db_input_verify low $NAME/net-bo-port is_port_number || true
 db_input_verify low $NAME/net-sbs-port is_port_number || true
 db_input_verify low $NAME/net-fatsv-port is_port_number || true
 db_input_verify low $NAME/net-heartbeat is_unsigned_int || true
 db_input_verify low $NAME/net-out-size is_unsigned_int || true
 db_input_verify low $NAME/net-out-interval is_unsigned_int || true
 db_input_verify low $NAME/net-buffer is_unsigned_int || true
 db_input_verify medium $NAME/net-bind-address is_ipaddrish_or_empty || true

 db_input_verify low $NAME/stats-interval is_unsigned_int || true

 db_input_verify low $NAME/json-interval is_positive_int || true
 db_input low $NAME/json-location-accuracy || true
 db_input low $NAME/json-dir || true

 db_input low $NAME/log-decoded-messages || true
 db_input low $NAME/extra-args || true
 
 db_go || True
fi

# Done.
db_stop