#! /bin/bash -e

# This probably needs configuring, as all syslog implementations are different
# comment out if broken and don't care.
# exit 0;

export LC_ALL=C # syslog time "should" be in C locale.

# Confish.
LOGS_KEEP_FOR_X_DAYS=91 # 13 * 7 == quarter of a year

prefix="/usr" # localstatedir can use it...

# Want to include all old files depending on how syslog/logrotate is configured
IN_LOG="/var/log/messages" # Main syslog file

if [  -r "/var/log/syslog" ]; then
  IN_LOG="/var/log/syslog" # Main syslog file
fi

LOGS_END_NUM=100

AND_HTTPD_LOGS_DIR=/var/log/and-httpd

# If you are using the logrotate dateext option...
LOGS_DATEEXT=false

if [ -r /etc/sysconfig/and-httpd ]; then
  . /etc/sysconfig/and-httpd
fi

# Let them just configure $IN_LOGS if they want
if [ "x$IN_LOGS" = "x" ]; then
  IN_LOGS="$IN_LOG"

  if $LOGS_DATEEXT; then
    for i in $(seq 1 $LOGS_END_NUM); do
      # Not as long, if you do weekly/monthly rotations
      dt=`date --date "$i days ago" +'%Y%m%d'`
      IN_LOGS="$IN_LOG.$dt.gz $IN_LOG.$dt $IN_LOGS" 
    done
  else
    for i in $(seq 1 $LOGS_END_NUM); do
      IN_LOGS="$IN_LOG.$i.gz $IN_LOG.$i $IN_LOGS" 
    done
  fi
fi

# filename suffix format
dto="`date +'%F'`"    # "2005-12-01"
pdto="`date --date='1 day ago' +'%F'`"

# syslog format
dtg="`date +'%b %e'`" # "Dec  1"
pdtg="`date --date='1 day ago' +'%b %e'`"

grep=/usr/share/and-httpd-0.99.11-tools/and-httpd-syslog-grep
comb=/usr/share/and-httpd-0.99.11-tools/and-httpd-syslog2apache-httpd-combined-log

if [ ! -d $AND_HTTPD_LOGS_DIR ]; then
  mkdir -p $AND_HTTPD_LOGS_DIR
fi

function grep_comb_log()
{
  if [ -r "$AND_HTTPD_LOGS_DIR/and-httpd-$2" ]; then
    TMPFILE=`mktemp "$AND_HTTPD_LOGS_DIR/and-httpd-$2.XXXXXXXXXX"` || exit 1
    usetmp=true
  else
    TMPFILE="$AND_HTTPD_LOGS_DIR/and-httpd-$2"
    usetmp=false
  fi

  $grep --skip-before "$2 at 00:00:00" --skip-missing \
        --output "$TMPFILE" "^$1" $IN_LOGS

  if $usetmp; then
    if cmp -s "$TMPFILE" "$AND_HTTPD_LOGS_DIR/and-httpd-$2"; then
      rm -f   "$TMPFILE"
    else
      mv      "$TMPFILE" "$AND_HTTPD_LOGS_DIR/and-httpd-$2"
    fi
  fi

  $comb --fast-check \
        --sync-file "$AND_HTTPD_LOGS_DIR/combined-and-httpd-$2" \
        --output    "$AND_HTTPD_LOGS_DIR/combined-and-httpd-$2" \
                    "$AND_HTTPD_LOGS_DIR/and-httpd-$2"
}

# Do arbitrary dates, mainly for testing...
if [ "x$1" != "x" ]; then
  dto=`date --date="$1" +'%F'`
  dtg=`date --date="$1" +'%b %e'`

  grep_comb_log "$dtg" "$dto"
  exit 0
fi

if [ ! -r "$AND_HTTPD_LOGS_DIR/and-httpd-$dto" ]; then
# If "todays" file doesn't exist, do yesterdays once more
  grep_comb_log "$pdtg" "$pdto"

  ln -sf "and-httpd-$dto" \
         "$AND_HTTPD_LOGS_DIR/and-httpd"
  ln -sf "combined-and-httpd-$dto" \
         "$AND_HTTPD_LOGS_DIR/combined-and-httpd"
fi

grep_comb_log "$dtg" "$dto"

if [ $LOGS_KEEP_FOR_X_DAYS -gt 0 ]; then
  # Remove old log files...
  find $AND_HTTPD_LOGS_DIR -mtime "+$LOGS_KEEP_FOR_X_DAYS" -print0 | \
  xargs -0 rm -f
fi
