ponysay/ponysay

61 lines
1.2 KiB
Text
Raw Normal View History

2012-02-26 17:43:26 +01:00
#!/usr/bin/env bash
2012-02-26 19:52:17 +01:00
2012-02-26 20:01:22 +01:00
version=0.4
2012-02-26 19:52:17 +01:00
SYSTEMPONIES=/usr/share/ponies
HOMEPONIES="${HOME}/.ponies"
2012-02-26 20:01:22 +01:00
pony=
2012-02-26 18:28:03 +01:00
cmd=cowsay
[[ ${0} == *ponythink ]] && cmd=cowthink
2012-02-26 16:47:05 +01:00
2012-02-26 20:01:22 +01:00
version() {
echo "ponysay v$version"
}
usage() {
version
echo
echo "Usage:"
echo "${0##*/} [options]"
echo
echo "Options:"
echo " -v Show version and exit"
echo " -h Show this help and exit"
echo " -f[name] Select a pony (Either a filename or a pony name)"
}
2012-02-26 16:47:05 +01:00
while getopts f:hv OPT
do
case ${OPT} in
2012-02-26 20:01:22 +01:00
v) version; exit ;;
h) usage; exit ;;
2012-02-26 19:52:17 +01:00
f) pony=$OPTARG ;;
2012-02-26 20:07:36 +01:00
\?) usage >&2; exit 1 ;;
2012-02-26 16:47:05 +01:00
esac
done
2012-02-26 19:52:17 +01:00
# 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
2012-02-26 20:01:22 +01:00
# Ponies use UTF-8 drawing characters. Prevent a Perl warning.
export PERL_UNICODE=S
2012-02-26 19:52:17 +01:00
exec "$cmd" -f "$pony"