#!/bin/sh
#
# This file is in the public domain

# Process options - or how to do getopt(3) in shell scripts ;-)
DEBUG=0
SINGLE=0
USAGE=0
PIDFILE=
while [ -n "$1" ]	# Loop as long as $1 is nonempty
do
	case $1 in
		-d)
			DEBUG=1
			shift
			;;
		-s)
			SINGLE=1
			shift
			;;
		-o)
			# Used to redirect fd 2 (where debugging output
			# normally goes) to a file - same as server itself
			if [ ! "$2" = "" ]
			then
				exec 2>$2
				shift;
			else
				echo ERROR: Option -o requires an argument 1>&2
				USAGE=1
			fi
			shift
			;;
		-p)
			# Used to write pid into a file, for log rotation
			if [ ! "$2" = "" ]
			then
				PIDFILE=$2
				shift
			else
				echo ERROR: Option -p requires an argument
				USAGE=1
			fi
			shift
			;;
		-h)
			USAGE=1
			shift
			;;
		*)
			# If this is not the last argument, it's an
			# unknown option - complain. Otherwise it's
			# the positional argument, and we're done
			# with the loop.
			if [ -n "$2" ] 	    # If $2 is nonempty, $1 is not last
			then
				echo ERROR: Unknown option $1 1>&2
				USAGE=1
				shift
			else
				break
			fi
			;;
	esac
done

if [ -z "$1" ]
then
	echo ERROR: No destination file specified 1>&2
	USAGE=1
fi
DESTFILE=$1

if [ $USAGE = 1 ]
then
	echo "Usage: radlogger [-d][is][-o file][-p pidfile] destination" 1>&2
	echo "       radlogger -h" 1>&2
	exit 1
fi

# Open output file (positional arg) for append on fd 3
exec 3>>$DESTFILE
trap "exec 3>>$DESTFILE" HUP

if [ -n "$PIDFILE" ]
then
	echo $$ >$PIDFILE
	trap "rm $PIDFILE" TERM
fi

# Loop, answering requests
while : 
do
	# Read message
	OUTPUT=
	IFS='='
	while read ATR VAL && [ -n "$VAL" ]
	do
		IFS=' '
		if [ "str" = "$ATR" ]
		then		
			# Remove quotes
			eval VAL=$VAL
			# Add header from 'str' attribute in front 
			OUTPUT="$VAL
$OUTPUT"
		else
			# Add pair. Note the real tab before $ATR
			OUTPUT="$OUTPUT	$ATR = $VAL
"
		fi
		IFS='='
	done
	IFS=' '

	# Show debugging output if requested
	[ $DEBUG = 1 ] && echo "radlogger[$$]: $OUTPUT" 1>&2

	# Log output to file
	if [ $SINGLE = 1 ]; then
		echo -n "$OUTPUT" 1>&3
	else
		echo "$OUTPUT" 1>&3
	fi

	# Reply
#	sleep 1		# testing only - don't worry, we're race-free ;-)
	echo "int=1
"

done

