#!/bin/bash
#
#################################################################################
#										#
#	Pathalizer 0.5 processing script 					#
#	Release $Id: pathalizer,v 1.3 2005/01/02 13:45:56 raboofje Exp $								#
#										#
#	Copyright (c) 2002, 2003 by Arnout Engelen <arnouten@bzzt.net> 		#
#	parts of this script by Luc de Louw <luc@delouw.ch>			#
#										#
#	License: GNU General public license					#
#	http://www.gnu.org/licenses/gpl.html					#
#										#
#################################################################################

# Initialize the default values
OUTDIR=`pwd`
FORMAT="pdf"
CONFIG="/opt/local/etc/pathalizer.conf"
QUIET=false

# Parse the options and check for valid args
while getopts "d:o:f:hq" option
do
	case "$option"
	in
		d)	OUTDIR=$OPTARG;;
		o)	FORMAT=$OPTARG;;
		f)	CONFIG=$OPTARG;;
		q)	QUIET=true;;
		*)	echo "Pathalizer Copyright (c) 2002-2005  by Arnout Engelen,"
			echo "parts of this script by Luc de Louw"
			echo ""
			echo "Usage: pathalizer [-d] [-o] [-f] logfile [logfile ..]"
			echo ""
			echo "       -d | --directory means the output directory"
			echo ""
			echo "       -o | --output means the Output format valid values are:"
			echo "		ps for Postscript"
			echo "		pdf for Acrobat PDF"
			echo "		png for Portable Network Graphic"
			echo "		jpg for jpeg picture format (To be done)"
			echo "		dot for graphviz dot format"
			echo "		events for pathalizer events format"
			echo ""
			echo "       -f specifies where to search the config file"
			echo "          default is /opt/local/etc/pathalizer.conf"
			echo ""
			echo "	     logfile is the absolute or relative path of the logfile"
			echo "	        to be processed. Logfiles may be gzipped or bzip2'ed,"
			echo "          they will be uncompressed on the fly."
			echo ""
			exit 1;;
	esac
done

# Check for logfilename provided
if [ "$OPTIND" -gt "$#" ]
then
	echo "Missing logfilename - use -h to get help."
	exit 2
fi

shift $(($OPTIND - 1))

LOGFILE=$1
LOGFILENAME=`basename $LOGFILE`

# Check if logfile exists
if [ ! -f $LOGFILE ]
then
	echo "Logfile does not exist"
	exit 3
fi

case "$FORMAT" 
in
	ps) ;;
	pdf) ;;
	png) ;;
	dot) ;;
	events) ;;
	*) echo "Invalid output format"
	exit 4
esac
	
# Check for the programs needed

pnmtopng=`which pnmtopng 2>/dev/null`

if [ "$?" -ne 0 ]
then
	echo
	echo "pnmtopng not found. Is netpbm installed?"
	echo
	exit 5
fi

NETPBMPATH=`dirname $pnmtopng 2>/dev/null`

# Printing parameters

# Check for output directory
if [ ! -d $OUTDIR ]
then
	echo "Outputdirectory does not exist"
	exit 6
else
	echo "Directory for output is: $OUTDIR"
fi

echo "Output format is: $FORMAT"

# Print warning if the configfile, eighter supplied or defaultlocation does not exist
if [ -f $CONFIG ]
then
	echo "Using configfile: $CONFIG"
else
	echo "Configfile $CONFIG not found, falling back to default values"
fi
echo "logfile to process is $1"

# Generating the events file

echo "Ordering logfile by session and by date"
#cat $LOGFILE | apache2events | sort > $OUTDIR/$LOGFILENAME.events
#cat $LOGFILE | apache2events $CONFIG | sort > /tmp/$LOGFILENAME.events

for i in $* 
  do
  if echo $i | grep -q '.gz$'; then
    zcat $i
  else if echo $i | grep -q '.bz2$'; then
      bzcat $i
    else
      cat $i
    fi
  fi
done | apache2events $CONFIG | sort > /tmp/$LOGFILENAME.events

echo "Generating graph edges"
event2dot /tmp/$LOGFILENAME.events $CONFIG > /tmp/$LOGFILENAME.dot

echo "Generating postscript output"
dot -Tps /tmp/$LOGFILENAME.dot -o /tmp/$LOGFILENAME.ps

case "$FORMAT" in
	ps)
		cp /tmp/$LOGFILENAME.ps $OUTDIR;;
	pdf) 
		echo "Converting to Acrobat PDF"
		ps2pdf /tmp/$LOGFILENAME.ps $OUTDIR/$LOGFILENAME.pdf;;
	png)
		echo "Converting to png"
		ps2img --pbmpath=$NETPBMPATH --dpi=350 --rotate=-90 /tmp/$LOGFILENAME.ps $OUTDIR/$LOGFILENAME.png;;
	dot)
		cp /tmp/$LOGFILENAME.dot $OUTDIR;;
	events)
		cp /tmp/$LOGFILENAME.events $OUTDIR;;
	ps) ;;
	*)	echo "Invalid output format"
		exit 4;;
esac

rm /tmp/$LOGFILENAME.events
rm /tmp/$LOGFILENAME.dot
rm /tmp/$LOGFILENAME.ps

exit 0
