#!/bin/bash

# Usage:
#   ./spartan.sh HOST PATH
#   ./spartan.sh HOST:PORT PATH
# Examples:
#   ./spartan.sh mozz.us /

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

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

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

    REQUEST_="${HOST_} ${PATH_} 0"
    echo -ne "${REQUEST_}\r\n" | nc ${HOST_} ${PORT_} | ${PAGER}

}

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

