#!/bin/sh

watchdogPID=$(pidof watchdog)

if [ "$(echo ${watchdogPID} | wc -w)" -gt  "1" ]; then 
  echo "Cannot determine watchdog: ${watchdogPID}"
  exit 1
fi

if [ "${watchdogPID}" = "" ]; then                  
  echo "Starting watchdog"                 
  watchdog -t 15 /dev/watchdog
  watchdogStartedPID=$(pidof watchdog)
  echo "Started watchdog with pid ${watchdogStartedPID}"
else                          
  echo "Watchdog is already running: ${watchdogPID}"
fi

for i in /opt/gira/etc/install.d/S??*; do
  [ ! -f "$i" ] && continue
  echo "starting $i" 
  $i
done

if [ "${watchdogPID}" = "" ]; then  
  echo "shutting down watchdog: ${watchdogStartedPID}"  
  kill ${watchdogStartedPID}             
fi
