]> git.armaanb.net Git - bin.git/blobdiff - fortune
xsel: new script
[bin.git] / fortune
diff --git a/fortune b/fortune
index 5030086a99a0b895d088fe9afa4b923e0fedf232..afb967c5833679af35705ec5e21b64a63731ff39 100755 (executable)
--- a/fortune
+++ b/fortune
@@ -1,60 +1,95 @@
 #!/usr/bin/sh -e
-# Fortune implementation in awk
+# Fortune implementation in POSIX sh/awk
 
-usage()
-{
-  echo "Usage: fortune [OPTIONS]
+usage() {
+               echo 'Usage: fortune [OPTIONS]
 a simple POSIX sh/awk implementation of fortune
 
-  -h, --help                 show this help message
-  -l, --list                 list cookie files installed to /usr/share/fortune/
-  -f \$FILE, --file \$FILE     specify full path to cookie file
-  -c \$FILE, --cookie \$FILE   specify path to cookie file relative
-                             to /usr/share/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'
 }
 
-FILE="$(find /usr/share/fortune/ -type f -not -name '*.dat')"
+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
-      ;;
-    -f | --file)
-      FILE=$2
-      ;;
-    -c | --cookie)
-      FILE=/usr/share/fortune/$2
-      ;;
-    -l | --list)
-      ls -1 /usr/share/fortune
-      exit
-      ;;
-    *)
-      echo "ERROR: unknown parameter \"$2\""
-      usage
-      exit 1
-      ;;
-  esac
-  shift 2
+               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
 
-awk 'BEGIN {
-  f++
+[ "$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++
 }
 
 $1 == "%" {
-  f++
+       len = length(fortunes[f])
+       if ( len>maxlen || len<minlen ) {
+               fortunes[f] = ""
+       } else {
+               f++
+       }
 }
 
 {
-  sub("%", "")
-  fortunes[f] = fortunes[f] (length(fortunes[f])?"\n":"") $0
+       sub("%", "")
+       fortunes[f] = fortunes[f] (length(fortunes[f])?"\n":"") $0
 }
 
 END {
-  "date +%N" | getline time
-  srand(time)
-  print fortunes[int(rand() * f-2) + 2]
+       "od -A n -t d -N 3 /dev/urandom" | getline seed
+       srand(seed)
+       print fortunes[int(rand() * (f - 2)) + 1]
 }' $FILE