bopher-ng

A better Gopher client in pure Bash
git clone git://git.luxferre.top/bopher-ng.git
Log | Files | Refs | README | LICENSE

list2map.sh (1600B)


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