#!/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 [[ "$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 fi # output the RFC-compliant Gophermap line printf '%s%s\t%s\t%s\t%s\r\n' "$etype" "$bname" "$esel" "$THOST" "$TPORT" done