#!/bin/sh -efu # This posix shell script makes it easier to manage your shore house using NPS # Updated on 20-05-2025 NPS_HOST="${NPS_HOST:-nightfall.city}" NPS_PORT="${NPS_PORT:-1915}" NPS_NEX_PORT="${NPS_NEX_PORT:-1900}" NPS_SECRET="${NPS_SECRET:-"${XDG_CONFIG_HOME:-~/.config}/nps.secret"}" die() { printf "%s\n" "$1" exit 1 } print_help() { die "Simple script to interact with your Nightfall City's Postal Service (NPS) Usage: nf [] Commands: u|up|upload [] mkdir rm edit cat [] water Environment: NPS_HOST (default: nightfall.city) NPS_PORT (default: 1915) NPS_SECRET (default: \$XDG_CONFIG_HOME/nps.secret) NPS_USERNAME (overwrites the username from the secret file) NPS_TOKEN (overwrites the citizen id from the secret file)" } load_username() { NPS_USERNAME="${NPS_USERNAME:-"$(sed '1q;d' "$NPS_SECRET")"}" if [ -z "$NPS_USERNAME" ] then die "Username expected at line 1 of id" fi } load_token() { NPS_TOKEN="${NPS_TOKEN:-"$(sed '2q;d' "$NPS_SECRET")"}" if [ -z "$NPS_TOKEN" ] then die "Token expected at line 2 of id" fi } upload() { if [ "$#" -ne 2 ] && [ "$#" -ne 1 ] then die "Usage: upload []" fi load_username load_token content="$(cat "${2:--}")" printf "shore/%s/%s\n%s\n%s\n.\n" \ "${NPS_USERNAME}" \ "${1}" \ "${NPS_TOKEN}" \ "${content}" \ | nc "$NPS_HOST" "$NPS_PORT" } edit() { if [ "$#" -ne 1 ] then die "Usage: edit " fi load_username load_token tmpfile="$(mktemp -u --suffix .nps."$(basename "${1}")")" #shellcheck disable=SC2064 trap "rm -f ${tmpfile}; trap - EXIT; exit" EXIT INT HUP printf "shore/%s/%s/\n.\n" \ "${NPS_USERNAME}" \ "${1}" \ | nc "$NPS_HOST" "$NPS_NEX_PORT" > "$tmpfile" content="$(cat "$tmpfile")" old="$(stat -c%Y "$tmpfile")" ${EDITOR} "$tmpfile" new="$(stat -c%Y "$tmpfile")" content="$(cat "$tmpfile")" if [ "$old" = "$new" ] then die "File unchanged" else printf "shore/%s/%s\n%s\n%s\n.\n" \ "${NPS_USERNAME}" \ "${1}" \ "${NPS_TOKEN}" \ "${content}" \ | nc "$NPS_HOST" "$NPS_PORT" fi } delete() { if [ "$#" -ne 1 ] then die "Usage: delete " fi load_username load_token printf "shore/%s/%s\n%s\nDELETE\n.\n" \ "${NPS_USERNAME}" \ "${1}" \ "${NPS_TOKEN}" \ | nc "$NPS_HOST" "$NPS_PORT" } mkdir() { if [ "$#" -ne 1 ] then die "Usage: mkdir " fi load_username load_token printf "shore/%s/%s\n%s\nCREATE_DIR\n.\n" \ "${NPS_USERNAME}" \ "${1}" \ "${NPS_TOKEN}" \ | nc "$NPS_HOST" "$NPS_PORT" } water() { if [ "$#" -ne 0 ] then die "Usage: water" fi load_token printf "shore/garden/water/\n%s\n.\n" \ "${NPS_TOKEN}" \ | nc "$NPS_HOST" "$NPS_PORT" } cat_() { if [ "$#" -eq 0 ] then load_username path=/ elif [ "$#" -eq 1 ] then load_username path="${1}" elif [ "$#" -eq 2 ] then NPS_USERNAME="${1}" path="${2}" else die "Usage: cat [] []" fi printf "shore/%s/%s/\n.\n" \ "${NPS_USERNAME}" \ "${path}" \ | nc "$NPS_HOST" "$NPS_NEX_PORT" } main() { if [ "$#" -le 0 ] then print_help fi cmd="$1" shift case "$cmd" in "u"|"up"|"upload") upload "$@" ;; "e"|"edit") edit "$@" ;; "c"|"cat") cat_ "$@" ;; "mkdir") mkdir "$@" ;; "rm") delete "$@" ;; "water") water "$@" ;; # "h"|"help") print_help ;; *) print_help ;; esac } main "$@"