#! /usr/bin/env bash # Editor for nightfall.city # samhunter@rawtext.club # Syntax: # ./nexedit # for example: # ./nexedit shore/samhunter/foo.bar # # Check based on oberon's suggestion in classifieds # Create a file with unique content then read it back check() { local -i rc=1 # only good if write successful local tag="${RANDOM}-${RANDOM}-${RANDOM}" local tagfile="shore/samhunter/tag" printf "%s\n%s\n%s\n.\n" \ "${tagfile}" \ "$(cat nightfall-city.id)" \ "$tag" | nc -w 10 nightfall.city 1915 ret="$(printf "%s\r\n" "${tagfile}" | nc nightfall.city 1900)" [[ "$tag" == "$ret" ]] && rc=0 return $rc } # Check whether nightfall.city is read-only or not check || { printf "Tagfile is read-only.\n" >&2 ; exit 1; } # Try to read the existing file readonly PAT='.*not found' if [[ -n "$1" ]]; then nexfile="$1" else exit 1 fi editfile=$(mktemp "/tmp/nexedit.XXXXXXX.nex") trap "rm -f ${editfile}" EXIT mapfile -t DATA < <(printf "%s\r\n" "${nexfile}" | nc nightfall.city 1900) # Open empty if nothing retrieved, otherwise load the current state [[ "${DATA[0]}" =~ $PAT ]] \ || printf "%s\n" "${DATA[@]}" > "${editfile}" ${EDITOR:-vim} "${editfile}" # Last chance save - in case N.C started rejecting writes while we # were editing check || cp "${editfile}" "${nexfile//\//_}.bak" # Upload printf "%s\n%s\n%s\n.\n" \ "${nexfile}" \ "$(cat nightfall-city.id)" \ "$(cat ${editfile})" | nc -w 10 nightfall.city 1915 # vim: ts=2 sw=2 expandtab ft=bash :