#!/bin/sh

# Load ipmodule variables
. /opt/gira/share/devicestack/ipmodule-vars
SWDTD="/etc/init.d/swdtd"

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=$(echo $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=$(echo $element | cut -b -6) 
   if [ "$variable" = "Enable" ]
   then
     wired_found=0
     value=$(echo $element | cut -b 8-) 
     wired_enabled="$value"
   fi 
  fi
    
  if [ "$wifi_found" = "1" ]
  then 
   variable=$(echo $element | cut -b -6) 
   if [ "$variable" = "Enable" ]
   then
    wifi_found=0
    value=$(echo $element | cut -b 8-) 
    wifi_enabled="$value"
   fi 
  fi
  done

  echo "Wired is: $wired_enabled"
  echo "wifi  is: $wifi_enabled"

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

}


case "$1" in
  start)
    check_phy
	configure_multicast
    ulimit -c unlimited >/dev/null 2>&1
    echo "/opt/userdata/core-%e-%p-%t" > /proc/sys/kernel/core_pattern
    echo "1" > /proc/sys/kernel/core_uses_pid
    GDS_COUNT=`ps | grep ${DEVICESTACK} | wc -l`
    if [ ${GDS_COUNT} -gt 1 ]
    then
      echo "[devicestack] Gira DeviceStack already running! Won't start again!"
      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
      echo -n "Starting DeviceStack ... "
      ${DEVICESTACK} > ${DS_LOGFILE} &
      echo $! > ${DS_PIDFILE}
      echo $!
      pid=""
      counter=3
      while [ "$pid" = "" ] && [ $counter -gt 0 ]
      do 
        pid=`pidof ${DEVICESTACK}`
        counter=$(($counter-1))
        sleep 3
      done
      if [ "$pid" = "" ]
      then
         echo "No devicestack running. Rebooting."
         ${GDS_REBOOT}
      fi
    else
      echo -n "Starting DeviceStack in commissioning mode ... "
      COMMAND="${DEVICESTACK} -C"
      ${COMMAND} > ${DS_LOGFILE} &
      echo $! > ${DS_PIDFILE}
      echo $!
    fi
    ;;
  stop)
    echo -n "Stopping DeviceStack ... "
    if start-stop-daemon --stop --quiet --pidfile ${DS_PIDFILE}
    then
      echo "done."
    else
      echo "failed."
    fi
    # Also terminate dummy watchdog if enabled
    if [ -f "${DUMMY_WATCHDOGFILE}" ]
    then
      ${SWDTD} stop
    fi
    ;;
  *)
    echo "Usage: $0 (start|stop)"
    exit 1
esac

exit 0
