#!/bin/sh

set -e

simpleword() {
	echo "$1" | tr '[A-Z]' '[a-z]' | egrep '^[a-z][a-z0-9-]+$' || exit 1
}

uriunescape() {
	echo "$1" | perl -e 'use URI::Escape; print lc(uri_unescape(<STDIN>));'
}

urlencode() {
	echo "$1" | perl -e 'use URI::URL; print URI::URL->new(<STDIN>)."\n";'
}

phone=`uriunescape "$1" | sed -e 's/\+/ /g'`; shift
set -- `uriunescape "$@" | sed -e 's/\+/ /g'`
app=`simpleword "$1"`; shift

case "$app" in
    bruger|brugere)
	users=`finger -s "$@" | awk '{print $1}' | tail -n +2`
	usercount=`echo $users | grep -qc .`
	if [ "$usercount" = "1" ]; then
		finger -mp "$users" | egrep -v -e '^(Directory|Last)'
	else
		localuserinfo --mail --custom='%username%: %fullname% <%mailaddress%>' $users
	fi
	;;
    adresse|adr|nummer|nr|krak|kraks|dgs|gulesider|degulesider)
	buffer=
	for word in "$@"; do
		if [ -n "$word" ]; then
			buffer="${buffer:+$buffer+}$word"
		fi
	done
	query="$(urlencode "$buffer")"
	wget -q -O- "http://www.krak.dk/Person/Resultat.aspx?Who=$query&WhoOnlySearch=false&Sort=Name" \
		| html2text -nobs -width 9999 \
		| egrep '^(\*\*.*\*\*|[^[:space:]]+_[^[:space:]]+|[[:space:]]+\* (Tlf|Mobil): .*)$' \
		| sed -e 's/^\*\*\*\*\* \(.*\) \*\*\*\*\*$/Person: \1/' -e 's/_/ /g' -e 's/ \+\* \(Tlf\|Mobil\): \+//' \
		| egrep -v '^(\*|Flere s|Udvid s)' \
		|| true
	wget -q -O- "http://www.krak.dk/Firma/Resultat.aspx?Query=$query" \
		| html2text -nobs -width 9999 \
		| egrep '^(\*\*.*\*\*|[^[:space:]]+_[^[:space:]]+|[[:space:]]+\* (Tlf|Mobil): .*)$' \
		| sed -e 's/^\*\*\*\*\* \(.*\) \*\*\*\*\*$/Firma: \1/' -e 's/_/ /g' -e 's/ \+\* \(Tlf\|Mobil\): \+//' \
		| egrep -v '^(\*|Flere s|Vis kun resultater|Se resultater|\[Se firmaprofil for|Se alle webresultater)' \
		|| true
	;;
    tog|togrejse)
	buffer=
	viacount=0
	for word in "$@"; do
		case "$word" in
		    fra)
			buffer="from"
			eval $buffer=
			;;
		    til)
			buffer="to"
			eval $buffer=
			;;
		    via)
			if [ $viacount -gt 0 ]; then
				eval lastviabuffer=\"\${$word$viacount}\"
				if [ -z "$lastviabuffer" ]; then
					echo >&2 "ERROR: Empty via directive."
					exit 1
				fi
			fi
			viacount="$(($viacount+1))"
			buffer="$word$viacount"
			eval $buffer=
			;;
		    rejse|st|st.|station)
			;;
		    *)
			case "$buffer" in
			    from|to|via[123])
				eval $buffer=\"\${$buffer:+\$$buffer+}$word\"
				;;
			    *)
				if [ -z "$from" ]; then
					from="$word"
				elif [ -z "$to" ]; then
					to="$word"
				else
					echo >&2 "ERROR: More than 2 cities mentioned or missing from/to/via directive."
					exit 1
				fi
			esac
			;;
		esac
	done
# FIXME: Handle more than a single via directive
	if [ -n "$from" ] && [ -n "$to" ] && [ $viacount -gt 0 ]; then
		url="http://www.rejseplanen.dk/bin/query.exe/mn?queryPageDisplayed=yes&S=$from+station&SBHF=2&Z=$to+station&ZBHF=2&V1=$via1+station&wai=yes&start=Find+rejse&start=Find+rejse"
	elif [ -n "$from" ] && [ -n "$to" ]; then
		url="http://www.rejseplanen.dk/bin/query.exe/mn?queryPageDisplayed=yes&S=$from+station&SBHF=2&Z=$to+station&ZBHF=2&wai=yes&start=Find+rejse&start=Find+rejse"
	else
		echo >&2 "ERROR: Too few cities, or missing from/to/via directive."
		exit 1
	fi
	wget -O- "$(urlencode "$url")" \
		| html2text -nobs -width 9999 \
		| grep '^Din afrejse' | sed -e 's/Din afrejse fra/Afr./' -e 's/Din ankomst til/Ank./' -e 's/ er \(den [0-9.]\+ \)\?kl. / kl. /' -e 's/ er i spor / spor /'
	;;
    benzin|benzinpris|diesel|dieselpris)
	# Set defaults based on app name
	case "$app" in
		diesel*) flavor="diesel";;
		fyr*|olie) flavor="olie";;
		*) flavor="95";;
	esac
	# Resolve options
	for word in "$@"; do
		case "$word" in
			92|95|98|diesel|olie|fyringsolie) flavor="$word";;
			[[:digit:]][[:digit:]][[:digit:]][[:digit:]][[:digit:]]) site="$word";;
			for|i|oktan|pris|amt) ;;
			*) county="$word";;
		esac
		shift
	done
	case "$flavor" in
		92) flavorid=1;;
		95) flavorid=2;;
		98) flavorid=3;;
		diesel) flavorid=6;;
		olie|fyringsolie) flavorid=7;;
	esac
	case "$county" in
		bornh*) countyid=1;;
		frederiksborg) countyid=3;;
		fyn) countyid=4;;
		nordj*) countyid=6;;
		ribe) countyid=7;;
		ringk*) countyid=8;;
		rosk*) countyid=9;;
		k?benh*|stork*) countyid=5;;
		storstr*) countyid=10;;
		s?nderj*) countyid=11;;
		vejle) countyid=12;;
		vestsj*) countyid=13;;
		viborg) countyid=14;;
		?rhus|aarhus) countyid=15;;
	esac
	# Lookup and compress results
	if [ -n "$site" ]; then
		wget -O- "http://www.fdmbenzinpriser.dk/wap/ShowStation.aspx?StationID=$site" | html2text -nobs -width 9999
	elif [ -n "$countyid" ]; then
		wget -O- "http://www.fdmbenzinpriser.dk/wap/AmtpriserPriceList.aspx?OctaneID=$flavorid&AmtID=$countyid" | html2text -nobs -width 9999
	else
		wget -O- "http://www.fdmbenzinpriser.dk/wap/Landspriser.aspx?ID=$flavorid" | html2text -nobs -width 9999
	fi
	;;
esac