#!/bin/bash
# (C) 2004 Mark Boddington, http://www.badpenguin.co.uk
# Licensed Under the GNU GPL Version 2.
#
# Process a Standard NCSA logfile and print the number of hits
# and unique visitors to standard output.

today=$( date +%Y%m%d )
tmpfile=/tmp/Logstats-${today}.tmp${RANDOM}

getuniq()
{
	year=$(date +%Y)
	echo -e "VisitDate${FiSe}Unique${FiSe}Total"
	cat $1 | sed -e "s#^\(.*\) - - \[\(../.../${year}\):.*#\1 \2#"| sort -u > $tmpfile
	for day in $( cat $tmpfile | awk '{ print $2 }' | sort -u )
	do
		count=$(grep $day $tmpfile | wc -l | awk '{ print $1 }' )
		total=$(grep $day $1 | wc -l | awk '{ print $1 }' )
		echo -e "${day}${FiSe}${count}${FiSe}${total}"
	done
}

helpexit()
{
	cat <<-EOF
	$0 Usage:

	This script takes a NCSA standard transfer log and displays the total number of hits
	for each day included in the logfile.
	Output is tab delimited by default, you can use -c to get comma delimitation.

	$0 [-c] <Log file>
	EOF
	exit
}

# Do some basic error checking
[ -z $1 ] && helpexit
if [ "$1" == "-c" ]
then
	if [ -z $2 ]
	then
		helpexit
	fi
 	FiSe=","
	shift
elif [ "$1" == -h ]
then
	helpexit
else
	FiSe="\t"
fi

# Process the log
if [ -f $1 ]
then
	getuniq $1
else
	echo "Sorry the file specified is not a regular file or it does not exist"
fi

# Delete our tmpfile if we own it and its a regular file.
[ -f $tmpfile -a -O $tmpfile ] && rm $tmpfile


