#!/bin/sh

# Wrapper for the whoisd server.
#
# This wrapper sets LD_LIBRARY_PATH, and then starts the whoisd server.
# At every crash, a message is sent to the $NOTIFY address and the server
# is trestarted; if the number of crashes exceeds $CRASHES
# a final message is sent and the server is not restarted.

#####################
# Site-specific parameters
# Change this variables as needed


# E-mail address where crash notifications are sent
NOTIFY=my-dbm@mydb.net

# Configuration file
CONFIG=$1

# Number of crashes per day before the rip stops
CRASHES=10

# Logfile
RIPLOG=rip.log


# Set LD_LIBRARY_PATH before executing

# glib-config
GLIBCONF=/usr/local/bin/glib-config
GLIBLIB=`$GLIBCONF --libs`

# MySQL lib directory
MYSQLLIB=/usr/local/mysql/lib

# Generic LD_LIBRARY_PATH
LD_LIBRARY_PATH=$GLIBLIB:$MYSQLLIB
export LD_LIBRARY_PATH

#####################

# Start the server

while echo RIP server started `date` >> $RIPLOG
do

num=`find . -name "core*" -mtime -1 | wc -l `

if [ $num -le $CRASHES ]
then

  ./whois_rip $CONFIG

  mv core core.`date '+%m%d_%H%M%S' `
  /usr/bin/echo "Reimp whoisd crashed & restarted\n" `date` | mailx -s "Reimp wh
oisd crashed & restarted" $NOTIFY
  sleep 1

else

  /usr/bin/echo "Number of reimp crashes exceeded $CRASHES\n" `date` | mailx -s 
"WARNING: Reimp is stopped and not restarted" $NOTIFY
  exit 1

fi

done

