-
Notifications
You must be signed in to change notification settings - Fork 4
/
pokemonsay.sh
executable file
·189 lines (179 loc) · 4.44 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env bash
usage() {
echo
echo " Description: Pokemonsay makes a pokemon 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 pokemon will be used by its name."
echo " -d, --nationalDex NATIONAL_DEX_NUMBER"
echo " Choose what pokemon will be used by its national dex number."
echo " -D, --nationalDexForms NATIONAL_DEX_NUMBER"
echo " Choose what pokemon will be used by its national dex number (With a random form for the chosen pokemon)."
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 " -n, --no-wrap"
echo " Do not wrap the messages."
echo " -l, --list"
echo " List all the pokemon available."
echo " -L, --list-forms"
echo " List all the pokemon alternate forms available."
echo " -N, --no-name"
echo " Do not tell the pokemon name."
echo " -t, --think"
echo " Make the pokemon 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
}
INSTALL_PATH=${HOME}/.bin/pokemonsay
# Where the pokemon are.
POKEMON_PATH=${INSTALL_PATH}/pokemons
list_pokemon() {
echo "Pokemon available in '$POKEMON_PATH/':"
echo
ALL_POKEMON="$(find $POKEMON_PATH -regextype sed -regex "${POKEMON_PATH}/[0-9]\+\w*[^\-]\.cow" | sort)"
echo "$ALL_POKEMON" | while read -r POKEMON; do
POKEMON=${POKEMON##*/}
POKEMON=${POKEMON%.cow}
DEX=${POKEMON%_*}
POKEMON=${POKEMON##*_}
echo "$DEX - ${POKEMON^}"
done
exit 0
}
list_pokemon_forms() {
echo "Pokemon available in '$POKEMON_PATH/':"
echo
ALL_POKEMON="$(find $POKEMON_PATH -regextype sed -regex ".*[\-]\+.*\.cow" | sort)"
echo "$ALL_POKEMON" | while read -r POKEMON; do
POKEMON=${POKEMON##*/}
POKEMON=${POKEMON%.cow}
DEX=${POKEMON%_*}
POKEMON=${POKEMON##*_}
echo "$DEX - $POKEMON"
done
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
;;
-d|--nationalDex)
NATIONAL_DEX="$2"
shift; shift
;;
-d=*|--nationalDex=*)
NATIONAL_DEX="${1#*=}"
shift
;;
-D|--nationalDexForms)
NATIONAL_DEX_FORMS="$2"
shift; shift
;;
-D=*|--nationalDexForms=*)
NATIONAL_DEX_FORMS="${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
;;
-n|--no-wrap)
DISABLE_WRAP="YES"
shift
;;
-l|--list)
list_pokemon
;;
-L|--list-forms|--listForms)
list_pokemon_forms
;;
-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
# Disable wrapping if the option is set, otherwise
# define where to wrap the message.
word_wrap=()
if [ "${DISABLE_WRAP}" = "YES" ]; then
word_wrap=("-n")
elif [ -n "$WORD_WRAP" ]; then
word_wrap=("-W" "$WORD_WRAP")
fi
# Define which pokemon should be displayed.
if [ -n "$POKEMON_NAME" ]; then
POKEMON_COW=$(find $POKEMON_PATH -iname "*$POKEMON_NAME.cow" | shuf -n 1)
elif [ -n "$NATIONAL_DEX" ]; then
POKEMON_COW=$(find $POKEMON_PATH -regextype sed -regex ".*${NATIONAL_DEX}\_\w*[^\-]\.cow" | shuf -n 1)
elif [ -n "$NATIONAL_DEX_FORMS" ]; then
POKEMON_COW=$(find $POKEMON_PATH -name "${NATIONAL_DEX_FORMS}_*.cow" | shuf -n 1)
elif [ -n "$COW_FILE" ]; then
POKEMON_COW="$COW_FILE"
else
POKEMON_COW=$(find $POKEMON_PATH -name "*.cow" | shuf -n 1)
fi
# Get the pokemon name.
FILENAME=$(basename "$POKEMON_COW")
POKEMON_NAME="${FILENAME%.*}"
POKEMON_NAME="${POKEMON_NAME##*_}"
# 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