-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinstall.sh
executable file
·218 lines (193 loc) · 6.1 KB
/
install.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
#!/bin/bash
# Copyright (c) 2016-2023 Benjamin Althues <[email protected]>
#
# Permission to use, copy, modify, and distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
usage()
{
echo Dotfiles installer by Benjamin Althues
echo --------------------------------------
echo "Usage: ./install.sh [--force]"
echo " ./install.sh [--remove]"
echo " ./install.sh [--confirm]"
echo " ./install.sh [--version]"
echo " ./install.sh [--status]"
echo
echo 'Use --confirm to install in a safe way without removing files'
echo 'Use --remove to remove all (pre-existing) files / symbolic links'
echo ' (before install)'
echo 'Use --force to remove all existing files and install in one step'
}
# Installer script version number, this gets bumped:
# - on any change to the install/update procedure
# - when a symlink is added or removed that is handled by this script
# (in dotfiles.list)
export BABABDOT_VERSION=6
# check if we are in the right directory
grep -q dotfiles .git/config 2>/dev/null
if [[ $? -ne 0 ]]; then
echo Error: cd to repository root first before running ./install.sh
exit 1
fi
if [[ -z ${RELATIVE_DOTFILES_PATH} ]]; then
RELATIVE_DOTFILES_PATH=$(pwd | sed -e "s|$HOME/||")
fi
# Symbolic linker function (does not overwrite files if they exist)
makelink()
{
file="${RELATIVE_DOTFILES_PATH}/dotfiles/$1"
ln -s "$file" 2>/dev/null
if [[ $? -ne 0 ]]; then
echo -e "SKIPPED\t${file}, the file or link already exists"
else
echo -e "LINKED\t${file}"
fi
}
pull_or_clone_github()
{
# This should always be executed from the parent of a (soon to be)
# checked out git workdirectory.
_user="$1"
_repo="$2"
echo "* $2 - https://github.com/$_user/$_repo"
if [ -d "$_repo" ]; then
cd "$_repo"
git pull
cd ..
else
git clone https://github.com/"$_user/$_repo".git
fi
}
_getdepends()
{
echo -- cloning / updating dependencies
mkdir -p "${RELATIVE_DOTFILES_PATH}/depends"
cd "${RELATIVE_DOTFILES_PATH}/depends"
pull_or_clone_github ryuslash baps1
pull_or_clone_github zsh-users zsh-autosuggestions
pull_or_clone_github zsh-users zsh-history-substring-search
pull_or_clone_github zsh-users zsh-syntax-highlighting
cd "$HOME"
}
LFS="
"
_status()
{
_sdf() {
_hf="$HOME/$1"
if [ -h "$_hf" ]; then
echo "OK $_hf"
diff -q "${RELATIVE_DOTFILES_PATH}${2}/${1}" "$_hf" || echo
elif [ -d "$_hf" ]; then
echo "!! $_hf is a directory"
diff -q "${RELATIVE_DOTFILES_PATH}${2}/${1}" "$_hf" || echo
elif [ -f "$_hf" ]; then
echo "!! $_hf is a regular file"
diff -q "${RELATIVE_DOTFILES_PATH}${2}/${1}" "$_hf" || echo
else
echo "!! $_hf does not exist"
fi
}
echo "The files below should all be symlinked to the files and"
echo "directories listed in 'dotfiles.list'. Any changes between"
echo "files are printed if found (using 'diff -q')"
echo
echo STATUS OF FILES IN dotfiles.list
echo --------------------------------
for line in $(cat "${HOME}/${RELATIVE_DOTFILES_PATH}/dotfiles.list"); do
_sdf "$line" /dotfiles
done
echo
echo 'STATUS of ~/bin DIRECTORY'
echo -------------------------
_sdf bin
}
_remove()
{
echo ':: Removing files'
for line in $(cat "${HOME}/${RELATIVE_DOTFILES_PATH}/dotfiles.list"); do
rm -vf "$line"
done
# unlink bin directory separately
if [ -h bin ]; then
rm -f bin 2>/dev/null
elif [ -d bin ]; then
echo Error: bin is not a symbolic link but a directory.
echo
echo ~/bin should either be non-existent or a symlink to
echo ${RELATIVE_DOTFILES_PATH} Please fix and run again.
exit 2
fi
}
function _confirm {
# create folder for vim bak and swp files
mkdir -p .vim-bak-swp
# try to create symlinks for the defined files and directories
echo -- creating symlinks to dotfiles
for line in $(cat "${HOME}/${RELATIVE_DOTFILES_PATH}/dotfiles.list"); do
makelink "$line"
done
# link bin directory separately
echo -- creating ~/bin symlink
ln -s ${RELATIVE_DOTFILES_PATH}/bin 2>/dev/null
# skip baps1 section below if it's already installed
echo -- checking baps1
command -v baps1 && return
# install or download baps1
if (command -v cc && command -v make); then
echo ++ building/installing baps1
INST_PATH="${HOME}/${RELATIVE_DOTFILES_PATH}/bin" make -e \
-C "${HOME}/${RELATIVE_DOTFILES_PATH}/depends/baps1/src" install
make -C "${HOME}/${RELATIVE_DOTFILES_PATH}/depends/baps1/src" clean
else
echo ++ downloading/installing baps1
curl -o "${HOME}/${RELATIVE_DOTFILES_PATH}/bin/baps1" \
http://files.babab.nl/programs/x86_64/baps1/linux/baps1
chmod +x "${HOME}/${RELATIVE_DOTFILES_PATH}/bin/baps1"
fi
}
if [[ ! "$1" ]]; then
usage
exit 1
fi
if [ -z "$HOME" ]; then
echo error: HOME is empty or unset
echo
echo Environment variable HOME must be set for this script to work
exit 2
fi
# cd to home; all further actions are relative to $HOME
cd "$HOME"
case $1 in
'--remove')
_remove
;;
'--confirm')
_confirm
_getdepends
;;
'--force')
_remove
_confirm
_getdepends
;;
'--version')
echo "install.sh v$BABABDOT_VERSION"
;;
'--status')
_status
;;
*)
usage
exit 1
;;
esac