#!/usr/bin/env sh
# This script is run when the EMU boots. It is used to check for application
# updates and execute all the applications that the EMU launches at startup.

INSTALL_DIR=/zoox
UPDATE_DIR=/zoox/update

# Ensure syslogs and trace logs can be generated.
mkdir -p /zoox/log && touch /zoox/log/syslog
mkdir -p /zoox/log/trace

# Set up the CAN and Chum logging directories and configuration file.
if [ $(uname -m) = "Zoox_EMU_v4" ]; then
  mkdir -p /mnt/ssd/log/can
  mkdir -p /mnt/ssd/log/can_events
  mkdir -p /mnt/ssd/log/ipc

  # Remove the old CAN log directory if it exists.
  if [ -d "/zoox/log/can" ]; then
    rm -r "/zoox/log/can"
  fi

  ln -s /mnt/ssd/log/can /zoox/log/can
  ln -s /mnt/ssd/log/can_events /zoox/log/can_events
  ln -s /mnt/ssd/log/ipc /zoox/log/ipc
else
  mkdir -p /zoox/log/can
  mkdir -p /zoox/log/can_events
  mkdir -p /zoox/log/ipc
fi

# Create the directories for storing provisional and quarantined ipc chum
# messages
mkdir -p /zoox/log/ipc/provisional_chum
mkdir -p /zoox/log/ipc/quarantined_chum

log()
{
  IFS=" " echo "$0: $*"
  logger -- "$0:" "$@"
}

die()
{
  log "$@"
  exit 1
}

log "Checking for data integrity and activate."
(cd "$UPDATE_DIR" && ./updater "$INSTALL_DIR") || die "Manifest verification failed."

# Add route to BOX
# @todo @jira{FW-10130} move this to respective BSPs once BSPs are updated automatically
if [ $(uname -m) = "Zoox_EMU_v4" ]; then
  route add -net 192.168.128 192.168.12.50
else
  route add -net 10.0.0 192.168.3.250
fi

log "Startup.sh: Starting Programs"
# Start all Zoox applications
(/zoox/supervisor > /dev/null; log "Supervisor exited") &
