-#!/bin/bash
-
-# Note: bash is required, sh will fail to output anything to the client
-# Probably related to interactions beteween & and output redirections
-#
-# It seems the timeout on netcat does not work when listening...
-# So you will have to either kill the process or make a last connection
-# (which will not be processed)
-
-# $0 [prog] ([port])
-# You can use the evironment variable $QUERY_STRING for the query
-
-[ "$ADDR" = "" ] && ADDR=127.0.0.1
-[ "$LOG" = "" ] && LOG=/tmp/gopher.service.err
-[ "$PIDF" = "" ] && PIDF="`mktemp`"
-[ "$TIMEOUT" = "" ] && TIMEOUT=10
-[ "$MAXCON" = "" ] && MAXCON=1024
-
-prog="$1"
-port="$2"
-defn="$3"
-[ "$port" = "" ] && port=70
-
-if [ "$prog" = "" ]; then
- rm "$PIDF"
- echo "Syntax: $0 [prog] ([port]) (default non-7)" >&2
- echo ' You can use the evironment variable $QUERY_STRING for the query' >&2
- echo ' The default port is 70' >&2
- echo ' A default message for non-service (7) request can be given' >&2
- echo ' A filename will be displayed, you can delete it to stop the service' >&2
- echo ' (you still have to make a last connection on $port)' >&2
+#!/bin/sh
+
+# $0 [gopher uri] ([mode])
+# uri: the gopher URI to dig to, e.g.:
+# - gopher://sdf.org:70/1/faq
+# - sdf.org/faq
+# - sdf.org/1/faq
+# - http://gopher.nikiroo.be:80/news
+# - ...
+# default port is 70 for gopher, 80 for http, 443 for https
+# default filetype depends upon selector:
+# - 0: plain text
+# - 1: menu (dir-like)
+# - ...
+# - download: fake mode to download the result without changes
+
+# Manual:
+# When asked for a number, you can also enter 'n' or 'p' to get the
+# next or previous item (+1 or -1)
+
+# ENV variables:
+# LINK_COLOR: escape sequences colour (def: 2)
+# - : means no escape sequence
+# 1 : means colour 1
+# 2 : means colour 2
+# [...]
+# INVERT : invert the output for image viewing (for white backgrounds)
+# 0 : do not invert (default)
+# 1 : invert
+
+# EXIT Codes:
+# 0: ok
+# 1: syntax error
+# 2: cannot contact server
+# 3: unknown selector mode
+# 255: special exit more 'q'
+
+URI="$1"
+
+PREFIX="[0-9hIg+]"
+
+PROTOCOL="`echo $URI | sed 's|^\([^:]*://\)\?\([^:/]*\)\(:[^/]*\)\?/\?\(.*\)\?|\1|g'`"
+SERVER="`echo $URI | sed 's|^\([^:]*://\)\?\([^:/]*\)\(:[^/]*\)\?/\?\(.*\)\?|\2|g'`"
+PORT="`echo $URI | sed 's|^\([^:]*://\)\?\([^:/]*\)\(:[^/]*\)\?/\?\(.*\)\?|\3|g'`"
+SELECTOR="`echo $URI | sed 's|^\([^:]*://\)\?\([^:/]*\)\(:[^/]*\)\?/\?\(.*\)\?|\4|g'`"
+MODE=
+
+PORT="`echo "$PORT" | sed 's/^://'`"
+
+# Defaults:
+if [ "$PORT" = "" ];then
+ case "$PROTOCOL" in
+ http://) PORT=80 ;;
+ https://) PORT=443;;
+ *) PORT=70 ;;
+ esac
+fi
+
+if [ "$MODE" = "" ]; then
+ # "" or dir-like selector? -> 1 ; if not -> 0
+ echo "$SELECTOR" | grep "/$" >/dev/null && MODE=1 || MODE=0
+ [ "$SELECTOR" = "" ] && MODE=1
+
+ # check explicit modes:
+ if echo "$SELECTOR" | grep "^/\?\($PREFIX\|download\)/" >/dev/null; then
+ MODE="`echo "$SELECTOR" | sed 's:^/\?\([^/]*\)/\(.*\):\1:'`"
+ SELECTOR="`echo "$SELECTOR" | sed 's:^/\?\([^/]*\)/\(.*\):\2:'`"
+ fi
+fi
+
+if [ "$SERVER" = "" ]; then
+ echo "Syntax error: $0 [gopher uri]" >&2