-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup.sh
More file actions
executable file
·169 lines (139 loc) · 5.19 KB
/
setup.sh
File metadata and controls
executable file
·169 lines (139 loc) · 5.19 KB
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
#!/usr/bin/env bash
# Install zsh and/or brew, configured for St Andrews CS lab PCs / host servers.
# Usage: see -h / usage()
#######################################
# Adds the specified string to the shell-rc files if it doesn't already exist.
# This adds the specified string to both .zshrc and .bashrc if they exist.
# Globals:
# HOME
# Arguments:
# $1: the string to be added to .bashrc.
#######################################
add_to_rc () {
for shell_file in "$HOME/.bashrc" "$HOME/.zshrc"; do
if [ -f "$shell_file" ]; then
append_if_not_present "$1" "$shell_file"
fi
done
}
#######################################
# Adds the given string to the given file if it doesn't already exist.
# Arguments:
# $1: the string to be added to the file.
# $2: the path to the file.
#######################################
append_if_not_present () {
grep -qxF "$1" "$2" || echo "$1" >> "$2"
}
#######################################
# Installs brew under the local user from git.
# Globals:
# HOME
# Arguments:
# None
#######################################
stasetup::install_brew () {
originaldir=$(pwd)
if [ ! -d "$HOME/.linuxbrew" ]; then
git clone https://github.com/Homebrew/brew "$HOME/.linuxbrew"
fi
$HOME/.linuxbrew/bin/brew update --force --quiet
eval "$($HOME/.linuxbrew/bin/brew shellenv)"
brew install binutils gcc
cd "$HOMEBREW_PREFIX/bin"
add_to_rc 'eval "$($HOME/.linuxbrew/bin/brew shellenv)"'
cd "$originaldir"
}
#######################################
# Install zsh using brew, and configure.
# Globals:
# HOME
# HOMEBREW_PREFIX
# BASH_VERSION
# ZSH_VERSION
# Arguments:
# None
#######################################
stasetup::install_zsh () {
originaldir=$(pwd)
## copied from old setup script, with brew bits removed
cd ${HOME}
NO_SHELL=false
if [ -n "$BASH_VERSION" ]; then
first='[[ $- != *i* ]] || '
second="$(which zsh)"
append_if_not_present "$first$second" "${HOME}/.bashrc"
elif [ -n "$ZSH_VERSION" ]; then
:
else
NO_SHELL=true
fi
git clone https://github.com/ohmyzsh/ohmyzsh.git ${HOME}/.oh-my-zsh
git clone --depth=1 https://github.com/romkatv/powerlevel10k.git ${ZSH_CUSTOM:-$HOME/.oh-my-zsh/custom}/themes/powerlevel10k
git clone https://github.com/zsh-users/zsh-autosuggestions ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-autosuggestions
git clone https://github.com/zsh-users/zsh-syntax-highlighting.git ${ZSH_CUSTOM:-~/.oh-my-zsh/custom}/plugins/zsh-syntax-highlighting
if [ -f "${HOME}/.zshrc" ]; then
ZSHRC_BACKUP="${HOME}/.zshrc.backup"
while [ -f "${ZSHRC_BACKUP}" ]; do
ZSHRC_BACKUP="${ZSHRC_BACKUP}.backup"
done
ZSHRC_BACKUP_REL=$(realpath --relative-to="${HOME}" "${ZSHRC_BACKUP}")
echo "~/.zshrc exists, moving it to ~/${ZSHRC_BACKUP_REL}"
mv ${HOME}/.zshrc ${ZSHRC_BACKUP}
fi
NO_ZSHRC=false
if [ -f "${HOME}/.zshrc" ]; then
NO_ZSHRC=true
else
curl -fsSL https://raw.githubusercontent.com/STAOJ/sta-setup/master/.zshrc > ${HOME}/.zshrc
fi
if [ -f "${HOME}/.p10k.zsh" ]; then
P10K_BACKUP="${HOME}/.p10k.zsh.backup"
while [ -f "${P10K_BACKUP}" ]; do
P10K_BACKUP="${P10K_BACKUP}.backup"
done
P10K_BACKUP_REL=$(realpath --relative-to="${HOME}" "${P10K_BACKUP}")
echo "~/.p10k.zsh exists, moving it to ~/${P10K_BACKUP_REL}"
mv ${HOME}/.p10k.zsh ${P10K_BACKUP}
fi
NO_P10K=false
if [ -f "${HOME}/.p10k.zsh" ]; then
NO_P10K=true
else
curl -fsSL https://raw.githubusercontent.com/STAOJ/sta-setup/master/.p10k.zsh > ${HOME}/.p10k.zsh
fi
if [ "$NO_SHELL" = true ]; then
echo "Install successful, you don't seem to be using either bash or zsh so we can't figure out how to change your shell to zsh. Using zsh is not necessary, but it has some nice things installed like syntax highlighting and auto completion"
fi
if [ "$NO_ZSHRC" = true ]; then
echo "~/.zshrc already exists and we are unable to create an backup of it. So the recommended ~/.zshrc was not installed. To install it, consider removing your ~/.zshrc and run \n curl -fsSL https://raw.githubusercontent.com/STAOJ/sta-setup/master/.zshrc > ${HOME}/.zshrc"
fi
if [ "$NO_P10K" = true ]; then
echo "~/.p10k.zsh already exists and we are unable to create an backup of it. So the recommended ~/.p10k.zsh was not installed. To install it, consider removing your ~/.p10k.configure and run \n curl -fsSL https://raw.githubusercontent.com/STAOJ/sta-setup/master/.p10k.zsh > ${HOME}/.p10k.zsh"
fi
cd "$originaldir"
}
usage () {
echo "setup.sh [-hB]
FLAGS:
-h Show this help message, and exit.
-B Only install brew not zsh.
"
}
# run if the script is directly executed (not sourced)
if [ "${BASH_SOURCE[0]}" == "$0" ]; then
install_zsh=true
while getopts "hB" arg; do
case $arg in
h) usage; exit 0;;
B) install_zsh=false;;
*) echo "invalid arguments"; usage; exit 1;;
esac
done
echo "Installing brew"
stasetup::install_brew
if [ "$install_zsh" = true ]; then
echo "Installing zsh"
stasetup::install_zsh
fi
fi