#!/bin/sh

. /etc/conf.d/tracd.conf
. /etc/rc.conf
. /etc/rc.d/functions

DAEMON_NAME=tracd

if [ -z "$PORT" ]; then
  PORT="-p 8080"
else
  PORT="-p $PORT"
fi

if [ -z "$AUTH" ]; then
  AUTH=
else
  AUTH="--auth ${AUTH//;/ --auth }"
fi

case "$1" in
  start)
    if [ -z "$PROJECT" ]; then
      echo "You need to set the project path in /etc/conf.d/${DAEMON_NAME}.conf"
      exit 1
    fi
    stat_busy "Starting $DAEMON_NAME: "
    tracd -d $HOSTNAME $PORT $AUTH $PROJECT
    if [ $? -gt 0 ]; then
      stat_fail
    else
      add_daemon $DAEMON_NAME
      stat_done
    fi
    ;;
  stop)
    stat_busy "Shutting down $DAEMON_NAME: "
    kill `ps ax|grep pyth|grep tracd|awk -- "{print \\$1;}"`
    rm_daemon $DAEMON_NAME
    stat_done
    ;;
  reload|restart)
    $0 stop
    $0 start
    ;;
  *)
    echo "Usage: $0 {start|stop|restart|reload}"
esac 
