#! /bin/bash
FQDN=zcrayfish.dyndns.org
#Get the HTTP command, filename, protocol version . . .
read -r CMD OBJ PROTO BADCLIENT

filename="$(echo -ne "$OBJ" | sed -e 's/%2c/,/I' -e 's/%20/ /I')"

# get HTTP extra headers
while read -r KEY VALUE FOO
do
	test -z "$KEY"	&& break;
	[ "$KEY" == $'\r' ] && break;
done

date="$(date -u +'%a, %d %b %Y %R:%S GMT')"

error400 () {
        echo -e "HTTP/1.0 400 Bad Request\r"
        echo -e "Content-Type: text/plain charset=us-ascii\r"
        echo -e "Allow: GET\r"
        echo -e "Date: $date\r"
        echo -e "Server: gopher to http gateway at $FQDN"
	echo -e "Connection: close"
        echo -e "\r\n"
        echo -e "HTTP/1.0 400 Bad Request\r"
        echo -e "Request could not be understood - bad syntax\r"
        echo -e "\r"
	echo -e "gopher to http gateway at $FQDN"
        exit
}

error404 () {
       echo -e "HTTP/1.0 404 Not Found\r"
       echo -e "Content-Type: text/plain charset=us-ascii\r"
       echo -e "Date: $date\r"
       echo -e "Server: gopher to http gateway at $FQDN"
       echo -e "Connection: close"
       echo -e "\r\n"
       echo -e "HTTP/1.0 404 Not Found\r"
       echo -e "'$OBJ' doesn't exist!\r"
       echo -e "This resource cannot be located.\r"
       echo -e "\r"
       echo -e "gopher to http gateway at $FQDN"
}

error501 () {
        echo -e "HTTP/1.0 501 Not Implemented\r"
        echo -e "Content-Type: text/plain charset=us-ascii\r"
        echo -e "Allow: GET\r"
        echo -e "Date: $date\r"
        echo -e "Server: gopher to http gateway at $FQDN"
	echo -e "Connection: close"
        echo -e "\r\n"
        echo -e "HTTP/1.0 501 Not Implemented\r"
        echo -e "This error has accoured for one of the following reasons:\r"
        echo -e " *This server only implements the GET method (e.g. your browser sent a HEAD or PUT)\r"
        echo -e " *This server, while acting as a gateway or proxy, does not support CSO queries.\r"
        echo -e "\r"
	echo -e "gopher to http gateway at $FQDN"
        exit
}

# See if request is a GET, if not, throw it away
test "$CMD" != 'GET' && error501
# Likewise ignore invalid gets too.
test ! -z "$BADCLIENT" && error400 && exit

#Step4
case "$filename" in
    /favicon.ico)
	error404
        ;;
    /sitemap*|/robots.txt)
	echo -e "HTTP/1.0 200 OK\r"
	echo -e "Date: $date\r"
	echo -e "Server: gopher to http gateway at $FQDN\r"
        echo -e "Connection: close\r"
	echo -e "\r"
	/usr/bin/lynx -anonymous -dump -source gopher://$FQDN:70/0/$filename
	;;
    /2*)
	error501
	;;
    /7*httpsearch*)
            echo -e "HTTP/1.0 200 OK\r"
            echo -e "Content-Type: text/html; level=1\r"
            echo -e "Date: $date\r"
            echo -e "Server: gopher to http gateway at $FQDN"
            echo -e "Connection: close\r"
            echo -e "\r"
            echo -e '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0 Strict Level 1//EN">'
        searchytwothousand="$(echo $filename | sed 's/httpsearch=//g')"
        /usr/bin/lynx -anonymous -dump -source gopher://$FQDN:70$searchytwothousand | sed 's/<A HREF="\/\/$FQDN:70/<A HREF="http:\/\/$FQDN:80/g' | sed 's/<A HREF="\/\//<A HREF="gopher:\/\//g'
        exit
        ;;

    /7*)
          echo -e "HTTP/1.0 200 OK\r"
          echo -e "Content-Type: text/html; charset=us-ascii\r"
          echo -e "Date: $date\r"
          echo -e "Server: gopher to http gateway at $FQDN"
          echo -e "\r"
          echo -e '<!DOCTYPE html PUBLIC "-//IETF//DTD HTML 2.0//EN">'
          echo -e '<title></title>'
          echo -e "<form action='$FQDN$filename' method='get'>"
          echo -e '<p>This is a searchable gopher index. Enter search keywords:<input type="text" name="httpsearch"></p></form>'
	;;

    /debug*)
	echo -e "HTTP/1.0 200 OK\r"
	echo -e "Content-Type: text/plain\r"
	echo -e "\r"
	set
	;;
###START OF DUMB / NON-INTELLIGENT GOPHER TYPES###
    /0*|/4*|/5*|/6*|/9*|/g*|/s*|/h*|/I*|/p*|/d*)
        echo -e "HTTP/1.0 200 OK\r"
        case "$filename" in
	    /0*)
	        echo -e "Content-Type: text/plain\r"
		;;
            /4*)
                echo -e "Content-Type: application/mac-binhex40\r"
                ;;
            /5*)
                echo -e "Content-Type: application/octet-stream\r"
                ;;
            /6*)
                echo -e "Content-Type: text/plain\r"
                ;;
            /9*)
                echo -e "Content-Type: application/octet-stream\r"
                ;;
            /g*)
                echo -e "Content-Type: image/gif\r"
                ;;
            /s*)
                echo -e "Content-Type: application/octet-stream\r"
                ;;
            /h*)
                echo -e "Content-Type: $CONTENT_TYPE\r"
                ;;
            /I*)
                echo -e "Content-Type: image/jpeg\r"
                ;;
            /p*)
                echo -e "Content-Type: image/png\r"
                ;;
            /d*)
                echo -e "Content-Type: application/pdf\r"
                ;;
        esac
        echo -e "Date: $date\r"
        echo -e "Server: gopher to http gateway at $FQDN\r"
        echo -e "Connection: close\r"
        echo -e "\r"
	/usr/bin/lynx -anonymous -dump -source gopher://$FQDN:70$filename 
        ;;
###END OF DUMB / NON-INTELLIGENT GOPHER TYPES###
    *)
	    echo -e "HTTP/1.0 200 OK\r"
	    echo -e "Content-Type: text/html; level=1\r"
	    echo -e "Date: $date\r"
	    echo -e "Server: gopher to http gateway at $FQDN"
	    echo -e "Connection: close\r"
	    echo -e "\r"
	    echo -e '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0 Strict Level 1//EN">'
	/usr/bin/lynx -anonymous -dump -source gopher://$FQDN:70$filename | \
          tidy -in -w 0 -cn --doctype omit --show-errors 0 --show-warnings 0 --tidy-mark 0 --quiet 1 | \
          sed -e 's/<a href="\/\/'$FQDN':70/<A HREF="http:\/\/'$FQDN'/g' -e 's/<a href="\/\//<A HREF="gopher:\/\//g'

	exit
	;;
esac
exit