commit bdfe168ec85a774c585d6153ef98f6bd789a1b3c Author: KatolaZ Date: Wed Jan 23 22:46:02 2019 +0000 initial commit diff --git a/gophed b/gophed new file mode 100755 index 0000000..1febbe7 --- /dev/null +++ b/gophed @@ -0,0 +1,68 @@ +#!/bin/sh + + +cleanup () { + rm -f $TMPFILE; +} + +go () { + printf "${1}\r\n" | nc $2 $3 +} + +usage() { + printf "usage: \n" + printf " visit a resource:\n" + printf " $0 \n" + printf " init links:\n" + printf " $0 -i \n" + printf " show this help:\n" + printf " $0 -h\n" + printf "\n" + printf "In a gopher page, just type:\n" + printf " !./visit %% LINE\n" + printf "to visit the resource at LINE, or:\n" + printf " !./dl %% LINE\n" + printf "to download the resource at LINE\n\n" + exit 0 +} + +trap cleanup INT QUIT HUP KILL ABRT TERM + +if [ "$1" = "-h" ]; then usage; fi + +if [ "$1" = "-i" ]; then + ln -s $0 visit + ln -s $0 dl + exit 0 +fi + +script_name=$(basename $0) + +if [ "${script_name}" = "gophed" ]; then + sel=$(echo $1 | sed -r 's:/:+:g') + TMPFILE=$(mktemp /tmp/gophed_$2_${sel}_$3.XXXXXXXXXXXXXXXXXXX) + go "$1" "$2" "$3" > $TMPFILE + cat show - show | ed $TMPFILE +fi +if [ "${script_name}" = "visit" ]; then + LINE=$(awk "{if (NR == $2) print \$0;}" $1) + RESOURCE=$(echo "$LINE" | cut -d ' ' -f 2) + HOST=$(echo "$LINE" | cut -d ' ' -f 3) + PORT=$(echo "$LINE" | awk -F ' ' '{ match($4,/[[:digit:]]+/); print substr($4,RSTART,RLENGTH) }') + SEL=$(echo $RESOURCE | sed -r 's:/:+:g') + TMPFILE=$(mktemp /tmp/gophed_${HOST}_${SEL}_${PORT}.XXXXXXXXXXXXXXXXXXX) + go "$RESOURCE" "$HOST" "$PORT" > $TMPFILE + cat show - show | ed $TMPFILE +fi +if [ "${script_name}" = "dl" ]; then + LINE=$(awk "{if (NR == $2) print \$0;}" $1) + RESOURCE=$(echo "$LINE" | cut -d ' ' -f 2) + HOST=$(echo "$LINE" | cut -d ' ' -f 3) + PORT=$(echo "$LINE" | awk -F ' ' '{ match($4,/[[:digit:]]+/); print substr($4,RSTART,RLENGTH) }') + DIR_RESOURCE=$(dirname $RESOURCE) + echo "Download $RESOURCE in ${HOST}${RESOURCE}" + mkdir -p "$HOST/$DIR_RESOURCE" + go "$RESOURCE" "$HOST" "$PORT" > "${HOST}${RESOURCE}" +fi + +cleanup