#!/bin/sh

##############################
# DIRECTORY LAYOUT

BASEDIR=/export/db/
BINDIR=$BASEDIR/bin
MR=$BINDIR/mr

##############################
# REFLECTORS CONFIGURATION
#
# List format:
#
# <mirror name> <listen port> <mirror server> <its port> <filter>
#                
mirror_list="\
mr_ripe 5555 joshua.ripe.net 43 $BINDIR/ripe2rpsl\n\
mr_radb 5556 rpsl.merit.edu  43 $BASEDIR/scripts/mirror/radb_filter.pl
"

stop_mirrors() {
	echo $mirror_list | while read mr_name rest
        do
		echo "mr_name=" $mr_name
	 pid_file=$BINDIR/${mr_name}.pid
         if test -f "$pid_file"
         then
	   PID=`cat $pid_file`
           echo "Killing mirror reflector with pid $PID"
	   if /usr/bin/kill -0 $PID > /dev/null 2> /dev/null
           then
             if /usr/bin/ps -ef | grep $MR | grep " $PID " > /dev/null 
             then 
	       kill $PID
               sleep 1
	       if /usr/bin/kill -0 $PID >/dev/null 2> /dev/null
               then
	         echo "WARNING: mirror reflector $mr_name with pid $PID still running"
	         sleep 1
	       fi
	       rm $pid_file
	     else
              echo "WARNING: process with pid $PID does not seem to be mirror reflector"
	      rm $pid_file
	     fi  
	   else  
	     echo "WARNING: process with pid $PID does not running"
	     rm $pid_file
	   fi  
	 else
	   echo "No mirror reflector pid file found. Looked for $pid_file."    
	 fi
	done
}

start_mirrors() {
  echo $mirror_list | while read mr_name l_port mirror port filter rest
  do
	if test ! -f "${filter}"
        then
          echo $0 ":$filter does not exist";
          exit 1;
        fi
        pid_file=$BINDIR/${mr_name}.pid
	if test -f $pid_file
        then
          PID=`cat $pid_file`
          if /usr/bin/kill -0 $PID > /dev/null 2> /dev/null
          then
            if /usr/bin/ps -ef | grep $MR | grep " $PID " > /dev/null
              then    # The pid contains a mr process
              echo "A $MR process already exists"
              exit 1
            fi
          fi
          rm -f $pid_file
          if test -f $pid_file
          then
           echo "Fatal error: Can't remove the pid file: $pid_file"
           echo "Please remove it manually and start $0 again"
           echo "mirror reflector $mr_name not started"
           stop_mirrors;
          fi
         fi

        echo "Starting mirror reflector $mr_name"
	echo $MR -l $l_port -h $mirror -p $port -f $filter 
	$MR -l $l_port -h $mirror -p $port -f $filter  >>$BINDIR/${mr_name}.log  2>&1 &
        sleep 2;
	mr_pid=$!
	if kill -0 $mr_pid >/dev/null 2> /dev/null
	then
	  pid_file=$BINDIR/${mr_name}.pid
	  echo $! >$pid_file;	
	else
	  echo $0 ":mirror reflector $mr_name cannot be started";
          stop_mirrors;
        fi 
	 	
   done
}

# start | stop
mode=$1

LD_LIBRARY_PATH=/export/db/src/glib-ucbcc/lib:/usr/local/mysql/lib/mysql
export LD_LIBRARY_PATH

# Safeguard (relative paths, core dumps..)
cd $BASEDIR

case "$mode" in
  'start')
      start_mirrors;;
  'stop')
      stop_mirrors;;
  *)
    # usage
    echo "usage: $0 start|stop"
    exit 1
esac
    

