-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinit.sh
executable file
·342 lines (289 loc) · 10.6 KB
/
init.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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#!/bin/bash
# init.sh
# Initialize (nearly) everything for a new machine. Install Homebrew (and use
# it to install other things) if on OS X, link dotfiles, and install Emacs
# packages.
#
# Usage:
# ./init.sh path/to/dotfile/repo
################################################################################
# Define helper functions
################################################################################
# Colors
# https://stackoverflow.com/questions/2924697/how-does-one-output-bold-text-in-bash
normal=$(tput sgr0)
red=$(tput setaf 1)
green=$(tput setaf 2)
# Location of dotfiles repo
if [ -z "$1" ]; then
echo "${red}Run script using ./init.sh path/to/dotfiles/repo${normal}"
exit 1
fi
dotfile_dir=$1
# Check if something is installed
# Usage: not_installed <program>
# https://stackoverflow.com/a/677212
not_installed() {
return ! command -v $1 &> /dev/null
}
# Make file if it doesn't already exist
# Usage: maybe_touch <path>
maybe_touch() {
if [ ! -f "$1" ]; then
touch -p "$1"
if [ "$?" -eq 0 ]; then
echo "${green}Created $1${normal}"
else
echo "${red}Could not create $1${normal}"
fi
fi
}
# Make directory if it doesn't already exist
# Usage: maybe_mkdir <path>
maybe_mkdir() {
if [ ! -d "$1" ]; then
mkdir -p "$1"
if [ "$?" -eq 0 ]; then
echo "${green}Created $1${normal}"
else
echo "${red}Could not create $1${normal}"
fi
fi
}
# Create symlinks verbosely
# Usage: try_symlink <src> <dst=src>
# Performs: ln -s "$dotfile_dir/$src" "$(pwd)/$dst"
# See https://stackoverflow.com/a/33419280 for reference on bash optional arguments
try_symlink() {
src=$1
dst=${2:-$src}
if [ -L "$dst" ]; then
echo "${red}Did not link $(pwd)/$dst: symlink already exists${normal}"
elif [ ! -f "$src" ]; then
ln -s "$dotfile_dir/$src" "$dst"
if [ "$?" -eq 0 ]; then
echo "${green}Linked $(pwd)/$dst -> $dotfile_dir/$src${normal}"
else
echo "${red}Could not link $(pwd)/$dst${normal}"
fi
else
echo "${red}Did not link $dst: non-symlink file already exists. Merge $dst into $dotfile_dir/$src first and then delete $dst before retrying${normal}"
fi
}
# Set environment variable verbosely
# Usage: try_setenv <variable> <value>
try_setenv() {
var=$1
val=$2
if [ -z "$var" ]; then
echo "export $var=\"$val\"" >> "$HOME/.bashrc-local"
if [ "$?" -eq 0 ]; then
echo "${green}Set $var to $val in $HOME/.bashrc-local${normal}"
else
echo "${red}Could not set $var to $val in $HOME/.bashrc-local${normal}"
fi
else
echo "${red}Did not set $var to $val in $HOME/.bashrc-local: already has a value${normal}"
fi
}
# Add to PATH environment variable verbosely
# Usage: try_addpath <dir> <front>
try_addpath() {
dir=$1
front=$2 # whether to add dir to front of PATH
if [ -z "$(grep $dir $HOME/.bashrc-local)" ]; then
if [ $front -eq 1 ]; then
echo "export PATH=$dir:\$PATH" >> "$HOME/.bashrc-local"
else
echo "export PATH=\$PATH:$dir" >> "$HOME/.bashrc-local"
fi
if [ "$?" -eq 0 ]; then
echo "${green}Added $dir to PATH in $HOME/.bashrc-local${normal}"
else
echo "${red}Could not add $dir to PATH in $HOME/.bashrc-local${normal}"
fi
else
echo "${red}Did not add $dir to PATH in $HOME/.bashrc-local: already there${normal}"
fi
}
# Length of time before timing out
timeout_length="30s"
# Print result of timeout downloading
# Usage: timeout_result <retcode> <filename>
function timeout_result {
if [ $1 -eq 124 ]; then
echo "${red}Killed downloading $2: timed out${normal}"
elif [ $1 -ne 0 ]; then
echo "${red}Could not download $2${normal}"
else
echo "${green}Successfully downloaded $2${normal}"
fi
}
################################################################################
# Install OS-specific things
################################################################################
case $OSTYPE in
darwin*) # MacOS
if not_installed brew; then
# Install brew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Add brew to PATH
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> "$HOME/.profile"
eval "$(/opt/homebrew/bin/brew shellenv)"
fi
brew install coreutils
brew install emacs
brew install git
brew install pandoc
brew install pdfsandwich
brew install python
brew install rename
brew install tmux
brew install wget
brew install --cask lyx
brew install --cask mactex
brew install --cask meld
brew install --cask skim
# The following are easier to build using Brew than in Julia
brew install gcc # HDF5.jl
brew install cmake # Polynomials.jl
# Set environment variables
try_addpath "/Library/TeX/texbin" 0
try_setenv "JUPYTER" "/opt/homebrew/bin/jupyter"
try_setenv "JUPYTER_PATH" "/opt/homebrew/share/jupyter"
try_setenv "JUPYTERLAB_DIR" "/opt/homebrew/share/jupyter/lab"
# Copy SF Mono font for use in non-Terminal apps (symlinking doesn't seem like enough)
# https://osxdaily.com/2018/01/07/use-sf-mono-font-mac/
# https://apple.stackexchange.com/a/376828
cp /System/Applications/Utilities/Terminal.app/Contents/Resources/Fonts/SF-Mono-*.otf "$HOME/Library/Fonts/"
if [ "$?" -eq 0 ]; then
echo "${green}Copied SF Mono font files to $HOME/Library/Fonts${normal}"
else
echo "${red}Could not copy SF Mono font files to $HOME/Library/Fonts${normal}"
fi
# Use SF Mono in Meld
# https://github.com/yousseb/meld/issues/38#issuecomment-547577592
defaults write org.gnome.meld "/org/gnome/meld/use-system-font" 0
defaults write org.gnome.meld "/org/gnome/meld/custom-font" "SF Mono, 14"
if [ "$?" -eq 0 ]; then
echo "${green}Set SF Mono as Meld font${normal}"
else
echo "${red}Could not set SF Mono as Meld font${normal}"
fi
# Copy Mac key bindings
maybe_mkdir "$HOME/Library/KeyBindings"
cp "$dotfile_dir/DefaultKeyBinding.dict" "$HOME/Library/KeyBindings"
# Symlink Mac keyboard layouts
cd "$HOME/Library/Keyboard Layouts"
try_symlink "$dotfile_dir/MyLayout.keylayout" "MyLayout.keylayout"
# Symlink/copy AppleScripts
services_dir="$HOME/Library/Services/"
cp -r "$dotfile_dir/applescripts/Make Desktop alias.workflow" services_dir
cp -r "$dotfile_dir/applescripts/Open in Chrome.workflow" services_dir
cp -r "$dotfile_dir/applescripts/Open in Skim.workflow" services_dir
cp -r "$dotfile_dir/applescripts/Open in TextEdit.workflow" services_dir
my_timeout="gtimeout $timeout_length"
;;
linux-gnu*)
apt-get update
if not_installed bc; then
sudo apt-get install bc
if [ "$?" -eq 0 ]; then
echo "${green}Installed bc${normal}"
else
echo "${red}Could not install bc${normal}"
fi
fi
my_timeout="timeout $timeout_length"
;;
*)
my_timeout="timeout $timeout_length"
;;
esac
################################################################################
# Link dotfiles
################################################################################
cd $HOME
# Create symlinks
for file in ".bashrc" ".tmux.conf" ".emacs" ".emacs-modes.el" ".gitconfig"; do
try_symlink "$file"
done
# Create local dotfiles if they don't already exist
for file in ".bashrc-local" ".emacs-local.el" ".gitconfig-local"; do
try_touch "$file"
done
# Pre-commit hook
chmod u+x "$dotfile_dir/pre-commit"
cd "$dotfile_dir/.git/hooks"
try_symlink "pre-commit"
# Julia startup
maybe_mkdir "$HOME/.julia/config"
cd "$HOME/.julia/config"
try_symlink "startup.jl"
try_symlink "startup_ijulia.jl"
# Stata startup
maybe_mkdir "$HOME/Documents/Stata"
cd "$HOME/Documents/Stata"
try_symlink "profile.do"
# TeX files
cd $dotfile_dir
texfiles=$(find tex/latex -name *.cls -o -name *.sty)
bstfiles=$(find bibtex/bst -name *.bst)
if not_installed kpsewhich; then
echo "${red}Didn't link TeX files: make sure /Library/TeX/texbin is in PATH and re-run init.sh${normal}"
else
texdir=$(kpsewhich -var-value=TEXMFHOME)
maybe_mkdir "$texdir"
# Style and class files
maybe_mkdir "$texdir/tex/latex"
cd "$texdir"
for file in $texfiles; do
try_symlink "$file"
done
# Bibliography style files
maybe_mkdir "$texdir/bibtex/bst"
cd "$texdir"
for file in "$bstfiles"; do
try_symlink $file
done
fi
# Pandoc templates
maybe_mkdir "$HOME/.pandoc"
maybe_mkdir "$HOME/.pandoc/templates"
cd "$HOME/.pandoc/templates"
try_symlink "pandoc/templates/GitHub.html5" "GitHub.html5"
################################################################################
# Install OS-agnostic things
################################################################################
cd "$HOME"
# Create .emacs.d if it doesn't already exist
maybe_mkdir "$HOME/.emacs.d"
maybe_mkdir "$HOME/.emacs.d/backup"
cd "$HOME/.emacs.d"
# Install emacs packages from package manager
emacs --script "$dotfile_dir/elpa-install.el"
# Emacs Stata mode
$my_timeout git clone "https://github.com/louabill/ado-mode.git"
timeout_result "$?" "ado-mode"
# Install Python packages (Jupyter + Plotly)
pip install -r $dotfile_dir/requirements.txt
# Install JupyterLab extensions that aren't installable via pip
jupyter labextension install jupyterlab-emacskeys
# Tmux plugin manager
if not_installed tmux; then
echo "${red}Didn't install tmux plugin manager: make sure tmux is installed and re-run init.sh${normal}"
else
if not_installed bc; then
echo "${red}bc is not installed; needed to compare tmux versions${normal}"
echo "${red}Didn't install tmux plugin manager: couldn't compare tmux versions${normal}"
else
tpmdir="$HOME/.tmux/plugins/tpm"
if [ -d "$tpmdir" ]; then
echo "${red}Didn't install tmux plugin manager: directory already exists${normal}"
elif [ $(echo "$TMUX_VERSION < 1.9" | bc) -eq 1 ]; then
echo "${red}Didn't install tmux plugin manager: need at least tmux version 1.9${normal}"
else
$my_timeout git clone "https://github.com/tmux-plugins/tpm" "$tpmdir"
fi
fi
fi