#!/bin/ksh

# www->gopher
# Make your SDF gopherspace visible inside your SDF webspace

# Copyright (c) 2010 Claudio Calvelli <intercal at sdf>;
# see below for information about copying, redistributing, etc.

# Please include the word GOPHER in the subject if sending email, so
# it doesn't get deleted as spam by mistake

# To install: copy into your webspace (make sure you make it executable
# and the name ends in ".cgi"); access the corresponding URL and make
# sure it works; link to it. For example, if installed as "gopher.cgi"
# at the top of your webspace, it will be accessible as

#     http://(your username).freeshell.org/gopher.cgi

# the script handles subdirectories in your gopher space automatically;
# however they must contain a "gophermap" file (it would be easy to modify
# this so it does not need a "gophermap", however I don't need it so
# I haven't - subject to the conditions below, feel free to make the
# necessary changes).

# Permission is hereby granted to use, misuse, modify, distribute, break,
# fix again, etcetera, this software, provided that the following
conditions
# are met:

# 1. Redistributions of this software must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. As the software is a shel script, it is normally not possible to
#    distribute it in binary (non human readable) form; however, if
#    such binary distribution is possible, it must reproduce the above
#    copyright notice, this list of conditions and the following
disclaimer
#    in the documentation and/or other materials provided with the
#    distribution.
# 3. Neither the name of the Author nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.

# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY
WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.


if [ "$HTTP_HOST" = '' ]
then
  echo 3
  echo "3 You appear to be accessing this via gopher. This is a"
  echo "3 www to gopher gateway, and you do not need it."
  exit 0
fi

path="$PATH_INFO"
case "$path" in
  */.*|.*)
      echo "Status: 403 Forbidden"
      echo "Content-Type: text/plain"
      echo
      echo "Sorry, you do not have permission to access that"
      exit 0
      ;;
esac

user="`whoami`"
full_path="/ftp/pub/users/$user/$path"
if [ -x "$full_path" ]
then
  case "$path" in
      *.cgi)
          exec "$full_path"
          ;;
  esac
fi

if [ -f "$full_path" ]
then
  echo "Status: 200 OK"
  echo "Content-Type: text/plain"
  echo
  cat "$full_path"
  exit 0
fi

if ! [ -d "$full_path" ]
then
  echo "Status: 404 Not found"
  echo "Content-Type: text/plain"
  echo
  echo "Sorry, the path you requested was not found"
  exit 0
fi

case "$path" in
  */) ;;
  *)
      echo 'Status: 301 Moved permanently'
      echo 'Content-Type: text/html; charset=iso-8859-1'
      echo "Location: http://$HTTP_HOST$REQUEST_URI/"
      echo ''
      echo '<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">'
      echo '<html><head>'
      echo '<title>301 Moved Permanently</title>'
      echo '</head><body>'
      echo '<h1>Moved Permanently</h1>'
      echo '<p>The document has moved'
      echo '<a href="http://'"$HTTP_HOST$REQUEST_URI/"'">here</a>.</p>'
      echo '</body></html>'
      exit 0
      ;;
esac

echo "Status: 200 OK"
echo "Content-Type: text/html"
echo
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"'
echo '    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
echo '<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">'
echo '<head>'
echo '<meta http-equiv="Content-Type" content="text/html; charset=utf-8"
/>'
echo "<title>Index of $path</title>"
echo '</head>'
echo '<body>'
echo '<p>'

export IFS='    '
dq='"'
while read name file host port
do
  if [ "$host" != '' ]
  then
      [ "$port" = '' ] || port=":$port"
      type="`echo "$name" | dd bs=1 count=1 2>/dev/null`"
      echo "<a href=${dq}gopher://$host$port/$type$file$dq>${name#?}</a>
(gopher link, may not work in your browser)<br />"
  else
      if [ "$file" = '' ]
      then
          [ "$name" = '' ] && name='&nbsp;'
          echo "$name<br />"
      else
          case "$name" in
              1*) echo "<a href=$dq$base$file$dq>${name#1}/</a><br />" ;;
              *) echo "<a href=$dq$base$file$dq>${name#?}</a><br />" ;;
          esac
      fi
  fi
done < "$full_path/gophermap"
echo '</p>'
echo '</body>'
echo '</html>'