X-Git-Url: https://git.armaanb.net/?a=blobdiff_plain;f=fortune;h=fde90857a6d61e6d17080364c1c9a470158f888a;hb=55a218e51cd587265c04508f816b8b2fff56840e;hp=02276e6ecf76e4b612583e6a397668312494d1c2;hpb=c2e630de3cbe3dff05fcf93bfe01c05b4ef71b92;p=bin.git diff --git a/fortune b/fortune index 02276e6..fde9085 100755 --- a/fortune +++ b/fortune @@ -1,22 +1,95 @@ -#!/usr/bin/awk -f -# Fortune implementation in awk +#!/usr/bin/sh -e +# Fortune implementation in POSIX sh/awk -BEGIN { - "date +%N" | getline time - srand(time) - "grep '%' " ARGV[1] " | wc -l" | getline n - line = int(rand() * n) +usage() { + echo 'Usage: fortune [OPTIONS] +a simple POSIX sh/awk implementation of fortune + + -h, --help show this help message + -l, --list list installed cookie files + -c FILE, --cookie FILE specify cookie file + -n LENGTH, --min LENGTH specify min fortune length + -N LENGTH, --max LENGTH specify max fortune length + + fortune looks for fortunes in $FORTUNEDIR, which defaults to + /usr/share/fortune. + +Exit codes: + 0 success + 1 generic error + 2 argument parsing error' +} + +parse_err() { + echo "$1" + $2 + exit 2 +} + +DIR=${FORTUNEDIR:-"/usr/share/fortune/"} +FILE="$(find $DIR -type f -not -name '*.dat')" +MAXLENGTH=1000000000000 +MINLENGTH=0 + +while [ "$1" != "" ]; do + case $1 in + -h | --help) + usage + exit + ;; + -c | --cookie) + FILE=$DIR/$2 + ;; + -n | --min) + MINLENGTH=$2 + ;; + -N | --max) + MAXLENGTH=$2 + ;; + -l | --list) + ls -1 $DIR + exit + ;; + *) + parse_err "ERROR: unknown option \"$1\"" usage + ;; + esac + shift 2 +done + +[ "$MAXLENGTH" -lt "$MINLENGTH" ] && { + parse_err "ERROR: maximum length is less than minimum length" +} + +[ -d "$FILE" ] && { + parse_err "ERROR: directory given, please provide a file or glob of files" +} + +[ "$(find $FILE -maxdepth 1 -print -quit 2> /dev/null)" ] || { + parse_err "ERROR: no such file or directory" +} + +awk -v maxlen=$MAXLENGTH -v minlen=$MINLENGTH \ +'BEGIN { + f++ } -/%/ { - if (p) { - exit +$1 == "%" { + len = length(fortunes[f]) + if ( len>maxlen || len