#!/bin/sh
DEST_URI="https://secure.bluecherry.net/twig/"

http_0_9=0

while read -r RAW_DATA; do
	DATA=`echo "$RAW_DATA" | tr -d "\r"`
	echo "$RAW_DATA" >> /tmp/redir.req
	tok1=`echo "$DATA" | awk '{print $1}'`
	tok3=`echo "$DATA" | awk '{print $3}'`
	if [ "$tok1" = "GET" -a -z "$tok3" ]; then
		http_0_9=1
		break
	fi
	if [ -z "$tok1" ]; then
		break
	fi
done

function http_echo() {
	echo -n "$*"
	echo -e "\r"
}

function header() {
	http_echo "HTTP/1.0 301 Moved permanently."
	http_echo "Date: `TZ=GMT date +"%a, %d %b %Y %H:%M:%S %Z"`"
	http_echo "Server: Simple redirector"
	http_echo "Connection: close"
	http_echo "Content-Type: text/html"
	http_echo "Location: $DEST_URI"
	http_echo ""
}

function body() {
	cat << _E_O_F_
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML>
<HEAD>
 <TITLE>Moved permanently</TITLE>
 <META HTTP-EQUIV="Refresh" CONTENT="0; url=$DEST_URI">
</HEAD>
<BODY>
<H1>Moved permanently</H1>
Please go to <A HREF="$DEST_URI">$DEST_URI</A> instead.
</BODY>
</HTML>
_E_O_F_
}

if [ $http_0_9 = 0 ]; then
	header
fi
body

exit 0
