forked from possatti/pokemonsay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpokemonsay.sh
executable file
·139 lines (129 loc) · 2.83 KB
/
pokemonsay.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#!/usr/bin/env bash
usage() {
echo
echo " Description: pokemonsay makes a Pokémon say something to you."
echo
echo " Usage: $(basename $0) [-p POKEMON_NAME] [-f COW_FILE] [-w COLUMN] [-l] [-n] [-t] [-h] [MESSAGE]"
echo
echo " Options:"
echo " -p, --pokemon POKEMON_NAME"
echo " Choose what Pokémon will be used by its name."
echo " -f, --file COW_FILE"
echo " Specify which .cow file should be used."
echo " -W, --word-wrap COLUMN"
echo " Specify roughly where messages should be wrapped."
echo " -l, --list"
echo " List all the Pokémon available."
echo " -N, --no-name"
echo " Do not tell the Pokémon name."
echo " -t, --think"
echo " Make the Pokémon think the message, instead of saying it."
echo " -h, --help"
echo " Display this usage message."
echo " MESSAGE"
echo " What the Pokemon will say. If you don't provide a message, a message will be read form standard input."
exit 0
}
# Where the Pokémon are located
pokemon_path="$(pwd)/cows"
list_pokemon() {
echo "Pokémon available in '$pokemon_path/':"
echo
all_pokemon="$(find $pokemon_path -name "*.cow" | sort)"
echo "$all_pokemon" | while read pokemon; do
pokemon=${pokemon##*/}
pokemon=${pokemon%.cow}
printf "%-8s\n" "${pokemon}"
done | column -x
exit 0
}
# While there are arguments, keep reading them
while [ $# -gt 0 ]
do
key="$1"
case $key in
-p|--pokemon)
POKEMON_NAME="$2"
shift; shift
;;
-p=*|--pokemon=*)
POKEMON_NAME="${1#*=}"
shift
;;
-f|--file)
COW_FILE="$2"
shift; shift
;;
-f=*|--file=*)
COW_FILE="${1#*=}"
shift
;;
-W|--word-wrap)
WORD_WRAP="$2"
shift; shift
;;
-W=*|--word-wrap=*)
WORD_WRAP="${1#*=}"
shift
;;
-l|--list)
list_pokemon
;;
-N|--no-name)
DISPLAY_NAME="NO"
shift
;;
-t|--think)
THINK="YES"
shift
;;
-h|--help)
usage
;;
-*)
echo
echo " Unknown option '$1'"
usage
;;
*)
# Append this word to the message
if [ -n "$MESSAGE" ]; then
MESSAGE="$MESSAGE $1"
else
MESSAGE="$1"
fi
shift
;;
esac
done
# Define where to wrap the message.
if [ -n "$WORD_WRAP" ]; then
word_wrap="-W $WORD_WRAP"
fi
# Support for macOS
if [ "$(uname)" == 'Darwin' ]; then
SHUF=gshuf
else
SHUF=shuf
fi
# Define which Pokémon should be displayed
if [ -n "$POKEMON_NAME" ]; then
pokemon_cow="$(find "$pokemon_path" -iname "$POKEMON_NAME*.cow")"
elif [ -n "$COW_FILE" ]; then
pokemon_cow="$COW_FILE"
else
pokemon_cow="$(find "$pokemon_path" -iname "*.cow" | $SHUF -n1)"
fi
# Get the Pokémon name
filename="$(basename "$pokemon_cow")"
pokemon_name="${filename%.*}"
# Call cowsay or cowthink
if [ -n "$THINK" ]; then
cowthink -f "$pokemon_cow" $word_wrap $MESSAGE
else
cowsay -f "$pokemon_cow" $word_wrap $MESSAGE
fi
# Write the pokemon name, unless requested otherwise
if [ -z "$DISPLAY_NAME" ]; then
echo "$pokemon_name"
fi