ponysay/ponysay
Jan Alexander Steffens (heftig) 2ddfe9d19b Add support for passing through -W
2012-02-26 20:13:34 +01:00

63 lines
1.3 KiB
Bash
Executable file

#!/usr/bin/env bash
version=0.4
SYSTEMPONIES=/usr/share/ponies
HOMEPONIES="${HOME}/.ponies"
pony=
wrap=
cmd=cowsay
[[ ${0} == *ponythink ]] && cmd=cowthink
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)"
echo " -W[column] The screen column where the message should be wrapped"
}
while getopts f:W:hv OPT
do
case ${OPT} in
v) version; exit ;;
h) usage; exit ;;
f) pony="$OPTARG" ;;
W) wrap="$OPTARG" ;;
\?) usage >&2; exit 1 ;;
esac
done
# 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
# Ponies use UTF-8 drawing characters. Prevent a Perl warning.
export PERL_UNICODE=S
exec "$cmd" -f "$pony" "${wrap:+-W$wrap}"