Files
bopher-ng/tools/list2map.sh
T

40 lines
1.6 KiB
Bash
Raw Normal View History

2023-04-03 19:14:01 +03:00
#!/bin/bash
# A simple tool for Gophermap fragment generation from existing file/directory lists
# Depends on file external command to determine the type
#
# Usage: find [root] [params] | list2map.sh
#
# Created by Luxferre in 2023, released into public domain
shopt -s extglob # enable extended pattern matching (just to be sure)
RDIR="$1" # root directory, no substitution if empty
THOST="$2" # can be empty per spec
TPORT="$3" # can be empty too
INDEXFILE='index.map' # the basename to consider an index file
typequery() { # external command wrapper to fetch the file's MIME type
local res="$(file -Nn --mime-type "$1")"
printf '%s' "${res##*: }"
}
while read -rs fpath; do # fully line-based operation
ftype="$(typequery "$fpath")" # MIME type here
bname="${fpath##*/}" # basename here
2023-04-04 07:28:39 +03:00
desc="$bname" # default to the basename
2023-04-03 19:14:01 +03:00
[[ "$bname" == "$INDEXFILE" ]] && continue # skip index map
etype=9 # default map entry type is binary until proven otherwise
esel="${fpath##$RDIR}" # if RDIR is empty, they are the same
if [[ "$ftype" == 'inode/directory' ]]; then # it's a directory
etype=1
esel="${esel}/${INDEXFILE}" # update the selector
elif [[ "${ftype%%/*}" == 'text' ]]; then # update etype for text files
etype=0
2023-04-04 07:28:39 +03:00
read -rsd$'\r' desc < "$fpath" # read the first file line as the description/title
desc="${desc##*([[:blank:]])}" # remove leading whitespace
desc="${desc%%*([[:blank:]])}" # remove trailing whitespace
2023-04-03 19:14:01 +03:00
fi
# output the RFC-compliant Gophermap line
2023-04-04 07:28:39 +03:00
printf '%s%s\t%s\t%s\t%s\r\n' "$etype" "$desc" "$esel" "$THOST" "$TPORT"
2023-04-03 19:14:01 +03:00
done