-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup
More file actions
executable file
·164 lines (143 loc) · 3.7 KB
/
Copy pathsetup
File metadata and controls
executable file
·164 lines (143 loc) · 3.7 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
#!/bin/sh
# Setup an account on a new machine.
# Create symbolic links in $HOME to all entries in this directory except
# those listed in denylist. All links created in $HOME will have a .
# prepended if the name here does not contain a leading .
# This script attempts to get bash-completions from github.
err() { printf "%s${1:+\n}" "$@"; } >&2
die() { err "$@"; exit 1; }
warn() { err "$@"; }
find_md5_executable() {
for MD5 in md5sum md5; do $MD5 > /dev/null </dev/null 2>&1 && break; done
}
establish_context() {
{
base=$(dirname "$(realpath "$0" || readlink -f "$0" ||
{ cd "$(dirname "$0")" && pwd -P | sed -e 's@$@/foo@'; }
)");
} 2> /dev/null
cd "$HOME" || die
}
parse_arguments() {
unset force
while test $# -gt 0; do
case $1 in
-f) force=1;;
*) die unexpected argument: $1;;
esac
shift
done
}
validate_md5_sums() {
unset abort
for file in $targets; do
if test -f "$file" && test "$($MD5 < "$HOME/$file")" != \
"$($MD5 < "$base/$file")"; then
warn "$file is different; staging in \$HOME"
git add "$file"
abort=1
fi
done
if test -n "$abort"; then
if test -z "$force"; then
die "-f to force overwrite"
fi
warn overwriting
fi
}
init() {
find_md5_executable
establish_context # Changes directory to $HOME
parse_arguments "$@"
setup_git
targets=$(ls -a "$base" | awk 'NR==FNR {a[$0] = 1; next} ! ($0 in a)' "$base/denylist" -)
validate_md5_sums
mkdir -p "$HOME"/.bash-history-dir || die
mkdir -p "$HOME"/.config/ctags || die
ln -fs "$HOME"/.ctags "$HOME"/.config/ctags/base.ctags || die
ln -fs "$base"/config/vim "$HOME"/.config/vim || die
ln -fs "$base"/config/git "$HOME"/.config/git || die
}
create_symlinks() {
for file in $targets; do
ln -sf "$base/$file" .${file#.} || die
done
}
create_file() {
# Create files that should be local to this machine
mkdir -p "$(dirname "$1")" || return 1
if ! test -f "$1"; then
cat > "$1"
elif diff "$1" - | grep -q .; then
warn "$1 already exists; not modifying"
fi
test -z "$NO_GIT" && git add "$1"
}
setup_git() {
local verbose
if test -z "$UNAME"; then
verbose=1
UNAME=$(id -F "$USER" 2> /dev/null)
fi
for n in EMAIL UNAME; do
eval v=\$$n;
if test -z "$v"; then
printf "$n not set in environment. Enter value: "
eval read $n
fi
done
( NO_GIT=1 create_file .gitconfig-local ) <<- EOF
[user]
email = ${EMAIL?}
name = ${UNAME?}
EOF
if git rev-parse --is-inside-work-tree > /dev/null 2>&1; then
if ! git diff-index --quiet HEAD --; then
die '$HOME is a dirty git repo. Aborting'
fi
else
git init -b main
fi
git add .gitconfig-local
}
setup_local_files() {
create_file "$HOME/.bash-local" <<- EOF
EMAIL="$EMAIL"
UNAME="$UNAME"
GIT_REMOTE_IDENTITY='
git@github.com:proj $EMAIL $UNAME
'
export EMAIL UNAME GIT_REMOTE_IDENTITY
EOF
SDKROOT=$(xcrun --sdk macosx --show-sdk-path 2> /dev/null)
if test -n "$SDKROOT"; then
echo "export SDKROOT=$SDKROOT" >> $HOME/.bash-local
fi
create_file "$HOME/.config/CONFIG_SITE" <<- 'EOF'
if test "${prefix}" = NONE; then
prefix="$HOME/$(uname -m)/$(uname -s)"
CPPFLAGS="$CPPFLAGS -I${prefix}/include"
LDFLAGS="$LDFLAGS -L${prefix}/lib"
if test -n "$SDKROOT"; then
CFLAGS="-isysroot $SDKROOT $CFLAGS"
LDFLAGS="-Wl,-syslibroot,$SDKROOT $LDFLAGS"
fi
fi
EOF
create_file "$HOME/.config/meson.ini" <<- EOF
[built-in options]
prefix = '$HOME/$(uname -m)/$(uname -s)'
EOF
}
main() {
init "$@"
create_symlinks
setup_local_files
ln -sf "$base/scripts" scripts
mkdir -p "$HOME"/.config/bash
(cd "$HOME"/.config/bash
curl -s -O https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
)
git ciam 'Setup script modified dotfiles in $HOME'
}
main "$@"