-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathset_up_machine.sh
More file actions
executable file
·312 lines (262 loc) · 9.31 KB
/
Copy pathset_up_machine.sh
File metadata and controls
executable file
·312 lines (262 loc) · 9.31 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
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
#!/bin/bash
# set_up_machine.sh
# Set up (nearly) everything for a new machine. Install Homebrew (and use
# it to install other things) if on MacOS, link dotfiles, and install Emacs
# packages.
#
# Usage:
# ./set_up_machine.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: is_installed <program>
# https://stackoverflow.com/a/677212
is_installed() {
command -v "$1" >/dev/null 2>&1
}
# Make file if it doesn't already exist
# Usage: maybe_touch <path>
maybe_touch() {
if [ ! -f "$1" ]; then
mkdir -p "$(dirname $1)"
touch "$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 ! is_installed brew; then
# Install Homebrew
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
# Add Homebrew to PATH
echo 'eval "$(/opt/homebrew/bin/brew shellenv)"' >> "$HOME/.profile"
eval "$(/opt/homebrew/bin/brew shellenv)"
fi
brew install coreutils # GNU file, shell, and text utilities
brew install emacs
brew install git
brew install ipython
brew install jupyterlab
brew install pdfsandwich # PDF OCR
brew install python
brew install rename
brew install tmux
brew install wget
brew install --cask lyx
brew install --cask mactex
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
# 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 ! is_installed bc; then
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" ".condarc"; 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
maybe_touch "$file"
done
# Pre-commit hook
chmod u+x "$dotfile_dir/pre-commit"
cd "$dotfile_dir/.git/hooks"
try_symlink "pre-commit"
# Claude Code startup
maybe_mkdir "$HOME/.claude"
cd "$HOME/.claude"
try_symlink "claude-keymap.json" "keybindings.json"
# Julia startup
maybe_mkdir "$HOME/.julia/config"
cd "$HOME/.julia/config"
try_symlink "startup.jl"
try_symlink "startup_ijulia.jl"
# Python startup
maybe_mkdir "$HOME/.ipython/profile_default"
cd "$HOME/.ipython/profile_default"
try_symlink "ipython_config.py"
# 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 is_installed kpsewhich; then
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
else
echo "${red}Didn't link TeX files: make sure /Library/TeX/texbin is in PATH and re-run init.sh${normal}"
fi
################################################################################
# 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/install-emacs-packages.el"
# Emacs Stata mode
$my_timeout git clone "https://github.com/louabill/ado-mode.git"
timeout_result "$?" "ado-mode"