#! /bin/sh

### BEGIN INIT INFO
# Provides:		overpass
# Required-Start:	$remote_fs $syslog
# Required-Stop:	$remote_fs $syslog
# Default-Start:	2 3 4 5
# Default-Stop:         0 1 6
# Short-Description:	Overpass startup script for dispatcher, area-queries and diff updates
### END INIT INFO

#You can override those  variables in /etc/default/overpass

DIFF_URL="http://planet.osm.org/replication/minute"
DB_DIR=/var/lib/overpass/
EXEC_DIR=/usr/local/bin/

#This directory will only contain logs in case of problems at start 
LOG_DIR=/var/log/overpass/
RUN_AS_USER=root
META=yes

#To enable area, setting this to yes isn't enought ! Please also read : http://wiki.openstreetmap.org/wiki/Talk:Overpass_API/install
AREA=no

if test -f /etc/default/overpass; then
    . /etc/default/overpass
fi

mkdir $LOG_DIR 2>/dev/null 
mkdir $DB_DIR 2>/dev/null
chown $RUN_AS_USER $DB_DIR 

DISPATCHER_PID_FILE=/var/run/dispatcher.pid
DISPATCHER_AREA_PID_FILE=/var/run/dispatcher_area.pid
DIFF_UPDATE_PID_FILE=/var/run/diff_update.pid
AREA_UPDATE_PID_FILE=/var/run/area_update.pid
DISPATCHER=$EXEC_DIR/dispatcher
DIFF_UPDATER=$EXEC_DIR/fetch_osc_and_apply.sh
AREA_BUILDER=$EXEC_DIR/rules_loop.sh

DISPATCHER_OPTIONS="--meta=$META --osm-base --db-dir=$DB_DIR"
AREA_DISPATCHER_OPTIONS="--areas --db-dir=$DB_DIR"

DIFF_UPDATER_OPTION="$DIFF_URL --meta=$META"


test -x $DISPATCHER || exit 0
. /lib/lsb/init-functions

# Are we running from init?
run_by_init() {
    ([ "$previous" ] && [ "$runlevel" ]) || [ "$runlevel" = S ]
}

check_dev_null() {
    if [ ! -c /dev/null ]; then
	if [ "$1" = log_end_msg ]; then
	    log_end_msg 1 || true
	fi
	if ! run_by_init; then
	    log_action_msg "/dev/null is not a character device!"
	fi
	exit 1
    fi
}

case "$1" in
  start)
	check_dev_null
	log_daemon_msg "Starting Overpass dispatcher daemon" "dispatcher"
	if test -f $DISPATCHER_PID_FILE ; then 
	  echo "dispatcher allready running ? : pid file exists in $DISPATCHER_PID_FILE"
	  exit 0
	fi
	rm /dev/shm/osm3s* 2>/dev/null
	rm $DB_DIR/osm3s* 2>/dev/null
	if start-stop-daemon --start --quiet -b --oknodo --make-pidfile -c $RUN_AS_USER --pidfile $DISPATCHER_PID_FILE --exec $DISPATCHER -- $DISPATCHER_OPTIONS >> $LOG_DIR/dispatcher.log 2>&1; then
	    #That directory is used by the xapi translator and not automatically created
	    mkdir /tmp/translate_xapi 2>/dev/null
	    chmod 777 /tmp/translate_xapi 
	    log_end_msg 0
	else
	    log_end_msg 1
	fi
	if [ $AREA = "yes" ] ; then
	  log_daemon_msg "Starting Overpass dispatcher daemon for areas" "dispatcher areas"
	  if start-stop-daemon --start --quiet -b --oknodo --make-pidfile -c $RUN_AS_USER --pidfile $DISPATCHER_AREA_PID_FILE --exec $DISPATCHER -- $AREA_DISPATCHER_OPTIONS >> $LOG_DIR/dispatcher.log 2>&1; then
	    log_end_msg 0
	  else
	    log_end_msg 1
	  fi
	  log_daemon_msg "Starting Overpass areas builder" "areas building"

	  if start-stop-daemon --start --quiet -b --oknodo --make-pidfile -c $RUN_AS_USER --pidfile $AREA_UPDATE_PID_FILE --exec $AREA_BUILDER -- $DB_DIR  >> $LOG_DIR/area_update.log 2>&1; then
	    log_end_msg 0
	  else
	    log_end_msg 1
	  fi
	fi
	if test -x $DIFF_UPDATER ; then
	log_daemon_msg "Starting Overpass diff updates with command $DIFF_UPDATER $DIFF_UPDATER_OPTION" "diff updates"
	  if start-stop-daemon --start --quiet -b --oknodo --make-pidfile -c $RUN_AS_USER --pidfile $DIFF_UPDATE_PID_FILE --exec $DIFF_UPDATER -- $DIFF_UPDATER_OPTION >> $LOG_DIR/diff-updates.log 2>&1; then
	    log_end_msg 0
	  else
	    log_end_msg 1
	  fi
	fi
	;;
  stop)
	if test -x $DIFF_UPDATER ; then
	  if start-stop-daemon --stop --quiet --oknodo --pidfile $DIFF_UPDATE_PID_FILE; then
	    log_daemon_msg "Stopping Overpass diff updates" "diff updates"
	    #A little clean up of the xapi tmp dirrectory
	    rm -r /tmp/translate_xapi 2>/dev/null
	    rm -f $DIFF_UPDATE_PID_FILE
	    log_end_msg 0
	  else
	    log_end_msg 1
	  fi
	fi
	log_daemon_msg "Stopping Overpass dispatcher daemon" "dispatcher"
	if start-stop-daemon --stop --quiet --oknodo --pidfile $DISPATCHER_PID_FILE; then
	    log_end_msg 0
	    rm /dev/shm/osm3s* 2>/dev/null
	    rm $DB_DIR/osm3s* 2>/dev/null
	    rm -f $DISPATCHER_PID_FILE
	else
	    log_end_msg 1
	fi
	if [ $AREA = "yes" ] ; then
	  log_daemon_msg "Stopping Overpass dispatcher area daemon " "dispatcher area"
	  if start-stop-daemon --stop --quiet --oknodo --pidfile $DISPATCHER_AREA_PID_FILE; then
	    log_end_msg 0
	    rm -f $DISPATCHER_AREA_PID_FILE
	  else
	    log_end_msg 1
	  fi
	  #FIXME this isn't enought, the osm3s_query builder will keep running for an entire loop of several hours, but we don't get it's pid
	  # Better than nothing -- sly 
	  killall osm3s_query
	  log_daemon_msg "Stopping Overpass area builder " "area builder"
	  if start-stop-daemon --stop --quiet --oknodo --pidfile $AREA_UPDATE_PID_FILE; then
	    log_end_msg 0
	    rm -f $AREA_UPDATE_PID_FILE
	  else
	    log_end_msg 1
	  fi
	fi
	;;

  status)
	status_of_proc -p $DISPATCHER_PID_FILE "" "overpass dispatcher"
	status_of_proc -p $DIFF_UPDATE_PID_FILE "" "overpass diff updates"
	if [ $AREA = "yes" ] ; then
	  status_of_proc -p $DISPATCHER_AREA_PID_FILE "" "overpass dispatcher areas" 
	fi
	;;

  *)
	log_action_msg "Usage: /etc/init.d/overpass {start|stop|status}"
	exit 1
esac

exit 0
