#!/bin/sh
#
# OperStats Crontab Script - Restart OperStats if needed, move generated files
#
# Instructions for installing this script can be found in the crontab file.

# Change this to the directory for OperStats (This is the default)
cd /home/user/operstats

# If you're using the web content, enter the directory you want these files
# to go:
DEST="/home/user/public_html"

# Check for channels.php. Move it if found, and chmod it.
if [ -f channels.php ] ; then
  mv channels.php $DEST
  chmod 644 $DEST/channels.php
fi

# Check for serverstats.php. Move it if found, and chmod it.
if [ -f serverstats.php ] ; then
  mv serverstats.php $DEST
  chmod 644 $DEST/serverstats.php
fi

# Check for tldstats.php. Move it if found, and chmod it.
if [ -f tldstats.php ] ; then
  mv tldstats.php $DEST
  chmod 644 $DEST/tldstats.php
fi

# Check for stats.php. Move it if found, and chmod it.
if [ -f stats.php ] ; then
  mv stats.php $DEST
  chmod 644 $DEST/stats.php
fi

# Check for OperStats and restart it if needed
if [ -f operstats.pid ] ; then
  PID=`cat operstats.pid`
  if `kill -CHLD $PID >/dev/null 2>&1`; then
    exit 0
  fi
  rm -f operstats.pid
fi

./operstats
exit 0
