aen.sh (2084B)
1 #!/bin/sh 2 # aen: Virgil's Aeneid (Latin, macronized) 3 # License: Public domain 4 5 SELF="$0" 6 7 get_data() { 8 sed '1,/^#EOF$/d' < "$SELF" | tar xz -O "$1" 9 } 10 11 if [ -z "$PAGER" ]; then 12 if command -v less >/dev/null; then 13 PAGER="less" 14 else 15 PAGER="cat" 16 fi 17 fi 18 19 show_help() { 20 exec >&2 21 echo "usage: $(basename "$0") [flags] [reference...]" 22 echo 23 echo " -l list books" 24 echo " -W no line wrap" 25 echo " -h show help" 26 echo 27 echo " Reference types:" 28 echo " <Book>" 29 echo " Individual book" 30 echo " <Book>:<Chapter>" 31 echo " Individual chapter of a book" 32 echo " <Book>:<Chapter>:<Verse>[,<Verse>]..." 33 echo " Individual verse(s) of a specific chapter" 34 echo " <Book>:<Chapter>-<Chapter>" 35 echo " Range of chapters in a book" 36 echo " <Book>:<Chapter>:<Verse>-<Verse>" 37 echo " Range of verses in a book chapter" 38 echo " <Book>:<Chapter>:<Verse>-<Chapter>:<Verse>" 39 echo " Range of chapters and verses in a book" 40 echo 41 echo " /<Search>" 42 echo " All verses that match a pattern" 43 echo " <Book>/<Search>" 44 echo " All verses in a book that match a pattern" 45 echo " <Book>:<Chapter>/<Search>" 46 echo " All verses in a chapter that match a pattern" 47 exit 2 48 } 49 50 while [ $# -gt 0 ]; do 51 isFlag=0 52 firstChar="${1%"${1#?}"}" 53 if [ "$firstChar" = "-" ]; then 54 isFlag=1 55 fi 56 57 if [ "$1" = "--" ]; then 58 shift 59 break 60 elif [ "$1" = "-l" ]; then 61 get_data aen.tsv | awk -v cmd=list "$(get_data aen.awk)" 62 exit 63 elif [ "$1" = "-W" ]; then 64 export AEN_NOLINEWRAP=1 65 shift 66 elif [ "$1" = "-h" ] || [ "$isFlag" -eq 1 ]; then 67 show_help 68 else 69 break 70 fi 71 done 72 73 cols=$(tput cols 2>/dev/null) 74 if [ $? -eq 0 ]; then 75 export AEN_MAX_WIDTH="$cols" 76 fi 77 78 if [ $# -eq 0 ]; then 79 if [ ! -t 0 ]; then 80 show_help 81 fi 82 83 while true; do 84 printf "aen> " 85 if ! read -r ref; then 86 break 87 fi 88 get_data aen.tsv | awk -v cmd=ref -v ref="$ref" "$(get_data aen.awk)" | ${PAGER} 89 done 90 exit 0 91 fi 92 93 get_data aen.tsv | awk -v cmd=ref -v ref="$*" "$(get_data aen.awk)" | ${PAGER}