#!/bin/sh

# Load ipmodule variables
. /opt/gira/share/devicestack/ipmodule-vars
SWDTD="/etc/init.d/swdtd"
CORES_PATH=/opt/userdata/cores
mkdir -p ${CORES_PATH}

configure_multicast()
{
  route del -net 224.0.0.0 netmask 240.0.0.0 2> /dev/null
  eth=`ifconfig | grep "^eth" | sed -e "s/\(eth[0-9]\)\(.*\)/\1/"`
  if [ -n "$eth" ]; then
    route add -net 224.0.0.0 netmask 240.0.0.0 $eth
  fi
  wlan=`ifconfig | grep "^wlan" | sed -e "s/\(wlan[0-9]\)\(.*\)/\1/"`
  if [ -n "$wlan" ]; then
    route add -net 224.0.0.0 netmask 240.0.0.0 $wlan
  fi
}

check_phy()
{
 settings=$(cat "/opt/userdata/connman/settings")
 arr=$(printf "%s\\n" "$settings" | tr "\n" "\EOF")
 wifi_found=0
 wired_found=0
 wifi_enabled="false"
 wired_enabled="false"

 for element in $arr
 do
  if [ "$element" = "[WiFi]" ]
  then 
    wifi_found=1
    wired_found=0
  fi
  
  if [ "$element" = "[Wired]" ]
  then
    wired_found=1 
    wifi_found=0
  fi
 
  
  if [ "$wired_found" = "1" ]
  then 
   variable=$(printf "%s\\n" "$element" | cut -b -6)
   if [ "$variable" = "Enable" ]
   then
     wired_found=0
     value=$(printf "%s\\n" "$element" | cut -b 8-)
     wired_enabled="$value"
   fi 
  fi
    
  if [ "$wifi_found" = "1" ]
  then 
   variable=$(printf "%s\\n" "$element" | cut -b -6)
   if [ "$variable" = "Enable" ]
   then
    wifi_found=0
    value=$(printf "%s\\n" "$element" | cut -b 8-)
    wifi_enabled="$value"
   fi 
  fi
  done

  printf "Wired is: %s\\n" "$wired_enabled"
  printf "wifi  is: %s\\n" "$wifi_enabled"

  if [ "$wired_enabled" = "false" ] && [ "$wifi_enabled" = "true" ]
  then
    printf "Turning off phy\\n"
    connmanctl enable ethernet
    connmanctl disable ethernet
  fi
}

case "$1" in
  start)
    check_phy
	configure_multicast
    ulimit -c unlimited >/dev/null 2>&1
    printf "%s/core_pid-%%e_sig-%%s\\n" "${CORES_PATH}" > /proc/sys/kernel/core_pattern
    GDS_COUNT=`ps | grep ${DEVICESTACK} | wc -l`
    if [ ${GDS_COUNT} -gt 1 ]
    then
      printf "[devicestack] Gira DeviceStack already running! Won't start again!\\n"
      exit 0
    fi
    
    # Start dummy watchdog on developer packages
    # Will prevent reboots after killing the devicestack
    if [ -f "${DUMMY_WATCHDOGFILE}" ]
    then
      ${SWDTD} start
    fi
    
    # Check for commissioning mode
    if [ -f "${COMMISSIONED_FILE}" ]
    then
      printf "Starting DeviceStack ... "
      ${DEVICESTACK} &
      printf "%s\\n" "$!" > ${DS_PIDFILE}
      printf "%s\\n" "$!"
      pid=""
      counter=3
      while [ "$pid" = "" ] && [ $counter -gt 0 ]
      do 
        pid=`pidof ${DEVICESTACK}`
        counter=$(($counter-1))
        sleep 3
      done
      if [ "$pid" = "" ]
      then
         printf "No devicestack running. Rebooting.\\n"
         ${GDS_REBOOT}
      fi
    else
      printf "Starting DeviceStack in commissioning mode ... "
      COMMAND="${DEVICESTACK} -C"
      ${COMMAND} &
      printf "%s\\n" "$!" > ${DS_PIDFILE}
      printf "%s\\n" "$!"
    fi
    ;;
  stop)
    printf "Stopping DeviceStack ... "
    if start-stop-daemon --stop --quiet --pidfile ${DS_PIDFILE}
    then
      printf "done.\\n"
    else
      printf "failed.\\n"
    fi
    # Also terminate dummy watchdog if enabled
    if [ -f "${DUMMY_WATCHDOGFILE}" ]
    then
      ${SWDTD} stop
    fi
    ;;
  *)
    printf "Usage: %s (start|stop)\\n" "$0"
    exit 1
esac

exit 0
