#!/bin/bash

#TTY=

#
# Set options 
#

ASTARGS=""
if [ -f /etc/sysconfig/astiumd ]
then
  . /etc/sysconfig/astiumd
fi

if [ "$TTY" != "" ]
then
  if [ -c /dev/tty${TTY} ]
  then
    TTY=tty${TTY}
  elif [ -c /dev/vc/${TTY} ]; then
    TTY=vc/${TTY}
  else
    echo "Cannot find your TTY (${TTY})" >&2 
    exit 1
  fi
  ASTARGS="${ASTARGS} -vvvddd" 
fi

ulimit -c unlimited
ulimit -n 2048

#
# Don't die if stdout/stderr can't be written to
#
trap '' PIPE

run_astiumd()
{
  while :
  do
    cd /tmp
    if [ "$TTY" != "" ]; then
      stty sane < /dev/${TTY}
      /usr/local/astium/sbin/astiumd -F ${ASTARGS} >& /dev/${TTY} < /dev/${TTY}
    else
      /usr/local/astium/sbin/astiumd -F ${ASTARGS} 2>&1 >> /var/log/astiumd.log < /dev/null
    fi
    EXITSTATUS=$?
    echo "Astiumd ended with exit status $EXITSTATUS" >> /var/log/astiumd.log
    echo "Automatically restarting Astiumd"           >> /var/log/astiumd.log
    sleep 1
    killall -q -9 astiumd
    sleep 1
  done
}

run_astiumd &

