-
Notifications
You must be signed in to change notification settings - Fork 0
/
install
executable file
·135 lines (109 loc) · 3.24 KB
/
install
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
#!/bin/sh
# Lucas Oberwager dotfiles install script
# contains code from and inspired by
# https://github.com/twpayne/chezmoi
set -e
FORCE=0
LOG_LEVEL=2
GITHUB_DOWNLOAD=https://github.com/Watt3r/dotfiles.git
INSTALL_DIR=~/.dotfiles
CONFIG="install.conf.yaml"
DOTBOT_DIR="dotbot"
DOTBOT_BIN="bin/dotbot"
usage() {
this="$1"
cat <<EOF
${this}: Download Lucas Oberwager's dotfiles
Usage: ${this} [-i installdir] [-l] [-h] [-f]
-i Set the installation directory (default is ${INSTALL_DIR}).
-l Set the log level (default is ${LOG_LEVEL}).
-f Force install, ignore if repo is already up to date
-h Show this help message.
EOF
exit 2
}
check_command() {
command -v "$1" > /dev/null 2>&1 || { echo >&2 "$1 is required but it's not installed. Aborting."; exit 1; }
}
get_remote_url() {
git -C "$1" config --get remote.origin.url
}
valid_repo() {
remote_url=$(get_remote_url "$1")
if [ "$remote_url" == "$GITHUB_DOWNLOAD" ]; then
return 0
else
return 1
fi
}
install_xcode_cli() {
if [ "$(uname)" = "Darwin" ]; then
if ! xcode-select --print-path &>/dev/null; then
echo "Xcode CLI tools not found. Installing them..."
xcode-select --install
echo "Please continue with the script after the installation is complete."
exit 1
fi
fi
}
log_info() {
[ 2 -le "${LOG_LEVEL}" ] || return 0
echo info "$@" 1>&2
}
log_crit() {
[ 0 -le "${LOG_LEVEL}" ] || return 0
echo critical "$@" 1>&2
}
parse_args() {
while getopts "i:l:fh" arg; do case "$arg" in
i) INSTALL_DIR="${OPTARG}";;
l) [ "$OPTARG" -ge 0 -a "$OPTARG" -le 2 ] 2>/dev/null && LOG_LEVEL="$OPTARG" || { echo "Error: -l requires an integer argument"; exit 1; };;
f) FORCE=1;;
*) usage "$0";;
esac; done
}
download_dotfiles() {
if [ ! -d "${INSTALL_DIR}" ]; then
log_info "Cloning dotfiles repo from ${GITHUB_DOWNLOAD}"
git clone --recurse-submodules ${GITHUB_DOWNLOAD} ${INSTALL_DIR}
return 1
fi
if ! valid_repo "${INSTALL_DIR}"; then
log_crit "Directory ${INSTALL_DIR} is not a valid repo, moving it for backup."
mv -n "${INSTALL_DIR}" "${INSTALL_DIR}-backup"
log_info "Cloning dotfiles repo from ${GITHUB_DOWNLOAD}"
git clone --recurse-submodules ${GITHUB_DOWNLOAD} ${INSTALL_DIR}
return 1
fi
git -C "${INSTALL_DIR}" fetch --quiet
LOCAL_COMMIT=$(git -C "${INSTALL_DIR}" rev-parse HEAD)
REMOTE_COMMIT=$(git -C "${INSTALL_DIR}" rev-parse @{u})
if [ "$LOCAL_COMMIT" != "$REMOTE_COMMIT" ] || [ "$FORCE" -ne 0 ]; then
log_info "Pulling remote changes"
git -C "${INSTALL_DIR}" pull
return 1
fi
return 0
}
dotbot() {
cd "${INSTALL_DIR}"
git -C "${DOTBOT_DIR}" submodule sync --quiet --recursive
git submodule update --init --recursive "${DOTBOT_DIR}"
"${INSTALL_DIR}/${DOTBOT_DIR}/${DOTBOT_BIN}" -d "${INSTALL_DIR}" -c "${CONFIG}" "${@}"
}
main() {
parse_args "$@"
install_xcode_cli
check_command git
if [ "$(whoami)" = "root" ]; then
log_crit "Do not run as root. Install dotfiles for a specific user."
exit 1
fi
if download_dotfiles ; then
log_info "Proper dotfiles repo exists and up-to-date. Goodbye."
exit 0
fi
dotbot || (log_crit "Installation failed" && exit 1)
log_info "Successfully installed."
}
main "$@"