-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup.sh
executable file
·152 lines (126 loc) · 4.21 KB
/
setup.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
#!/usr/bin/env bash
mkdir -p ~/.local/bin
# Create the rickroller script in the ~/.local/bin directory
cat <<EOF >~/.local/bin/rick
#!/usr/bin/env bash
roll_count=0
path=\$(realpath "\$0")
# Reset lyrics index to 0 if roll_count is 0
if [ "\$1" = '-r' ]; then
sed -i "s/^roll_count=[0-9]\+\$/roll_count=0/" "\$path"
exit
fi
lyrics=("Never gonna give you up"
"Never gonna let you down"
"Never gonna run around and desert you"
"Never gonna make you cry"
"Never gonna say goodbye"
"Never gonna tell a lie and hurt you"
"Never gonna give you up"
"Never gonna let you down"
"Never gonna run around and desert you"
"Never gonna make you cry"
"Never gonna say goodbye"
"Never gonna tell a lie and hurt you"
"(Ooh, give you up)"
"(Ooh, give you up)"
"Never gonna give, never gonna give"
"(Give you up)"
"Never gonna give, never gonna give"
"(Give you up)"
"We've known each other for so long"
"Your heart's been aching, but"
"You're too shy to say it"
"Inside, we both know what's been going on"
"We know the game and we're gonna play it"
"I just wanna tell you how I'm feeling"
"Gotta make you understand"
"Never gonna give you up"
"Never gonna let you down"
"Never gonna run around and desert you"
"Never gonna make you cry"
"Never gonna say goodbye"
"Never gonna tell a lie and hurt you"
"Never gonna give you up"
"Never gonna let you down"
"Never gonna run around and desert you"
"Never gonna make you cry"
"Never gonna say goodbye"
"Never gonna tell a lie and hurt you"
"Never gonna give you up"
"Never gonna let you down"
"Never gonna run around and desert you"
"Never gonna make you cry"
"Never gonna say goodbye"
"Never gonna tell a lie and hurt you")
max="\${#lyrics[@]}"
if [ -t 1 ]; then
echo "\${lyrics[\$roll_count]}"
else
echo -n "\${lyrics[\$roll_count]}"
fi
if [[ -n "\${lyrics[\$((\$roll_count + 1))]}" ]]; then
sed -i "s/^roll_count=[0-9]\+\$/roll_count=\$((\$roll_count + 1))/" "\$path"
else
sed -i "s/^roll_count=[0-9]\+\$/roll_count=0/" "\$path"
fi
EOF
# # Determin the shell type (bash, zsh, etc) and thus the right RC file
# shell_rc_file=~/.$(basename "$SHELL")rc
shell_rc_file=~/.bashrc
echo -e "\n\n# Inserted by rickroll-lyrics-PS1. Don't update" >>$shell_rc_file
if [[ ! "$PATH" =~ "$HOME/.local/bin" ]]; then
export PATH="$HOME/.local/bin:$PATH"
echo 'export PATH="$HOME/.local/bin:$PATH"' >>$shell_rc_file
fi
cat <<EOF >~/.local/bin/roll
#!/usr/bin/env bash
argmnt() {
if [[ \$1 =~ ^-\$2 ]]; then
return 0
else
return 1
fi
}
if argmnt "\$1" r; then
# Reset PS1 content from ~/.ps1.bk
if [ -f ~/.ps1.bk ]; then
export PS1="\$(cat ~/.ps1.bk)"
fi
# Reset PROMPT_COMMAND from ~/.pcmd.bk
if [ -f ~/.pcmd.bk ]; then
export PROMPT_COMMAND="\$(cat ~/.pcmd.bk)"
fi
# Remove rick script plus PROMPT_COMMAND and PS1 backups
rm -f ~/.pcmd.bk ~/.ps1.bk
# Remove code added by the script in the shell_rc_file
sed -i '/^# Inserted by rickroll-lyrics-PS1/,/^# End of rickroll-lyrics-PS1/d' "$shell_rc_file"
echo "Done. You might need to open a new terminal session"
else
# Backup current PS1
if [ ! -f ~/.ps1.bk ]; then
echo "\$PS1" > ~/.ps1.bk
fi
# Backup current PROMPT_COMMAND
if [ -n "\$PROMPT_COMMAND" ]; then
echo "\$PROMPT_COMMAND" > ~/.pcmd.bk
fi
# Reset lyrics index whenever 'roll' is called
~/.local/bin/rick -r
export PROMPT_COMMAND='PS1="\[\033[1;32m\]\$(~/.local/bin/rick)\[\033[0m\] \[\033[1;34m\]\w\[\033[0m\]$ "'
fi
EOF
chmod +x ~/.local/bin/rick
chmod +x ~/.local/bin/roll
echo "# Do not remove or comment the line below, instead run 'source roll -r' to reset to default" >>$shell_rc_file
echo "source roll" >>$shell_rc_file
echo "# End of rickroll-lyrics-PS1" >>$shell_rc_file
# if [[ ! "$PATH" =~ "$HOME/.local/bin" ]]; then
# export PATH="$HOME/.local/bin:$PATH"
# fi
# Output sucess message if script is not sourced. Results will be visible if sourced
if [[ "$(basename -- "$0")" == "setup.sh" ]]; then
echo -e "\e[32mYou can now open a new terminal session and type 'roll'\e[0m"
exit
fi
source roll