#!/bin/sh
#
### BEGIN INIT INFO
# Provides:        cow
# Required-Start:  $network $remote_fs
# Required-Stop:   $network $remote_fs
# Default-Start:   2 3 4 5
# Default-Stop:    0 1 6
# Short-Description: Start the CoW wiki system
### END INIT INFO
#
# System-wide start/stop script for CoW.  This should be copied into
# /etc/init.d, renamed to something sensible, then symlinked into the relevant
# /etc/rcN.d directories appropriately.
#

####### Customisable parameters ########

# Descriptive name for this COW instance
NAME=CoW

# The top directory of the cow tree (i.e. gatewiki/cow).  Grails must be
# located at $COW_HOME/../grails, the Cow-customised sventon app at
# $COW_HOME/../sventon, and GWT at $COW_HOME/../gwt.  In practice this means
# that if you want to serve several sites from different instances of CoW on
# one server then you should check out a single copy of the whole of gatewiki,
# then duplicate the "cow" directory under different names, one for each
# instance (alternatively use the create-custom-site.sh script - see the user
# guide on "deployment and runtime dependencies").
COW_HOME=/opt/gatewiki/cow

# The port on which you want CoW to run. If you are going to run multiple Cow
# instances then you will definately need to chage this
COW_PORT=8080
COW_HTTPS_PORT=8443

# This assumes you want to run a public instance of CoW. If you only want to
# run a localhost accessable version
COW_MODE=server

# Any other options you want to pass to the ant that runs CoW.
COW_OPTS=

# Unix user name under which CoW is to be run
RUN_AS=gate

# The CoW user home -- this is where .cowrc.d will be looked for
COW_USER_HOME=/home/$RUN_AS

# PID file.  The default of $COW_HOME/cow.pid should be fine for most uses
# but if you're running multiple copies try e.g. COW_USER_HOME instead
PIDFILE="$COW_HOME/cow.pid"

# Logging: you should set one or other of LOGFILE or LOGPIPE.  If LOGFILE is
# set then standard output and standard error from CoW will be redirected to
# the given file.  If LOGPIPE is set then they will instead be piped to this
# command (for example a log rotator program).
#
# Log file to which standard output and error streams should be redirected
# Again if you're running multiple copies try e.g. COW_USER_HOME instead
LOGFILE="$COW_HOME/cow.log"

# Program to which the standard output and error streams should be piped, for
# example a log rotation program
# LOGPIPE="/usr/sbin/rotatelogs $COW_HOME/cow.log.%Y-%m-%d-%H_%M_%S 5M"

[ -f $LOGFILE ] && mv $LOGFILE ${LOGFILE}.previous
STACKTRACE="${COW_HOME}/stacktrace.log"
[ -f $STACKTRACE ] && mv $STACKTRACE ${STACKTRACE}.previous

# The location of ant (1.7 or later)
ANT_HOME=/opt/ant-1.7
ANT=$ANT_HOME/bin/ant

# The location of the Java runtime used to run CoW.  If you intend to run solr
# then Java 6 is required, otherwise Java 5.
JAVA_HOME=/usr/lib/jvm/java-6-sun

# Locale settings - the locale under which CoW runs must match the character
# encoding of the .yam files it will be processing.  en_GB.UTF-8 is a good
# choice if you have UTF-8 YAM files.
LANG=en_GB.UTF-8
LC_ALL=en_GB.UTF-8

######## Customisable parameters end here ########

export ANT_HOME ANT JAVA_HOME COW_HOME LANG LC_ALL

PATH=/sbin:/bin:/usr/sbin:/usr/bin

. /lib/lsb/init-functions

if [ -n "$LOGPIPE" ]; then
  LOGGING="| $LOGPIPE"
elif [ -n "$LOGFILE" ]; then
  LOGGING=">> $LOGFILE"
fi

case $1 in
  start)
    log_daemon_msg "Starting $NAME" "CoW"
    start-stop-daemon --start --quiet --oknodo --pidfile="$PIDFILE" --startas="/bin/sh" --chuid=$RUN_AS --chdir="$COW_HOME" --background --make-pidfile \
      -- -c "exec $ANT -emacs -Dgate.cow.server.port=$COW_PORT -Dgate.cow.server.port.https=$COW_HTTPS_PORT -Dgate.cow.mode=$COW_MODE -Dgate.cow.user.home=$COW_USER_HOME $COW_OPTS run-prod 2>&1 $LOGGING"
    log_end_msg $?
  ;;

  stop)
    log_daemon_msg "Stopping $NAME" "CoW"
    # issue the shutdown request
    ( cd "$COW_HOME" && $ANT shutdown )

    # Watch to see what happens, kill if necessary
    start-stop-daemon --stop --quiet --oknodo --pidfile="$PIDFILE" --exec="$JAVA_HOME/bin/java" --retry=30/TERM/10/KILL/10
    log_end_msg $?
    rm -f "$PIDFILE"
  ;;

  try-restart)
    if $0 status >/dev/null ; then
      $0 restart
    else
      exit 0
    fi
  ;;

  restart|reload|force-reload)
    $0 stop && sleep 2 && $0 start
  ;;

  status)
    pidofproc -p "$PIDFILE" "$JAVA_HOME/bin/java"
    status=$?
    if [ $status -eq 0 ]; then
      log_success_msg "$NAME is running."
    else
      log_failure_msg "$NAME is not running."
    fi
    exit $status
  ;;

  *)
    echo "Usage: $0 {start|stop|restart|try-restart|force-reload|status}"
    exit 2
  ;;
esac
