#!/bin/sh

PIDFILE=`grep "pidfile " /etc/linesrv.conf|awk '{ print $2; }'`

if kill -0 `cat $PIDFILE` > /dev/null 2> /dev/null; then
  # LineControl Server is running, ok.
  /bin/true
else
  unknownpids=`pgrep linesrv`
  for  pid in $unknownpids; do                                                   
    if [ "$pid" != "$$" ]; then
      # Unknown LineControl Server is running, ok? Assume yes.
      /bin/true
    else
      if [ -e /var/lib/linesrv/started ]; then
        # LineControl Server is running and not stopped via init.d-script
        echo "LineControl Server not running, so trying to start it..."
        /etc/init.d/linesrv start
      else
        # LineControl Server is not running, but stopped via init.d-script, ok.
        /bin/true
      fi
    fi
  done
fi
