#!/bin/sh
#
# ufdbGrab - grab info about the OS, libc, squid and ufdbGuard config
#
# the output is meant to be send to support@urlfilterdb.com in case that
# there are severe errors with ufdbGuard.
#
# RCS: $Id: ufdbGrab,v 1.8 2005/11/01 19:33:22 root Exp root $

if [ $# -ne 2 ]
then
   echo "usage: ufdbGrab <config-dir> <log-dir>"
   exit 1
fi

TMPDIR=${TMPDIR:-/tmp}
export TMPDIR

echo "*** warning: temporary space in $TMPDIR is used"
echo "    If you run out of space, set the environment variable TMPDIR"
echo "    to a location where you have enough space."
echo

CONFDIR="$1"
LOGDIR="$2"

VERSION="$Revision: 1.8 $"
VERSION=${VERSION#* }
VERSION=${VERSION% *}

function echolog
{
   echo "$*"
   echo "$*" >> $TT/log
}

function logcmd
{
   echo >> $TT/log
   echolog "running command $* ..."
   eval $* >> $TT/log
}

function trycopyfile
{
   file="$1"
   if [ -f "$file" ]
   then
      echolog "Copying $file ..."
      gzip < $file > $TT/`basename $file`.gz
   fi
}

TTDIR=ufdbGrab.$$
TT=$TMPDIR/$TTDIR
rm -f $TT/log

mkdir $TT
if [ ! -d $TT ]
then
   echo "cannot create temporary directory $TT"
   exit 1
fi

echolog "ufdbGrab version $VERSION with config=$CONFDIR log=$LOGDIR"
logcmd "ls -l $CONFDIR"
ETCDIR=$CONFDIR

if [ -d $CONFDIR/etc ]
then
   logcmd "ls -l $CONFDIR/etc"
   ETCDIR=$CONFDIR/etc
fi

trycopyfile /etc/hosts

trycopyfile $ETCDIR/squid.conf
trycopyfile $ETCDIR/ufdbGuard.conf
trycopyfile $ETCDIR/ufdbguard.conf
trycopyfile `pwd`/Makefile

logcmd "ls -l $LOGDIR" 

trycopyfile $LOGDIR/ufdbGuard.log 
trycopyfile $LOGDIR/ufdbGuard.log.1 
trycopyfile $LOGDIR/ufdbguardd.log
trycopyfile $LOGDIR/ufdbgclient.log
trycopyfile $LOGDIR/squidGuard.log

if [ -f $ETCDIR/squid.conf ]
then
   # cache.log
   SQUIDLOGFILE=`grep '^cache_log' $ETCDIR/squid.conf 2>/dev/null | awk '{print $2}'`
   if [ "$SQUIDLOGFILE" = "" ]
   then
      if [ -f $CONFDIR/logs/cache.log ]
      then
         SQUIDLOGFILE=$CONFDIR/logs/cache.log
      fi
   fi
   trycopyfile "$SQUIDLOGFILE"

   # access.log
   SQUIDLOGFILE=`grep '^cache_access_log' $ETCDIR/squid.conf 2>/dev/null | awk '{print $2}'`
   if [ "$SQUIDLOGFILE" = "" ]
   then
      if [ -f $CONFDIR/logs/access.log ]
      then
         SQUIDLOGFILE=$CONFDIR/logs/access.log
      fi
   fi
   trycopyfile "$SQUIDLOGFILE"
fi

if [ -f $ETCDIR/ufdbGuard.conf ]
then
   DBHOME=`grep '^dbhome' $ETCDIR/ufdbGuard.conf 2>/dev/null | awk '{print $2}'`
   if [ "$DBHOME" = "" ]
   then
      if [ -d $CONFDIR/blacklists ]
      then
         DBHOME=$CONFDIR/blacklists
      fi
   fi
   trycopyfile $DBHOME/alwaysdeny/domains
   trycopyfile $DBHOME/alwaysallow/domains
fi

date > $TT/date
uname -a > $TT/uname
ifconfig -a > $TT/ifconfig 2>/dev/null

# we are finished collecting info
echolog "Generating a compressed tar file ..."
cd $TMPDIR
tar cf - $TTDIR | gzip > $TTDIR.tar.gz
rm -rf $TTDIR

echo
echo "The compressed output of ufdbGrab is in $TMPDIR/$TTDIR.tar.gz"

exit 0


