#!/bin/sh

# This script runs percollator.se with the proper options for our site.

# Set these variables.  AWK is where your best version of awk lives.  This
# should be preferably gawk, then nawk, and finall awk.  STATSDIR is the
# base directory for the statistics.  Underneath STATSDIR is a bin/ and
# stats_percollator/ directory.
AWK=/usr/public/bin/gawk
STATSDIR=/home/bzajac/Code/MRTG/bzajac/percollator

# Get the real hostname without any trailing .*.
uname=`/usr/bin/uname -n | /usr/bin/cut -d. -f1`

# If this is running on a mail machine, then we need to write our files
# into /export/home/stats_percollator/HOSTNAME, otherwise into the
# STATSDIR/stats_percollator/HOSTNAME directory.
if expr $uname : mail >/dev/null; then
  OUTDIR=/export/home/stats_percollator/$uname
else
  OUTDIR=$STATSDIR/stats_percollator/$uname
fi
echo "Writing data into $OUTDIR/"

# Choose the correct web access log.
if test -r /export/home/JavaWebServer1.1.1/logs/javawebserver/access_log; then
  NCSAHTTPLOG=/export/home/JavaWebServer1.1.1/logs/javawebserver/access_log
else
  NCSAHTTPLOG=/export/home/weblog/access_log
fi
echo "Using www access log file $NCSAHTTPLOG"

# Export the environmental variables.
export NCSAHTTPLOG OUTDIR

# Check if percollator is already running.
pids=`/usr/ucb/ps auxww | $AWK '/percollator.se/ && !/awk/ {print $2}'`
if test "$pids" != ""; then
  echo "Percollator already running.  Exiting."
  exit 0
fi

# Cd to / so that any mounted /home filesystems can be unmounted.
cd /

# Create the output directory if it doesn't exist yet.
if test ! -d $OUTDIR; then
  echo "Creating $OUTDIR/"
  mkdir -p $OUTDIR
fi

if test ! -d $OUTDIR; then
  echo "Unable to create $OUTDIR/" 1>&2
  exit 1
fi

# Kill any running percollators.
$STATSDIR/bin/stop_percol

# Now start the logging.
echo "Starting logging"
/usr/public/packages/RICHPse/bin/se $LE_PATCH -DWATCH_OS -DWATCH_HTTPD $STATSDIR/bin/percollator.se &

# Write the PID of percollator to a file to make killing easier.
pid=$!
echo $pid > $OUTDIR/percollator.pid

# Sleep for a couple of seconds to allow any percollator warnings to appear.
sleep 5
