-
Notifications
You must be signed in to change notification settings - Fork 2
/
dotfile_installer
290 lines (253 loc) · 7.42 KB
/
dotfile_installer
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
#!/bin/bash
# Helper functions for Dotfies Manager
#
# This file contains a bunch of helper functions for installing files and
# directories using the Dotfiles Manager script.
#
# Author: Darshit Shah <darnir@gmail.com>
# License: MIT
#XXX: Bash strict mode is not in effect during backup operations. Investigate
# This helper function gets the location of where the configuration file should
# exist on the system based on the keywords passed to it. Supported keywords
# are:
# * etc: Attempts to store the file in the user's /etc directory. The command
# should be executed with superuser privileges. TODO: Automatically set
# SUDO when the location is /etc
# * home: Stores the file in the user's $HOME dir with a dot (.) affixed to
# the filename.
# * userconf: Stored the file in the user's configuration directory, i.e.
# ~/.config/.
# * bin: Symlink the file in the /usr/local/bin/ directory. TODO:
# Automatically set SUDO when location is bin
# * systemd-user: Attempts to install the file as a unit file in the systemd
# user configuration directory, ~/.config/systemd/user/. TODO: Automatically
# set the file to to create a hardlink when installing a systemd unit file
# * autostart: Install the file to ~/.config/autostart.
# If none of the above keywords are provided, the parameter is assumed to
# contain the absolute path to the directory where the file should be
# installed.
_get_conf_file() {
local confpath
local confopt="${1^^}"
case "$1" in
etc) confpath="/etc/";;
home) confpath="$HOME/.";;
userconf) confpath="$HOME/.config/";;
bin) confpath="/usr/local/bin/";;
systemd-user) confpath="$HOME/.config/systemd/user/";;
autostart) confpath="$HOME/.config/autostart/";;
xdg_*_home) confpath="${!confopt}/";;
*) confpath="$1/"
esac
mkdir -p "$confpath"
confpath="${confpath}${_CONF_NAME}"
echo "$confpath"
}
_check_sudo() {
if [[ $1 == true ]]
then
echo "sudo"
else
echo ""
fi
}
_set_linktype() {
if [[ -z $1 || $1 == true ]]
then
echo "-s "
else
echo ""
fi
}
_test_source_file() {
log_info_n "Checking if ${_CONF_NAME} exists in package\t"
if [ ! -f "${_LOC_FILE}" ]
then
log_warn "[404]"
fi
log_okay "[Done]"
}
_backup_target() {
log_info_n "Checking if ${_CONF_FILE} exists\t"
if [ -f "${_CONF_FILE}" ]
then
log_warn "[Exists]"
_BACKUPNAME="${_BCK_FILE}"
_BCKNO=1
while [ -f "${_BACKUPNAME}" ]; do
_BACKUPNAME="${_BCK_FILE}.${_BCKNO}"
_BCKNO=$((_BCKNO+1))
done
log_info_n "Target file found, saving to ${_BACKUPNAME}\t"
if [ -L "${_CONF_FILE}" ]
then
REAL_CONF=$(readlink -m "${_CONF_FILE}")
${SUDO} cp -R "${REAL_CONF}" "${_BACKUPNAME}"
${SUDO} rm "${_CONF_FILE}"
else
${SUDO} mv "${_CONF_FILE}" "${_BACKUPNAME}"
fi
log_okay "[Done]"
else
${SUDO} rm "${_CONF_FILE}" 2>/dev/null || true
log_okay "[Done]"
fi
}
# Reuse backup code from backupfile method
_backup_target_dir() {
log_info_n "Checking if ${_CONF_FILE} exists\t"
if [ -d "${_CONF_FILE}" ]
then
log_warn "[Exists]"
log_info_n "Target directory found, saving to ${_BCK_FILE}\t"
$SUDO mv -v "${_CONF_FILE}" "${_BCK_FILE}"
log_okay "[Done]"
else
log_okay "[Done]"
fi
}
_log_installed() {
local srcfile="$1"
local dstfile="$2"
local lnktype="$3"
if [[ $lnktype == '-s ' ]]; then
echo -e " * $srcfile\t-> $dstfile" >> "$TEMPDB"
else
echo -e " * $srcfile\t--> $dstfile" >> "$TEMPDB"
fi
}
_real_install() {
local sudo="$1"
local linktype="$2"
local loc_file="$3"
local conf_file="$4"
log_info_n "Linking dotfile ${_CONF_NAME} to ${conf_file}\t"
# TODO: Test if the linktype is possible. Else fallback to a copy and warn
# user about it.
#
# Shellcheck gives a warning here asking to doublequote the linktype
# variable. However, that will break the ln command since the variable may
# be empty sometimes. Doublequoting will pass an empty parameter to ln
# causing the command to fail.
${sudo} ln ${linktype}"${loc_file}" "${conf_file}"
log_okay "[Done]"
}
_begin_install() {
log_all_n "white" "Installing: "
log_all_n "nc" "$(eval printf "%-25s" "$1")"
log_std ""
}
_end_install() {
_log_installed "$_LOC_FILE" "$_CONF_FILE" "$linktype"
log_all "green" "[Done]"
}
# Function: install_file()
# Parameter List:
# $1 : Conf File Name
# $2 : Install Path for Conf File
# $3 : Needs root privileges to install?
# $4 : Is a symlink? (Default: Yes)
install_file() {
_begin_install "$@"
local _CONF_NAME="$1"
local _CONF_FILE=$(_get_conf_file "$2")
local _BCK_FILE=${_CONF_FILE}.bck
local _LOC_FILE=$(pwd)/Packages/${pkgname}/${_CONF_NAME}
local SUDO=$(_check_sudo "$3")
local linktype=$(_set_linktype "$4")
_test_source_file
_backup_target
_real_install "${SUDO}" "${linktype}" "${_LOC_FILE}" "${_CONF_FILE}"
_end_install
}
# TODO: Use a param to end_install to cause a yellow warning or red failed
# output
install_private_file() {
_begin_install "$@"
local _CONF_NAME="$1"
local _CONF_FILE=$(_get_conf_file "$2")
local _BCK_FILE=${_CONF_FILE}.bck
local _LOC_FILE="/media/truecrypt1/${_CONF_NAME}"
local SUDO=$(_check_sudo "$3")
local linktype=$(_set_linktype "$4")
log_info_n "Checking if ${_LOC_FILE} is mounted\t"
if [[ ! -f "${_LOC_FILE}" ]]
then
log_warn "[404]"
log_info "Will continue and create symlink to location"
else
_backup_target
fi
_real_install "${SUDO}" "${linktype}" "${_LOC_FILE}" "${_CONF_FILE}"
_end_install
}
install_directory() {
_begin_install "$@"
local _CONF_NAME="$1"
local _CONF_FILE=$(_get_conf_file "$2")
local _BCK_FILE=${_CONF_FILE}bck
local _LOC_FILE=$(pwd)/Packages/${pkgname}/${_CONF_NAME}
local SUDO=$(_check_sudo "$3")
local linktype=$(_set_linktype "$4")
_backup_target_dir
_real_install "${SUDO}" "${linktype}" "${_LOC_FILE}" "${_CONF_FILE}"
_end_install
}
depends() {
./dotfiles -b "$pkgname" -d "$1"
}
################################################################################
# Custom Functions for use by the dotfile package installers #
################################################################################
install_packages() {
sudo ./pacapt --noconfirm -S $*
}
install_aur() {
if ! isarchlinux; then
log_warn "System is not ArchLinux. Packages not installed."
log_info "Please install $1 manually"
return
fi
# We are in Arch Linux
echo "Testing if $1 is installed"
if pacman -Q "$1" &> /dev/null; then
echo "Package already installed"
return
fi
echo "Installing $1 from AUR"
pushd /tmp
wget https://aur.archlinux.org/cgit/aur.git/snapshot/"$1".tar.gz
tar -xvzf "$1".tar.gz
pushd "$1"
makepkg -si
popd
popd
}
safetouch() {
if [ ! -f "$1" ]
then
log_info "Touching $1"
touch "$1"
else
log_info "$1 exists, not touching"
fi
}
is_in_path() {
exec_name="$1"
if ! which "$exec_name" &> /dev/null; then
return 1
else
return 0
fi
}
isarchlinux() {
log_info_n "Checking if current system is Arch Linux\t"
if which pacman &> /dev/null
then
log_okay "[Done]"
return 0
else
log_warn "[NotArch]"
return 1
fi
}