2012-07-21 15:43:21 +02:00
|
|
|
#!/usr/bin/env bash
|
2012-02-26 19:52:17 +01:00
|
|
|
|
2012-08-01 03:33:30 +02:00
|
|
|
|
|
|
|
# Get bash script directory's parent
|
|
|
|
INSTALLDIR="$(dirname $( cd -P "$( dirname "${BASH_SOURCE[0]}" )" && pwd ))"
|
|
|
|
|
|
|
|
# Subscripts
|
|
|
|
listcmd="$INSTALLDIR/lib/ponysay/list.pl"
|
|
|
|
linklistcmd="$INSTALLDIR/lib/ponysay/linklist.pl"
|
|
|
|
truncatercmd="$INSTALLDIR/lib/ponysay/truncater"
|
|
|
|
quotecmd="$INSTALLDIR/lib/ponysay/pq4ps"
|
2012-08-18 04:21:49 +02:00
|
|
|
qlistcmd="$INSTALLDIR/lib/ponysay/pq4ps-list.pl"
|
2012-08-01 03:33:30 +02:00
|
|
|
|
|
|
|
pony="*" # Selected pony
|
|
|
|
wrap="" # Message wrap column
|
|
|
|
ponies=() # Selected ponies
|
|
|
|
|
|
|
|
scrw=`(stty size <&2 || echo 0 0) | cut -d ' ' -f 2` # Screen width
|
|
|
|
scrh=`(stty size <&2 || echo 0 0) | cut -d ' ' -f 1` # Screen height
|
|
|
|
|
|
|
|
# KMS ponies extension
|
2012-07-21 18:16:18 +02:00
|
|
|
kmscmd=""
|
2012-08-16 18:27:20 +02:00
|
|
|
[ "$TERM" = "linux" ] && kmscmd=$(for c in $(echo $PATH":" | sed -e 's/:/\/ponysay2kmsponysay /g'); do if [ -f $c ]; then echo $c; break; fi done)
|
|
|
|
[ ! "$kmscmd" = "" ] && TERM="-linux-"
|
2012-07-21 18:16:18 +02:00
|
|
|
|
2012-05-07 16:07:39 +02:00
|
|
|
|
2012-08-01 03:33:30 +02:00
|
|
|
# Cowsay script
|
2012-08-16 18:27:20 +02:00
|
|
|
if [ ${0} == *ponythink ]; then
|
|
|
|
if [ "$PONYSAY_COWTHINK" = "" ]; then
|
2012-08-01 03:33:30 +02:00
|
|
|
cmd=cowthink
|
|
|
|
customcmd=0
|
|
|
|
else
|
|
|
|
cmd="$PONYSAY_COWTHINK"
|
|
|
|
customcmd=1
|
|
|
|
fi
|
2012-07-20 05:10:41 +02:00
|
|
|
else
|
2012-08-16 18:27:20 +02:00
|
|
|
if [ "$PONYSAY_COWSAY" = "" ]; then
|
2012-08-01 03:33:30 +02:00
|
|
|
cmd=cowsay
|
|
|
|
customcmd=0
|
|
|
|
else
|
|
|
|
cmd="$PONYSAY_COWSAY"
|
|
|
|
customcmd=1
|
|
|
|
fi
|
2012-07-20 05:10:41 +02:00
|
|
|
fi
|
2012-07-20 18:07:59 +02:00
|
|
|
|
2012-02-26 16:47:05 +01:00
|
|
|
|
2012-08-01 03:33:30 +02:00
|
|
|
|
|
|
|
# Pony quotes
|
|
|
|
ponyquotes() {
|
2012-08-16 18:27:20 +02:00
|
|
|
[ "$TERM" = "-linux-" ] && TERM="linux"
|
2012-08-18 01:56:08 +02:00
|
|
|
"$0" ${wrap:+-W$wrap} $("$quotecmd" $@)
|
2012-08-01 03:33:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
# Function for printing the ponies and the message
|
2012-02-26 20:30:25 +01:00
|
|
|
say() {
|
2012-07-20 18:07:59 +02:00
|
|
|
# Ponies use UTF-8 drawing characters. Prevent a Perl warning.
|
|
|
|
export PERL_UNICODE=S
|
2012-08-01 03:33:30 +02:00
|
|
|
|
|
|
|
# Clear screen in TTY
|
2012-08-18 01:56:08 +02:00
|
|
|
( [ "$TERM" = "linux" ] || [ "$TERM" = "-linux-" ] ) && echo -ne '\e[H\e[2J'
|
2012-08-01 03:33:30 +02:00
|
|
|
|
|
|
|
# Set PONYSAY_SHELL_LINES to default if not specified
|
2012-08-16 18:27:20 +02:00
|
|
|
[ "$PONYSAY_SHELL_LINES" = "" ] && PONYSAY_SHELL_LINES=2
|
2012-08-01 03:33:30 +02:00
|
|
|
|
|
|
|
# Height trunction, show top
|
2012-07-20 18:07:59 +02:00
|
|
|
function htrunchead {
|
2012-08-01 03:33:30 +02:00
|
|
|
head --lines=$(( $scrh - $PONYSAY_SHELL_LINES ))
|
2012-07-20 18:07:59 +02:00
|
|
|
}
|
2012-08-01 03:33:30 +02:00
|
|
|
|
|
|
|
# Height trunction, show bottom
|
2012-07-20 18:07:59 +02:00
|
|
|
function htrunctail {
|
2012-08-01 03:33:30 +02:00
|
|
|
tail --lines=$(( $scrh - $PONYSAY_SHELL_LINES ))
|
2012-07-20 05:10:41 +02:00
|
|
|
}
|
2012-08-01 03:33:30 +02:00
|
|
|
|
|
|
|
# Simplification of customisation of cowsay
|
2012-08-16 18:27:20 +02:00
|
|
|
if [ $customcmd = 0 ]; then
|
2012-07-20 18:07:59 +02:00
|
|
|
function cowcmd {
|
|
|
|
pcmd='#!/usr/bin/perl\nuse utf8;'
|
2012-08-16 18:27:20 +02:00
|
|
|
ccmd=$(for c in $(echo $PATH":" | sed -e 's/:/\/'"$cmd"' /g'); do if [ -f $c ]; then echo $c; break; fi done)
|
2012-07-20 18:07:59 +02:00
|
|
|
|
2012-08-16 18:27:20 +02:00
|
|
|
if [ ${0} == *ponythink ]; then
|
2012-07-24 16:16:59 +02:00
|
|
|
cat <(echo -e $pcmd) $ccmd > "/tmp/ponythink"
|
|
|
|
perl '/tmp/ponythink' "$@"
|
|
|
|
rm '/tmp/ponythink'
|
2012-07-20 18:07:59 +02:00
|
|
|
else
|
|
|
|
perl <(cat <(echo -e $pcmd) $ccmd) "$@"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
else
|
|
|
|
function cowcmd {
|
|
|
|
$cmd "$@"
|
|
|
|
}
|
|
|
|
fi
|
2012-08-01 03:33:30 +02:00
|
|
|
|
|
|
|
# KMS ponies support
|
2012-08-16 18:27:20 +02:00
|
|
|
if [ "$kmscmd" = "" ]; then
|
2012-07-21 18:16:18 +02:00
|
|
|
function runcmd {
|
|
|
|
cowcmd -f "$pony" "$@"
|
|
|
|
}
|
|
|
|
else
|
|
|
|
function runcmd {
|
|
|
|
cowcmd -f <($kmscmd "$pony") "$@"
|
|
|
|
}
|
|
|
|
fi
|
2012-08-01 03:33:30 +02:00
|
|
|
|
|
|
|
# Print the pony and the message
|
2012-07-20 18:07:59 +02:00
|
|
|
if [ "$TERM" = "linux" ] || [ "$PONYSAY_TRUNCATE_HEIGHT" = 'yes' ] || [ "$PONYSAY_TRUNCATE_HEIGHT" = 'y' ] || [ "$PONYSAY_TRUNCATE_HEIGHT" = '1' ]; then
|
|
|
|
if [ "$PONYSAY_BOTTOM" = 'yes' ] || [ "$PONYSAY_BOTTOM" = 'y' ] || [ "$PONYSAY_BOTTOM" = '1' ]; then
|
2012-07-21 18:16:18 +02:00
|
|
|
runcmd "${wrap:+-W$wrap}" | wtrunc | htrunctail
|
2012-07-20 18:07:59 +02:00
|
|
|
else
|
2012-07-21 18:16:18 +02:00
|
|
|
runcmd "${wrap:+-W$wrap}" | wtrunc | htrunchead
|
2012-07-20 18:07:59 +02:00
|
|
|
fi
|
2012-07-03 09:57:53 +02:00
|
|
|
else
|
2012-07-21 18:16:18 +02:00
|
|
|
runcmd "${wrap:+-W$wrap}" | wtrunc
|
2012-07-03 09:57:53 +02:00
|
|
|
fi
|
2012-02-26 20:30:25 +01:00
|
|
|
}
|
|
|
|
|
2012-07-18 16:47:57 +02:00
|
|
|
|
2012-08-01 03:33:30 +02:00
|
|
|
|
|
|
|
# If no stdin and no arguments then print usage and exit
|
2012-08-18 01:56:08 +02:00
|
|
|
if [ -t 0 ] && [ $# == 0 ]; then
|
2012-08-01 03:33:30 +02:00
|
|
|
usage
|
|
|
|
exit
|
2012-07-19 03:02:42 +02:00
|
|
|
fi
|
2012-07-20 18:07:59 +02:00
|
|
|
|
2012-08-01 03:33:30 +02:00
|
|
|
|
|
|
|
|
|
|
|
# Parse options
|
|
|
|
while getopts "f:W:Llhvq" OPT; do
|
2012-07-20 18:07:59 +02:00
|
|
|
case ${OPT} in
|
|
|
|
v) version; exit ;;
|
|
|
|
h) usage; exit ;;
|
2012-07-26 03:40:30 +02:00
|
|
|
f) ponies+=( $OPTARG ) ;;
|
2012-07-20 18:07:59 +02:00
|
|
|
l) list; exit ;;
|
|
|
|
L) linklist; exit ;;
|
|
|
|
W) wrap="$OPTARG" ;;
|
2012-08-18 01:56:08 +02:00
|
|
|
q) shift $((OPTIND - 1)); ponyquotes "$*"; exit ;;
|
2012-07-20 18:07:59 +02:00
|
|
|
\?) usage >&2; exit 1 ;;
|
|
|
|
esac
|
2012-02-26 16:47:05 +01:00
|
|
|
done
|
2012-02-26 20:30:25 +01:00
|
|
|
shift $((OPTIND - 1))
|
2012-02-26 19:52:17 +01:00
|
|
|
|
2012-07-19 03:02:42 +02:00
|
|
|
|
2012-08-01 03:33:30 +02:00
|
|
|
# Check for cowsay
|
2012-07-11 17:36:18 +02:00
|
|
|
hash $cmd &>/dev/null; if [ $? -ne 0 ]; then
|
2012-07-20 18:07:59 +02:00
|
|
|
cat >&2 <<EOF
|
2012-03-12 02:34:00 +01:00
|
|
|
You don't seem to have the $cmd program.
|
|
|
|
Please install it in order to use this wrapper.
|
|
|
|
|
|
|
|
Alternatively, symlink it to '$cmd' in anywhere in \$PATH
|
|
|
|
if it actually exists under a different filename.
|
|
|
|
EOF
|
2012-07-20 18:07:59 +02:00
|
|
|
exit 1
|
2012-03-12 02:34:00 +01:00
|
|
|
fi
|