#!/bin/bash

# Apply power
echo 1 > /dev/loadA6
echo 1 > /dev/loadA7

#LPC3Reg::P0OutpSetPhys_ = UART2_SHUTDOWN
devmem2 0x40028124 w 0x00000008 # P0.3 mux set
devmem2 0x40028050 w 0x00000008 # Set direction of P0.3 output
devmem2 0x40028044 w 0x00000008 # UART 2 (TX1) enable
#devmem2 0x40028048 w 0x00000008 # UART 2 (TX1) disable

# Resolve the vehicle's host IP
# Host IP should be 10.89.X.2, ESP IP should be 10.89.X.3, where X is the vehicle's unique subnet ID
case $HOSTNAME in
  lrauv-ahi)
  HOST_IP="10.89.1.2"
  ;;
  lrauv-aku)
  HOST_IP="10.89.2.2"
  ;;
  lrauv-opah)
  HOST_IP="10.89.3.2"
  ;;
  lrauv-melia)
  HOST_IP="10.89.4.2"
  ;;
  lrauv-makai)
  HOST_IP="10.89.5.2"
  ;;
  lrauv-tethys)
  HOST_IP="10.89.6.2"
  ;;
  lrauv-whoidhs)
  HOST_IP="10.89.7.2"
  ;;
  lrauv-daphne)
  HOST_IP="10.89.8.2"
  ;;
  lrauv-galene)
  HOST_IP="10.89.9.2"
  ;;
  lrauv-pontus)
  HOST_IP="10.89.10.2"
  ;;
  lrauv-brizo)
  HOST_IP="10.89.11.2"
  ;;
  lrauv-triton)
  HOST_IP="10.89.12.2"
  ;;
  *)
  # Try to parse the host's IP from ifconfig
  HOST_IP=$(ifconfig | grep 'inet addr:'| grep -v '127.0.0.1' | cut -d: -f2 | awk '{ print $1}')
  ;;
esac

ESP_IP=${HOST_IP%?}"3"
# Google public DNS
DNS="8.8.8.8"

if [ -z "$HOST_IP" ]
then
  echo "Error. Failed to resolve host IP."
else
  # Start point-to-point protocol daemon
  cmd="pppd xonxoff asyncmap A0000 /dev/ttyTX1 linkname esp noauth local lock 115200 $HOST_IP:$ESP_IP persist maxfail 0 holdoff 10 lcp-echo-interval 60 lcp-echo-failure 3 proxyarp ktune deflate 12 ms-dns $DNS"
  echo $cmd;eval $cmd
fi
