#! /bin/sh 
 #
 # This file is part of the Anotatornia suite.
 # 
 # Copyright © 2007, 2008, 2009, 2010 by Instytut Podstaw Informatyki
 # Polskiej Akademii Nauk (IPI PAN; Institute of Computer Science, Polish
 # Academy of Sciences; cf. www.ipipan.waw.pl).  All rights reserved.
 # 
 # This file may be distributed and/or modified under the terms of the
 # GNU General Public License version 3 as published by the Free Software
 # Foundation and appearing in the file COPYING included in the packaging
 # of this file.  (See http://www.gnu.org/licenses/translations.html for
 # unofficial translations.)
 # 
 # A commercial license is available from IPI PAN (contact
 # Michal.Ciesiolka.waw.pl or ipi.waw.pl for more
 # information).  Licensees holding a valid commercial license from IPI
 # PAN may use this file in accordance with that license.
 # 
 # This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING
 # THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 # PURPOSE.
 #

#
# skeleton	example file to build /etc/init.d/ scripts.
#		This file should be used to construct scripts for /etc/init.d.
#
#		Written by Miquel van Smoorenburg <miquels@cistron.nl>.
#		Modified for Debian 
#		by Ian Murdock <imurdock@gnu.ai.mit.edu>.
#
# Version:	@(#)skeleton  1.9  26-Feb-2001  miquels@cistron.nl
#

# Modified for Anotatornia project by natror in 2008.

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DESC="Anotatornia mongrel servers"
NAME=anotatornia-mongrels
if [ -f /usr/bin/mongrel_rails ] ; then
    DAEMON=/usr/bin/mongrel_rails
else
    DAEMON=~/bin/mongrel_rails
fi
#/usr/bin/mongrel_rails
SCRIPTNAME=/etc/init.d/$NAME

test -x $DAEMON || exit 0

# configuration 
# don't change here, override in /etc/default/mongrel instead


if [ -e ~/latex ]; then
    MONGREL_USER=zrogezrg
else
    MONGREL_USER=natror
fi
MONGREL_PATHER=natror

PROJECT_PATH=/home/$MONGREL_PATHER/anotatornia

if [ "$USER" = "$MONGREL_USER" ]
then
    MONGREL_CHUID=
else
    MONGREL_CHUID="--user $MONGREL_USER --group $MONGREL_USER"
fi


# runs given command ($1) for the given app ($2) or all apps, 
# if $2 is not given
run_command() {
    echo  $1
  # reset optional args
  case "$1" in
      start|reload)
	  for p in 8003 8004 ; do 
#        pidfile="/var/run/$NAME.$p.pid"
	      echo version/port $p
	      pidfile="$PROJECT_PATH/log/$NAME.$p.pid"
	      logfile="$PROJECT_PATH/log/mongrel-daemon.log"
	      DAEMON_OPTS="-e nk$p -P $pidfile -l $logfile"
	      
	      case "$1" in
		  start)
		      $DAEMON   start -d  $MONGREL_CHUID   $DAEMON_OPTS -p $p -c $PROJECT_PATH
		      sleep 5 # bez tego zakrzyczało, że chown nie widzi pidfile'a
		      chown $MONGREL_USER $pidfile
		      ;;
		  reload)
		      echo "reload is currently a no-op."
		      ;;
	      esac
	  done
	  ;;
      stop)
	  for p in 8003 8004 ; do
	      pidfile="$PROJECT_PATH/log/$NAME.$p.pid"
	      echo "killing process pointed in $pidfile"
              ($DAEMON stop  --pid $pidfile || kill -9 `cat $pidfile` || echo "couldn't kill process $pidfile (`cat $pidfile`), removing pid file anyway") && rm -f $pidfile
	  done
	  ;;
  esac
  echo "."
}



case "$1" in
    start)
	echo "Starting $DESC: $NAME"
	run_command 'start'
	echo "."
	;;
    stop)
	echo "Stopping $DESC: $NAME"
	run_command 'stop'
	echo "."
	;;
    reload)
	echo "Reloading $DESC: $NAME"
	run_command 'reload'  
	echo "."
	;;
    restart|force-reload)
  #
  #  If the "reload" option is implemented, move the "force-reload"
  #  option to the "reload" entry above. If not, "force-reload" is
  #  just the same as "restart".
  #
	echo "Restarting $DESC: $NAME"
	run_command 'stop'
	sleep 1
	run_command 'start'
	echo "."
  ;;
    *)
  # echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
	echo "Usage: $SCRIPTNAME {start|stop|reload|restart}" >&2
	exit 1
	;;
esac

exit 0
