#!/bin/bash
#
# tkl		Start/Stop the tklite zebra daemon.
# 		Modified for Solaris OS
#
# Kasper Lvschall - 2003/12/12
#
# place this script in /etc/init.d/tkl or make a symlink to it
# since it is required by oai and web harvesting tasks
#
# register runlevel using symlink (only one is really needed)
# ln -s /etc/init.d/tklite /etc/rc2.d/S91tklite

# configuration values

# location of zebra
zebrasrv=/usr/local/tkl/bin/zebrasrv
zebraidx=/usr/local/tkl/bin/zebraidx

# zebra pid file relative to portal dir
pidfile="db/lock/zebrasrv.pid"

# user of the apache process
uid="nobody"

# where to look for portal configuration file
uidhome="/usr/local/tkl"

# set socket connection method
# default can be overrided by creating a db/connect.cfg file
# relative to portal dir with one line containing the method.
# content of a db/connect.cfg could be: unix:db/socket tcp:@:210
connect="unix:db/socket"

if test `whoami` != $uid && test `whoami` != "root" ; then
    echo "error: this script must be run as root (or $uid for indexing)."
    exit 1
fi

# check for zebra
test -x $zebrasrv || exit 0
test -x $zebraidx || exit 0

# find configuration file for tklite
conffile=""
if [ -f /etc/tklite.conf ] 
    then conffile="/etc/tklite.conf"
fi 
if [ -f $uidhome/tklite.conf ] 
    then conffile=$uidhome/tklite.conf
fi

if [ -z $conffile ] 
    then 
    echo  "Toolkit Lite IDZebra server: no configuration files found"
    echo  "  in default location /etc/tklite.conf or $uidhome/tklite.conf" 
    echo  "  no action performed unless configuration files added" 
    echo  "  containing one absolute portal directory per line"
    echo ""
    exit 0 
fi

# read configuration file for portals where to start idzebra
dirs=`grep -v "^#" $conffile`

# define search function for finding to-be indexed directories in each portal
findindexdirs() {
    ids=`cd $1 && grep -l "<searchable>1</searchable>" */directory.tkl | sed s@/directory.tkl@@`
    # cutting newlines out
    indexdirs=""
    for id in $ids ; do
	indexdirs="$indexdirs $id"
    done
}

# perform usual init.d daemon services 
# - set umask for server sockets
umask 0000

case "$1" in
  start)
        echo "Starting tklite: "
	for d in $dirs; do
	    if [ -f $d/db/connect.cfg ] ; then
	        connection=`cat $d/db/connect.cfg`
	    else
		connection=$connect
            fi
	    echo -n "$d " ;
	    if [ ! -f $d/$pidfile ] ; then
		$zebrasrv -w $d -u $uid -c db/zebra.cfg -l db/server.log $connection &
		sleep 1
		if  [ -f $d/$pidfile ] ; then
		    echo -n "started (pid `cat $d/$pidfile`) "
		else
		    echo -n "error "
		fi
	    else
		echo -n "running "
	    fi
            echo
	done
	;;

  stop)
        echo "Stopping tklite: "
	for d in $dirs; do
	    echo -n "$d " ;
	    if  [ -f $d/$pidfile ] ; then
		kill `cat $d/$pidfile`
		sleep 1
                if [ -f $d/$pidfile ] ; then
			kill -9 `cat $d/$pidfile`
			echo -n "-9 "
			if  [ -f $d/$pidfile ] ; then
			    rm  -f $d/$pidfile
			fi
		fi
		echo -n "stopped "
	    else
		echo -n "dead "
	    fi
            echo
	done
	;;

  status)
        echo "Status of tklite:"
	for d in $dirs; do
	    echo -n "$d " ;
            if [ -f $d/$pidfile ] ; then
                echo -n "running (pid `cat $d/$pidfile`)"
                pid=`cat $d/$pidfile`
                running=`ps -p $pid -o pid | tail -1`
                if [ $running != "PID" ] ; then
		    echo " checked ok"
                else
                    echo " dead"
                fi
            else
                echo "no-pid found"
            fi
	done
	;;

  restart|reload)
	$0 stop
	sleep 1
	$0 start
	;;

  reindex|index)
	# make sure that indexing is done as user $uid
	if test `whoami` != $uid; then
	    su $uid -c "$0 $*"
	    exit 0
	fi

	echo "Indexing tklite: "
	if  [ $2 ] ; then
	    if [ ! -d $2 ] ; then
		echo "error: no portal at $2"
		exit 1;
	    fi
	    # only indexing this one portal
	    dirs=$2
	fi
	for d in $dirs; do
	    findindexdirs "$d"
	    if [ $3 ] ; then
		# only indexing this directory
		indexdirs=$3
		if [ ! -d $d/$indexdirs ] ; then
		    echo "error: no directory $3 in portal $d"
		    exit 1;
		fi
	    fi
	    echo -n "$d "
	    echo -n "($indexdirs )"
	    ( cd $d ; 
                if [ $1 = "index" ] ; then
                    rm db/index.log
		    $zebraidx -l db/index.log -c db/zebra.cfg init ;
                    echo -n " clean"
                fi
		$zebraidx -L -l db/index.log -c db/zebra.cfg update $indexdirs ;
		$zebraidx -l db/index.log -c db/zebra.cfg commit ;
                echo " ok"
	    )
	done
	;;

  *)
	echo "Usage: $0 {start|stop|reload|status|restart}"
	echo "or     $0 {index|reindex} [/path/to/portal [sub/dir]]"
	exit 1
esac

exit 0
