#!/bin/sh

#####################
# Site-specific parameters

# E-mail address where crash notifications are sent
NOTIFY=dbase-errs@ripe.net

# Configuration file
CONFIG=$1

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

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


# Host-specific settings
if [ `hostname` = "cypress" ]; then
  LD_LIBRARY_PATH=/ncc/dbwork/platforms/i386-pc-solaris2.7/lib:/usr/local/mysql/lib/
  export LD_LIBRARY_PATH
fi

if [ `hostname` = "yucca" ]; then
  LD_LIBRARY_PATH=/ncc/dbwork/platforms/sparc-sun-solaris2.7/lib:/usr/local/mysql/lib/
  export LD_LIBRARY_PATH
fi

if [`hostname` = "orange.ripe.net" ]; then
  LD_LIBRARY_PATH=/ncc/dbwork/platforms/sparc-sun-solaris2.8/lib:/usr/local/lib/mysql
  export LD_LIBRARY_PATH
fi

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

# Start mirror reflector

echo "Starting mirror reflector..."
../mr/mr -l 5555 -h joshua.ripe.net -p 43 -f ../rip/ripe2rpsl >mr.log 2>&1 &
sleep 2

if  ps -p $! | grep $! | grep -v grep 
then
 echo "MR started"
else
 echo "Cannot start mirror reflector. Exiting"
 exit 1
fi
 

while echo RIP server started `date` >> rip.log
do

num=`find . -name "core*" -mtime -1 | wc -l `
if [ $num -le $CRASHES ]
then

  ./whois_rip $CONFIG
  if [ -f ${CONFIG}.q ]
  then
   CONFIG=${CONFIG}.q
  fi

  mv core core.`date '+%m%d_%H%M%S' `
  /usr/bin/echo "Reimp whoisd crashed & restarted\n" `date` | mailx -s "Reimp whoisd 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

