#!/bin/bash

# Usage:
#   ./gemini.sh HOST PATH
#   ./gemini.sh HOST:PORT PATH
# Examples:
#   ./gemini.sh gemini.circumlunar.space /
#   ./gemini.sh gemini.circumlunar.space /docs/
#   ./gemini.sh gemini.circumlunar.space:1965 /
#   ./gemini.sh gemini.ctrl-c.club /

PAGER='bat --language markdown --style plain'

function trim() {
    echo -ne ${1} | xargs
}

function gemini() {
    readarray -d ':' -t HOST_PORT <<< ${1}
    HOST_=$(trim ${HOST_PORT[0]})
    PORT_=$(trim ${HOST_PORT[1]:-1965})
    PATH_=$(trim ${2})

    URL_="gemini://${HOST_}${PATH_}"
    echo -ne "${URL_}\r\n" | ncat --ssl ${HOST_} ${PORT_} | ${PAGER}

}

HOST_PORT_=${1}
PATH_=${2}
gemini ${HOST_PORT_} ${PATH_}

