-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstart.sh
executable file
·294 lines (262 loc) · 11.9 KB
/
start.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
# ______ _ _____ _ _
# | ___ \ | | | __ \ | | | | ____ __
# | |_/ / _ _ | |__ ___ | | \/ ___ | | __| | / __ )____ ______/ /_
# | / | | | || _ \ / _ \| | __ / _ \ | | / _ | / __ / __ // ___/ __ \
# | |\ \ | |_| || |_) || __/| |_\ \| (_) || || (_| | / /_/ / /_/ (__ ) / / /
# \_| \_| \____||____/ \___| \____/ \___/ |_| \____//_____/\____/____/_/ /_/
# https://github.com/thibaudcolas/rubegoldbash
# By: Thibaud Colas, License: CC0
# Colors, Solarized theme from https://github.com/necolas/dotfiles
if [[ $COLORTERM = gnome-* && $TERM = xterm ]] && infocmp gnome-256color >/dev/null 2>&1; then
export TERM='gnome-256color';
elif infocmp xterm-256color >/dev/null 2>&1; then
export TERM='xterm-256color';
fi;
if tput setaf 1 &> /dev/null; then
tput sgr0; # reset colors
bold=$(tput bold);
reset=$(tput sgr0);
# Solarized colors, taken from http://git.io/solarized-colors.
black=$(tput setaf 0);
blue=$(tput setaf 33);
cyan=$(tput setaf 37);
green=$(tput setaf 64);
orange=$(tput setaf 166);
purple=$(tput setaf 125);
red=$(tput setaf 124);
violet=$(tput setaf 61);
white=$(tput setaf 15);
yellow=$(tput setaf 136);
else
bold='';
reset='';
black='';
blue='';
cyan='';
green='';
orange='';
purple='';
red='';
violet='';
white='';
yellow='';
fi;
rubehappy="${orange}(◕‿◕)${yellow}";
rubemeh="${orange}(◕_◕)${yellow}";
rubeeager="${orange}(っ◕‿◕)っ${yellow}";
rubeimpatient="${orange}(づ◕‿◕。)づ${yellow}";
rubethinking="${orange}(◕‿◕。)${yellow}";
rubeyay="${orange}(ノ◕ヮ◕)ノ${yellow}";
rubepoker="${orange}༼ つ ◕_◕ ༽つ${yellow}";
rubewink="${orange}◕‿↼${yellow}";
# Font: Modified Doom + Smushed Slant
echo "${bold}${yellow}______________________________________________________________________________${reset}"
echo "${bold}${cyan}______ _ _____ _ _ ${green} ${reset}"
echo "${bold}${cyan}| ___ \ | | | __ \ | | | | ${green} ____ __ ${reset}"
echo "${bold}${cyan}| |_/ / _ _ | |__ ___ | | \/ ___ | | __| | ${green} / __ )____ ______/ /_ ${reset}"
echo "${bold}${cyan}| / | | | || _ \ / _ \| | __ / _ \ | | / _ | ${green} / __ / __ // ___/ __ \ ${reset}"
echo "${bold}${cyan}| |\ \ | |_| || |_) || __/| |_\ \| (_) || || (_| | ${green} / /_/ / /_/ (__ ) / / / ${reset}"
echo "${bold}${cyan}\_| \_| \____||____/ \___| \____/ \___/ |_| \____/ ${green}/_____/\____/____/_/ /_/ ${reset}"
echo "${bold}${yellow}______________________________________________________________________________${reset}"
echo "${bold}${cyan}....is now installed! ${rubehappy} Cool, let's get started!${reset}"
echo ""
echo "${bold}${rubepoker} I'm ${orange}Rube${yellow}, the command-line guru. Nice to meet you, ${orange}$(whoami).${reset}"
echo "${bold}${rubeeager} Can you do some Rube Goldberg bash for me? A lot of pipes (${white}|${yellow}), please!${reset}"
echo ""
# Calculates the player score!
export BESTSCORE=$((0))
export FIRST=$((0))
export STAGE=$((0))
function score_game() {
local combo='|'
if [ -n "$BASH_VERSION" ]; then
history -a
history -c
history -r
fi
local last_command=$(cat $HISTFILE | tail -n 1)
local multiplier=$(grep -o "$combo" <<< $last_command | wc -l)
multiplier=$((multiplier))
local command_length=$(echo $last_command | wc -m)
local command_score=$(( multiplier * command_length ))
export BESTSCORE=$(($BESTSCORE > $command_score ? $BESTSCORE: $command_score))
if [[ "$command_score" -eq 0 ]]; then
local rubeface=$rubemeh;
else
local rubeface=$rubehappy;
fi;
if [[ "$FIRST" -eq 0 ]]; then
export FIRST=$((1))
echo "${bold}${rubewink} If you need help with commands, try this: ${white}http://explainshell.com/${reset}"
echo "${bold}${rubethinking} You can share your score with the world by using the ${white}rubeshare${yellow} command."
else
echo "${rubeface} Command Score:${white} $command_score${yellow}, Best: ${white}$BESTSCORE${reset}"
fi;
echo ""
if (( $BESTSCORE > 500 )); then
export STAGE=5
elif (( $BESTSCORE > 200 )); then
export STAGE=4
elif (( $BESTSCORE > 100 )); then
export STAGE=3
elif (( $BESTSCORE > 50 )); then
export STAGE=2
elif (( $BESTSCORE > 20 )); then
export STAGE=1
fi;
case "$STAGE" in
0 )
echo "${bold}${rubethinking} Let's start with something simple, some ${white}ls -la ~${yellow} for example.${reset}"
echo "${bold}${rubeyay} And then we can try some basic piping! ${white}ls -la ~ | grep bash${yellow}.${reset}"
;;
1 )
echo "${bold}${rubeyay} You did it! ... but that was easy, I told you what to do!${reset}"
echo "${bold}${rubethinking} You can share your score with the world by using the ${white}rubeshare${yellow} command."
echo "${bold}${rubehappy} Ok, let's go a little bit further. Pipe the previous command into ${white}wc -l${yellow}.${reset}"
;;
2 )
echo "${bold}${rubeeager} That was fast, we need to go deeper! ${red}Web Scraping${yellow} is an ancient dark art.${reset}"
echo "${bold}${rubeimpatient} Behold the power of ${white}curl${yellow}! ${white}curl -s whenwillitbedone.trgdy.com${yellow}.${reset}"
echo "${bold}${rubeeager} You can use ${white}grep${yellow} and ${white}sed${yellow} to only keep the good stuff: ${white}grep '<p>'${yellow}, ${white}sed 's/<p>//g'${yellow}.${reset}"
;;
3 )
echo "${bold}${rubepoker} ${red}Powerful you have become, the dark side I sense in you.${reset}"
echo "${bold}${rubemeh} Use your powers for good, not for evil! But here's some nice cool black magic:${reset}"
echo "${bold}${rubeyay} ${white}lynx -dump twitter.com/globalgamejam | head -n150 | tail -n30 | grep -v '(BUTT'${reset}"
;;
4 )
echo "${bold}${rubepoker} ${red}If you end your training now — if you choose the quick and easy path as Vader did — you will become an agent of evil.${reset}"
echo "${bold}${rubeyay} ${white}xargs -n 1${yellow} is a powerful weapon, not to use lightly — that said, try it with ${white}curl${yellow}.${reset}"
echo "${bold}${rubemeh} Always pass on what you have learned. - by using the ${white}rubeshare${yellow} command.${reset}"
;;
5 )
echo "${bold}${rubethinking} I have nothing more to teach you, my young apprentice. Let's ${white}rubeshare${yellow}.${reset}"
echo "${bold}${rubewink} You proved yourself worthy, here's something you might enjoy:${reset}"
echo "${bold}${rubewink} ${white}telnet towel.blinkenlights.nl ${yellow}(to exit, hit ${red}^]${white} and type ${red}quit${white})${reset}"
;;
esac;
}
function rubesay() {
if hash say 2>/dev/null; then
local voice=$([ $[ $RANDOM % 2 ] == 0 ] && echo "Victoria" || echo "Alex")
say -v $voice "$@"
fi
}
function write_history() {
if [ -n "$ZSH_VERSION" ]; then
fc -AI
elif [ -n "$BASH_VERSION" ]; then
history -w
fi
}
# Configure history to work the way we want to.
export HISTFILE=~/.rubegoldbash_history
write_history
rm -f ~/.rubegoldbash_history
touch ~/.rubegoldbash_history
export HISTIGNORE=''
export HISTTIMEFORMAT='%T $ '
export HISTSIZE=10000
export SAVEHIST=10000
export HISTCONTROL=ignoreboth:erasedups
# Always enable colored `grep` output
export GREP_OPTIONS="--color=auto";
if [ -n "$ZSH_VERSION" ]; then
precmd() { score_game; }
elif [ -n "$BASH_VERSION" ]; then
# When the shell exits, append to the history file instead of overwriting it
shopt -s histappend
# Case-insensitive globbing (used in pathname expansion)
shopt -s nocaseglob;
# Autocorrect typos in path names when using `cd`
shopt -s cdspell;
# After each command, append to the history file and reread it
export PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND$'\n'}score_game"
fi
# Nice prompt from https://github.com/necolas/dotfiles.
# Shell prompt based on the Solarized Dark theme.
# Screenshot: http://i.imgur.com/EkEtphC.png
# Heavily inspired by @necolas’s prompt: https://github.com/necolas/dotfiles
# iTerm → Profiles → Text → use 13pt Monaco with 1.1 vertical spacing.
# Highlight the user name when logged in as root.
if [[ "${USER}" == "root" ]]; then
userStyle="${red}";
else
userStyle="${orange}";
fi;
if [ -n "$ZSH_VERSION" ]; then
# Set the terminal title to the current working directory.
PS1="${userStyle}%n"; # username
PS1+="${white} playing ";
PS1+="${cyan}RubeGold${green}Bash"; # host
PS1+="${white} in ";
PS1+="${green}%d"; # working directory
PS1+="${white} — ";
PS1+="${rubethinking}";
PS1+="${yellow} What do we do now?";
PS1+="
";
PS1+="${white}\$ ${reset}"; # `$` (and reset color)
PS2="${yellow}→ ${reset}";
else
# Set the terminal title to the current working directory.
PS1="\[\033]0;\w\007\]";
PS1+="\[${bold}\]\n"; # newline
PS1+="\[${userStyle}\]\u"; # username
PS1+="\[${white}\] playing ";
PS1+="\[${cyan}RubeGold\]\[${green}Bash\]"; # host
PS1+="\[${white}\] in ";
PS1+="\[${green}\]\w"; # working directory
PS1+="\[${white}\] — ";
PS1+="\[${rubethinking}\]";
PS1+="\[${yellow}\] What do we do now?";
PS1+="\n";
PS1+="\[${white}\]\$ \[${reset}\]"; # `$` (and reset color)
PS2="\[${yellow}\]→ \[${reset}\]";
fi
export PS1;
export PS2;
# Share your results online!
function rubeshare() {
write_history
local description="$(whoami)'s RubeGoldBash game — $(date)"
if [[ "$BESTSCORE" -eq 0 ]]; then
local rubeface=$rubemeh;
local message="Try again.";
else
local rubeface=$rubehappy;
local message="Yeah!";
fi;
echo ""
echo "${rubeface} $message You scored ${red}$BESTSCORE points!"
rubesay "$message You scored $BESTSCORE points!"
echo "${white}...Uploading ${bold}${userStyle}$(whoami)${white}'s ${cyan}RubeGold${green}Bash ${white}game — ${orange}$(date)${reset}"
#TODO Find another way to serialize JSON
# local player_history=$(cat $HISTFILE | sed '/^#/ d' | sed ':a;N;$!ba;s/\n/ /g')
#TODO Destub
local player_history=$(cat $HISTFILE | tail -n 1 | sed '/^#/ d' | sed ':a;N;$!ba;s/\n/ /g')
player_history="${player_history//\"/\\\"}"
player_history="${player_history//\n/\\n}"
local gist_upload='{"public": true,"description": "'"$description"'","files": {"history.sh": {"content": "'"$player_history"'"}}}'
local gist_response=$(curl --silent -X POST -d "$gist_upload" https://api.github.com/gists)
# local gist_response='{"html_url": "https://gist.github.com/banana","test":true}'
local gist_url=$(echo $gist_response | grep -o "https://gist.github.com/[a-z0-9]*" | head -n 1)
local gist_hash=$(echo $gist_url | cut -d '/' -f 4)
echo ""
echo "${bold}${yellow}" '____________________________________________________________' "${reset}"
echo "${bold}${red}" ' _ _ _ _ ___ ' "${reset}"
echo "${bold}${red}" '| || | (_) __ _ | |_ / __| __ ___ _ _ ___ ___' "${reset}"
echo "${bold}${red}" '| __ | | | / _` | | ` \ \__ \ / _| / _ \ | `_| / -_) (_-<' "${reset}"
echo "${bold}${red}" '|_||_| |_| \__, | |_||_| |___/ \__| \___/ |_| \___| /__/' "${reset}"
echo "${bold}${red}" ' |___/ ' "${reset}"
echo "${bold}${yellow}" '____________________________________________________________' "${reset}"
echo ""
curl --silent -X POST --data "player=$(whoami)&score=$BESTSCORE&gist=$gist_hash" http://highscore.rubegoldbash.com/scores.txt | column -s, -t
# curl --silent http://highscore.rubegoldbash.com/scores.txt | column -s, -t
echo "${rubeimpatient}${reset}"
echo ""
echo "${bold}${white}View the full list online at ${red}http://www.rubegoldbash.com/${reset}"
echo "${bold}${white}View your saved game at ${bold}${red}$gist_url${reset}"
echo "${bold}${rubewink} Wanna try something else? ${white}telnet towel.blinkenlights.nl ${yellow}(to exit, hit ${red}^]${white} and type ${red}quit${white})${reset}"
echo ""
}