#!/usr/bin/env bash version=0.5 SYSTEMPONIES=/usr/share/ponies HOMEPONIES="${HOME}/.ponies" pony="*" wrap= if [[ -f `which cowsay` ]]; then true else echo "You don't seem to have the cowsay program." echo "Please install it in order to use this wrapper." echo -n "(Or symlink it to 'cowsay' in anywhere in \$path " echo "if it actually exists under a different filename." exit; fi cmd=cowsay [[ ${0} == *ponythink ]] && cmd=cowthink version() { echo "ponysay v$version" } usage() { version echo -e "\nUsage:" echo "${0##*/} [options] [message]" echo echo "If [message] is not provided, reads the message from STDIN" 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" } say() { # Ponies use UTF-8 drawing characters. Prevent a Perl warning. export PERL_UNICODE=S exec "$cmd" -f "$pony" "${wrap:+-W$wrap}" } 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 shift $((OPTIND - 1)) if [[ ! -f $pony ]]; then # Pony not a file? Search for it 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 if [[ -n "$*" ]]; then # Handle a message given via arguments say <<<"$*" else say fi