#!/bin/sh
# Version 2
#
# Use suckless fold command instead of GNU coreutils.
# https://dl.suckless.org/sbase/sbase-0.1.tar.gz
#
# `fold` from GNU coreutils corrupts UTF-8.
# <https://debbugs.gnu.org/cgi/bugreport.cgi?bug=44975>
# <https://unix.stackexchange.com/questions/454044/
#  unicode-safe-alternative-for-fold/486428#486428>

page="$1"
if [ -z "$page" ]
then
   echo "Usage: gopherpoodia-fetch.sh [Wikipedia page title or URL]"
   echo ""
   echo "Converts Wikipedia page to plaintext document."
   echo ""
   exit 1
fi

base="https://en.wikipedia.org/wiki/"
title=${page##"$base"}
title=${title##/}
enctitle=$(echo "$title" | sed -e 's|/|%2F|g')
link="$base$enctitle"

agent="User-Agent: Gopherpoodia ([email protected])"
api="https://en.wikipedia.org/api/rest_v1"
url="$api/page/html/$enctitle"

outdir="public_gopher/wiki/"
safetitle=$(echo "$enctitle" |\
   sed -e 's|%2F|-|g' -e 's|\.\.|_|g' -e 's|:|_|g')
outfile="$outdir/$safetitle"

mkdir -p "$outdir"
curl -H "User-Agent: $agent" "$url" |\
   webdump -l -r -w 70 -b "$base"  |\
   sed -e 's|/\./|/|g'             |\
   fold.suckless -s -w 72 >"$outfile"

echo "" >>"$outfile"
echo "From: <$link>" >>"$outfile"