#!/bin/bash
function gmirewrite() {
    sed 's#^=> \(gemini://[^ ]*\)$#=> \1 \1#;
         s#^=> \(gemini://[^ ]*/\) #=> \1index.gmi #;
         s#^=> gemini://#=> '"$HOME"'/.cache/offpunk/gemini/#g;'
}

# Assuming offpunk has been synced recently, rewrite the links in
# antenna's home page to point to the local cache, and then do the
# same for every such file in turn, converting them to HTML
netcache.py --offline gemini://warmedal.se/~antenna |\
    gmirewrite |\
    awk '/^=> / { print $2; }' |\
    while read -r path; do
        dest="$(echo "$path" | sed 's#.gmi$##; s#$#.html#;')"
        printf "Converting %s...\n" "$path"
        gmirewrite < $path | gmi2html.awk > $dest
    done

# Rewrite and convert antenna's home page itself, which will serve as
# a starting point for plucker
netcache.py --offline gemini://warmedal.se/~antenna |\
    gmirewrite |\
    sed 's#\(=> [^ ]*\).gmi #\1 #g; s#\(=> [^ ]*\) #\1.html #' |\
    gmi2html.awk > antenna.html

# Pluck it - result will end up in ~/.plucker/Antenna.pdb
plucker-build -H antenna.html -M 2 -f Antenna \
              --bpp=4 \
              --maxwidth=160 \
              --zlib-compression
