]> git.armaanb.net Git - bin.git/commitdiff
fortune: Add argument parsing
authorArmaan Bhojwani <me@armaanb.net>
Mon, 1 Mar 2021 04:40:17 +0000 (23:40 -0500)
committerArmaan Bhojwani <me@armaanb.net>
Mon, 1 Mar 2021 04:40:17 +0000 (23:40 -0500)
fortune

diff --git a/fortune b/fortune
index 3f4e961f2b980de37683431b54dad3057b1f6873..4868b1a65004cdbb69dda658951866b1cf333731 100755 (executable)
--- a/fortune
+++ b/fortune
@@ -1,6 +1,35 @@
 #!/usr/bin/sh -e
 # Fortune implementation in awk
 
+usage()
+{
+    echo "fortune -- a simple awk/POSIX sh implementation of fortune"
+    echo ""
+    echo "\t-h --help     show this help message"
+    echo "\t--file=\$FILE  specify path to cookie file"
+}
+
+FILE="$(find /usr/share/fortune/ -type f -not -name '*.dat')"
+while [ "$1" != "" ]; do
+  PARAM=`echo $1 | awk -F= '{print $1}'`
+  VALUE=`echo $1 | awk -F= '{print $2}'`
+  case $PARAM in
+      -h | --help)
+          usage
+          exit
+          ;;
+      -f | --file)
+          FILE=$VALUE
+          ;;
+      *)
+          echo "ERROR: unknown parameter \"$PARAM\""
+          usage
+          exit 1
+          ;;
+  esac
+  shift
+done
+
 awk '$1 == "%" {
   sub(/%/, "")
   ++f
@@ -14,4 +43,4 @@ END {
   "date +%N" | getline time
   srand(time)
   print fortunes[int(rand() * f) + 1]
-}' ${1:-"/usr/share/fortune/cookie"}
+}' $FILE