Wholesale rewrite of tests once I realized solution could be more portable
authorThomas Leavitt <thomas@thomasleavitt.org>
Sun, 2 Dec 2018 11:11:47 +0000 (03:11 -0800)
committerThomas Leavitt <thomas@thomasleavitt.org>
Sun, 2 Dec 2018 11:11:47 +0000 (03:11 -0800)
.bash_logout
.bash_profile

index 7db7200..dde9689 100644 (file)
@@ -1,5 +1,4 @@
-export COWPATH="/usr/local/git/cowfiles:/usr/local/git/cowsay-files/cows:/usr/local/share/localcows:/usr/local/share/cows"
-
+# ~/.bsah_lgout
 function lolcatcowsayfortune {
 
     # randomly pick between cowsay and cowthink
@@ -11,15 +10,18 @@ function lolcatcowsayfortune {
     eye_type=${eye_types[ $RANDOM % 8 ]}
 
     NUMOFCOWS=`cowsay -l | grep -v "Cow files in" | tail -n +2 | wc -w`
-    WHICHCOW=$((RANDOM%$NUMOFCOWS+1))
+    WHICHCOW= $((RANDOM%$NUMOFCOWS+1))
     THISCOW=`cowsay -l | tail -n +2 | sed -e 's/\ /\'$'\n/g' | sed $WHICHCOW'q;d'`
 
     #echo "Selected cow: ${THISCOW}, from ${WHICHCOW}"
     fortune -s | $cowsay_type $eye_type -f $THISCOW -n | lolcat
 }
 
-# fortune, with cowsay and random cows, and lolcat; here instead of .bashrc 
+# require all components to be installed for function to execute
+# fortune, with cowsay and random cows, and lolcat; here instead of .bashrc
 # to prevent remote eommands from failing due to output being generated
-if [ -e /usr/local/share/cows ] &&  [ -x /usr/local/bin/cowsay ] && [ -x /usr/local/bin/fortune ] && [ -x /usr/local/bin/lolcat ]; then
+if [ -n "$COWPATH" ] && [ -x "$(command -v cowsay)" ] && [ -x "$(command -v fortune)" ] && [ -x "$(command -v lolcat)" ]; then
     lolcatcowsayfortune
+else
+    echo "Missing required components, please check to ensure that fortune, cowsay, and lolcat are all installed, and that COWPATH is not null."
 fi
index de83538..a73577a 100644 (file)
@@ -1,6 +1,25 @@
 [[ -s ~/.bashrc ]] && source ~/.bashrc
 
-export COWPATH="/usr/local/git/cowfiles:/usr/local/git/cowsay-files/cows:/usr/local/share/localcows:/usr/local/share/cows"
+# dynamically build COWPATH based on commonly used locations for cowfiles
+if [ -e /usr/local/git/cowfiles ]; then
+   COWPATH="/usr/local/git/cowfiles:$COWPATH"
+fi
+if [ -e /usr/local/git/cowsay-files/cows ]; then
+    COWPATH="/usr/local/git/cowsay-files/cows:$COWPATH"
+fi
+if [ -e /usr/local/share/localcows ]; then
+    COWPATH="/usr/local/share/localcows:$COWPATH"
+fi
+if [ -e /usr/local/share/cows ]; then
+    COWPATH="/usr/local/share/cows:$COWPATH"
+fi
+if [ -e /usr/share/cowsay ]; then
+    COWPATH="/usr/share/cowsay:$COWPATH"
+fi
+
+if [ -n "$COWPATH" ]; then
+    export COWPATH
+fi
 
 function lolcatcowsayfortune {
 
@@ -13,15 +32,18 @@ function lolcatcowsayfortune {
     eye_type=${eye_types[ $RANDOM % 8 ]}
 
     NUMOFCOWS=`cowsay -l | grep -v "Cow files in" | tail -n +2 | wc -w`
-    WHICHCOW=$((RANDOM%$NUMOFCOWS+1))
+    WHICHCOW= $((RANDOM%$NUMOFCOWS+1))
     THISCOW=`cowsay -l | tail -n +2 | sed -e 's/\ /\'$'\n/g' | sed $WHICHCOW'q;d'`
 
     #echo "Selected cow: ${THISCOW}, from ${WHICHCOW}"
     fortune -s | $cowsay_type $eye_type -f $THISCOW -n | lolcat
 }
 
-# fortune, with cowsay and random cows, and lolcat; here instead of .bashrc 
+# require all components to be installed for function to execute
+# fortune, with cowsay and random cows, and lolcat; here instead of .bashrc
 # to prevent remote eommands from failing due to output being generated
-if [ -e /usr/local/share/cows ] &&  [ -x /usr/local/bin/cowsay ] && [ -x /usr/local/bin/fortune ] && [ -x /usr/local/bin/lolcat ]; then
+if [ -n "$COWPATH" ] && [ -x "$(command -v cowsay)" ] && [ -x "$(command -v fortune)" ] && [ -x "$(command -v lolcat)" ]; then
     lolcatcowsayfortune
+else
+    echo "Missing required components, please check to ensure that fortune, cowsay, and lolcat are all installed, and that COWPATH is not null."
 fi