aboutsummaryrefslogtreecommitdiff
path: root/ponysay
diff options
context:
space:
mode:
authorJan Alexander Steffens (heftig) <jan.steffens@gmail.com>2012-02-26 19:52:17 +0100
committerJan Alexander Steffens (heftig) <jan.steffens@gmail.com>2012-02-26 20:05:47 +0100
commit729ac65c7f3174e6b4119a696c14330c9b488e3a (patch)
treeadf09f0974a51b683aaad511b7f132e1e8bcee85 /ponysay
parentc840fc697688437b8eb2fb0724d62d59f40b00ca (diff)
downloadponysay-729ac65c7f3174e6b4119a696c14330c9b488e3a.tar.gz
ponysay-729ac65c7f3174e6b4119a696c14330c9b488e3a.tar.bz2
ponysay-729ac65c7f3174e6b4119a696c14330c9b488e3a.zip
Search for ponies more intelligently
Diffstat (limited to 'ponysay')
-rwxr-xr-xponysay55
1 files changed, 31 insertions, 24 deletions
diff --git a/ponysay b/ponysay
index 27b5d40..5d6d2c7 100755
--- a/ponysay
+++ b/ponysay
@@ -1,37 +1,44 @@
#!/usr/bin/env bash
+
+# Ponies use UTF-8 drawing characters. Prevent a Perl warning.
export PERL_UNICODE=S
-[[ -z ${PONYDIR} ]] && PONYDIR=/usr/share/ponies
-HOMEDIR=${HOME}/.ponies
+SYSTEMPONIES=/usr/share/ponies
+HOMEPONIES="${HOME}/.ponies"
cmd=cowsay
[[ ${0} == *ponythink ]] && cmd=cowthink
-function ponyf() {
- if [[ -f ${HOMEDIR}/${1}.pony ]]; then
- ${cmd} -f "${HOMEDIR}/${1}.pony" $@
- elif [[ -f ${PONYDIR}/${1}.pony ]]; then
- ${cmd} -f "${PONYDIR}/${1}.pony" $@
- else
- ponyr $@
- fi
-}
-function ponyr() {
- if [[ -d ${HOMEDIR} ]] && [[ -n $(find ${HOMEDIR} -name \*.pony) ]]; then
- ${cmd} -f $(ls ${HOMEDIR}/*.pony | sort -R | head -n1) $@
- elif [[ -d ${PONYDIR} ]] && [[ -n $(find ${PONYDIR} -name \*.pony) ]]; then
- ${cmd} -f $(ls ${PONYDIR}/*.pony | sort -R | head -n1) $@
- fi
-}
+pony=
while getopts f:hv OPT
do
case ${OPT} in
- v) echo "ponysay v0.2" ; exit;;
- h) echo "-v for version, -h for this, -f to specify a ponyfile. It'll choose a random ponyfile if no argument is given." ; exit;;
- f) ponyf $* ; exit;;
- \?) echo "DERP"; exit;;
- *) ponyr $* ; exit;;
+ v) echo "ponysay v0.4" ; exit;;
+ h) echo "-v for version, -h for this, -f to specify a ponyfile. It'll choose a random ponyfile if no argument is given." ; exit;;
+ f) pony=$OPTARG ;;
+ \?) echo "DERP"; exit;;
esac
done
-ponyr $*
+
+# Pony not a file? Search for it
+if [[ ! -f $pony ]]; then
+
+ # Pony not set? Choose all
+ [[ -z $pony ]] && pony="*"
+
+ ponies=()
+
+ [[ -d $SYSTEMPONIES ]] && ponies+=( "$SYSTEMPONIES"/$pony.pony )
+ [[ -d $HOMEPONIES ]] && ponies+=( "$HOMEPONIES"/$pony.pony )
+
+ if (( ${#ponies} < 1 )); then
+ echo >&2 "All the ponies are missing! Call the Princess!"
+ exit 1
+ fi
+
+ # Choose a random pony
+ pony="${ponies[$RANDOM%${#ponies[@]}]}"
+fi
+
+exec "$cmd" -f "$pony"