#!/bin/bash
#
# init script for YaCy
#
# Provided by Matthias Kempka, 26.12.2004
# Updated by Florian Richter, 17.7.2008
#
### BEGIN INIT INFO
# Provides:          yacy
# Required-Start:    $network
# Required-Stop:     $network
# Default-Start:     3 5
# Default-Stop:      0 1 6
# Short-Description: Distributed web search engine
# Description:       yacy is a distributed search engine
#                    config-file is /etc/yacy/yacy.conf
### END INIT INFO

NAME="yacy"
DESC="YaCy P2P Web Search"
YACY_HOME="/usr/share/yacy"
DATA_HOME="/var/lib/yacy"
PID_FILE="/var/run/yacy.pid"
USER=yacy


# Set this to the maximum number of seconds the script should try to shutdown
# yacy. You might want to increase this on slower peers or for bigger
# databases.
SHUTDOWN_TIMEOUT=50

# Default niceness if not set in config file
NICE_VAL=0

JAVA_ARGS="-server -Djava.net.preferIPv4Stack=true -Djava.awt.headless=true -Dfile.encoding=UTF-8 -Dsolr.directoryFactory=solr.MMapDirectoryFactory"

. /etc/rc.status
rc_reset

if [ "$(id -u)" != "0" -a "$(whoami)" != "$USER" ] ; then
	echo "please run this script as root!"
	exit 4
fi

JAVA=$(which java 2> /dev/null)
if [ ! -x "$JAVA" ]; then
	echo "The 'java' command is not executable."
	echo "Either you have not installed java or it is not in your PATH"
	if [ $1 == "stop" -a $2 == "--force" ]; then exit 0; else exit 1; fi
fi

cd $YACY_HOME

#get javastart args
if [ -s DATA/SETTINGS/yacy.conf ]
then
	# startup memory
	for i in Xmx Xms; do
		j=$(grep javastart_$i DATA/SETTINGS/yacy.conf | sed 's/^[^=]*=//');
		if [ -n $j ]; then JAVA_ARGS="-$j $JAVA_ARGS"; fi;
	done
	
	# Priority
	j=$(grep javastart_priority DATA/SETTINGS/yacy.conf | sed 's/^[^=]*=//');

	if [ ! -z "$j" ];then
		if [ -n $j ]; then NICE_VAL=$j; fi;
	fi
	
else
	JAVA_ARGS="-Xmx120m -Xms120m $JAVA_ARGS"
fi

# generating the proper classpath
CP=/usr/share/java/yacy.jar:$YACY_HOME/htroot
for name in /usr/share/java/yacy/*.jar; do
	CP=$CP:$name
done
CLASSPATH=$CP

if [ -f $PID_FILE ]; then
	pid=$(cat "$PID_FILE")
	pidno=$( ps ax | grep "$pid" | awk '{ print $1 }' | grep "$pid" )
fi

RETVAL=0
case "$1" in
  start)
	if [ -n "$pidno" ]; then
		echo "already running"
		exit 0
	fi

	echo -n "Starting $DESC. "
	ARGS="$JAVA_ARGS -classpath $CLASSPATH net.yacy.yacy"

	cmdline="$JAVA $ARGS"
	if [ "$(whoami)" != "$USER" ]; then
		nice -$NICE_VAL sudo -u yacy $cmdline &>/dev/null &
	else
		nice -$NICE_VAL $cmdline &>/dev/null &
	fi
	echo $! >$PID_FILE
	sleep 1
	ps ax|grep "^ *$(cat $PID_FILE)" > /dev/null
	if [ "$?" == "0" ]; then

		rc_status -v
		RETVAL=0
		chown yacy:root $PID_FILE
	else

		rc_failed
		rc_status -v
		RETVAL=1
	fi
	;;
	
  stop)
	if [ -n "$pidno" ]; then

		echo -n "Shutting down $DESC: "
		cd $YACY_HOME
		cmdline="$JAVA $JAVA_ARGS -cp $CLASSPATH net.yacy.yacy -shutdown"
		if [ "$(whoami)" != "$USER" ]; then
			sudo -u yacy $cmdline &>/dev/null & 
		else
			$cmdline &>/dev/null & 
		fi
		shutdown_pid=$!

		timeout=$SHUTDOWN_TIMEOUT
		while [ -n "$pidno" ]; do
			let timeout=$timeout-1
			if [ $timeout -eq 0 ]; then
				kill -9 $pid &>/dev/null
				break
			fi
			echo -n  "."
			sleep 1
			pidno=$( ps ax | grep $pid | awk '{ print $1 }' | grep $pid )
		done

		# dont forget to kill shutdown process if necessary
		shutdown_pid=$( ps ax | grep $shutdown_pid | awk '{ print $1 }' | grep $shutdown_pid )
		if [ -n "$shutdown_pid" ] ; then
			kill -9 $shutdown_pid &>/dev/null
		fi

		if [ "$2" != "--leave-pidfile" ]; then
			rm $PID_FILE
		fi
		cd - >/dev/null

		rc_status -v
		exit 0
	fi
	echo "not running."
	;;

  restart)
	$0 stop --leave-pidfile
	sleep 3
	$0 start
	;;
  reload)
	$0 restart
	;;
  force-reload)
	$0 restart
	;;
  status)
	# needed by Fedora
	if [ -n "$pidno" ]; then
		echo "is running."
		exit 0
	else
		if [ -f $PID_FILE ]; then
			echo "is dead, but pid file exists."
			exit 1
		else
			echo "is not running."
			exit 3
		fi
	fi
	;;
  *)
	echo "Usage: $0 {start|stop|restart}" >&2
	exit 1
	;;
esac

exit $RETVAL
