-
Notifications
You must be signed in to change notification settings - Fork 0
/
setup
executable file
·131 lines (115 loc) · 2.85 KB
/
setup
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
#!/bin/sh
# 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 .
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")")
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, set V=1 to see diff"
test -n "$V" && diff -u "$HOME/$file" "$base/$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
parse_arguments "$@"
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
mkdir -p "$HOME"/.config/git
ln -fs ../../"$base"/.gitignore_global "$HOME"/.config/git/ignore || die
}
create_symlinks() {
for file in $targets; do
ln -sf "$base/$file" .${file#.} || die
done
}
create_file() {
mkdir -p "$(dirname "$1")" || return 1
if ! test -f "$1"; then
cat > "$1"
elif diff "$1" - | grep .; then
warn "$1 already exists with above diffs from expected; not modifying"
fi
if test -n "$V"; then ( set -x; cat "$1" ) fi
}
setup_git() {
local verbose
if test -z "$UNAME"; then
verbose=1
UNAME=$(id -F "$USER")
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
# Create files that should be local to this machine
( V=$verbose create_file "$HOME/.gitconfig-local" ) <<- EOF
[user]
email = ${EMAIL?}
name = ${UNAME?}
EOF
}
setup_local_files() {
create_file "$HOME/.bash-local" <<- EOF
EMAIL="$EMAIL"
UNAME="$UNAME"
GIT_REMOTE_IDENTITY='
[email protected]:proj $EMAIL $UNAME
'
export EMAIL UNAME GIT_REMOTE_IDENTITY
EOF
create_file "$HOME/.config/$USER/CONFIG_SITE" <<- 'EOF'
if test "${prefix}" = NONE; then
prefix="$HOME/$(uname -m)/$(uname -s)"
CPPFLAGS="-I${prefix}/include"
LDFLAGS="-L${prefix}/lib"
fi
EOF
}
main() {
init "$@"
create_symlinks
setup_git
setup_local_files
ln -sf "$base/scripts" scripts
mkdir -p "$HOME"/.git-template-links
(cd "$HOME"/.git-template-links; ln -sf "$base"/git-templates/hooks)
(cd "$HOME"/.config/"$USER"
curl -s -O https://raw.githubusercontent.com/git/git/master/contrib/completion/git-completion.bash
)
}
main "$@"