-
Notifications
You must be signed in to change notification settings - Fork 0
/
install.sh
executable file
·291 lines (263 loc) · 9.36 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
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
291
#!/bin/bash
#
# install.sh
# by @picas (me at picas dot fr)
# <http://github.com/e-picas/dotfiles.git>
# package installer script licensed under CC BY-NC-SA 4.0 <http://creativecommons.org/licenses/by-nc-sa/4.0/>
#
#######################################################################
# script infos
declare -rx VERSION="1.1.0"
declare -rx NAME="picas/dotfiles"
declare -rx REPO="http://github.com/e-picas/dotfiles"
declare -rx USAGE=$(cat <<EOT
## ${NAME} installer & updater
This script will try to install or update all dotfiles, binaries and sub-directories of this clone
in a directory (by default, current user 'HOME'). The default behavior is to be fully interactive
(the built-in 'find' command used in this script needs to receive an 'y' answer to process).
usage:
$0 [-hV]
$0 [-fv] <install_dir=\$HOME/> <type=all> <mode=symlinks>
arguments:
install_dir = \$HOME : installation target directory
type = all : installation type in 'all', 'bin', 'home', 'subdirs' or any sub-directory name
mode = symlinks : define the copy mode:
- 'symlinks' to symlink files from repository
- 'hard-copies' to copy files as new hard files
options (must be used in alphabetical order if multiple):
-f|--force : force installing all files (NOT interactive)
-v|--verbose : increase script verbosity
-h|--help : get this help information
-V|--version : get the script version
<${REPO}> - ${VERSION}
EOT
);
declare -x _FORCED=false
declare -x _VERBOSE=false
declare -rx DEFUSERDIR="$(cd ~ && pwd)"
declare -rx HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
declare -rx AUTOINSTALLER='auto-install.sh'
declare -rx BASHRCDIR='.bashrc.d'
declare -rx MODELSGLOB="*.model"
declare -a EXCLUDEDDIRS=( .idea .git bin dev docs etc home modules )
#######################################################################
# get_absolute_path()
get_absolute_path() {
local cwd="$(pwd)"
local path="$1"
while [ -n "$path" ]; do
cd "${path%/*}" 2>/dev/null;
local name="${path##*/}"
path="$($(type -p greadlink readlink | head -1) "$name" || true)"
done
pwd
cd "$cwd"
}
# usage ()
usage () {
echo "${USAGE}"
}
# version ()
version () {
echo "${NAME} ${VERSION} (${REPO})"
}
# error ( str='' )
error () {
{ echo "> $*"
echo '---'
usage
} >&2
exit 1
}
#######################################################################
if [ "$1" == '-h' ]||[ "$1" == '--help' ]||[ "$1" == 'help' ]
then
usage
exit 0
fi
if [ "$1" == '-V' ]||[ "$1" == '--version' ]||[ "$1" == 'version' ]
then
version
exit 0
fi
if [ "$1" == '-f' ]||[ "$1" == '--force' ]
then
export _FORCED=true
shift
fi
if [ "$1" == '-v' ]||[ "$1" == '--verbose' ]
then
export _VERBOSE=true
shift
fi
# install directory
declare -x INSTALLDIR="${1:-${DEFUSERDIR}}"
if [ ! -d "$INSTALLDIR" ]
then
error "unknown installation directory '${INSTALLDIR}'!"
fi
# type of installation
declare -x INSTALLTYPE="${2:-all}"
# mode of installation
declare -x INSTALLMODE="${3:-symlinks}"
# no interaction in FORCED mode
LNOPTS='-s'
CPOPTS=''
if $_FORCED
then
FINDEXECTYPE='-exec '
LNOPTS+=' -f'
CPOPTS+=' -f'
else
FINDEXECTYPE='-ok '
fi
# list of excluded directories for sub-directories
ARGEXCLUDED=''
counter=1
for _d in "${EXCLUDEDDIRS[@]}"; do
ARGEXCLUDED+=" -name $_d"
[ $counter -lt ${#EXCLUDEDDIRS[@]} ] && ARGEXCLUDED+=" -o";
let counter+=1
done
# go to the clone directory
cd "$HERE"
# test if we are in a GIT clone
if [ ! -d .git ]
then
export INSTALLMODE='hard-copies'
echo "!! > current directory doesn't seem to be a clone of a GIT repository ... Installation mode has been automatically set on 'hard-copies'."
read -p "do you want to continue ? [N/y] " _resp
_resp="${_resp:-N}"
if [ "${_resp}" != 'y' ] && [ "${_resp}" != 'Y' ]
then
echo "_ abort"
exit 0
fi
fi
# how-to info
if ! $_FORCED
then
echo
echo "The installation process will begin ... Any prompt is waiting for an answer to validate the action or file:"
echo " - default answer is ALWAYS 'NO'"
echo " - you HAVE to type 'y' to validate a prompt"
echo
fi
# update of submodules
if [ -d .git ] && [ -f .gitmodules ]
then
$_VERBOSE && echo "> updating git sub-modules ..."
git submodule init
git submodule sync --recursive
git submodule update --recursive
fi
# installation of dotfiles
if [ "$INSTALLTYPE" == 'dotfiles' ]||[ "$INSTALLTYPE" == 'home' ]||[ "$INSTALLTYPE" == 'all' ]
then
# symlink all but models
$_VERBOSE && echo "> installing dotfiles in '${INSTALLDIR}/' ..."
for f in $(find "$(cd ${HERE}/home; pwd)" -maxdepth 1 -name "$MODELSGLOB" -prune -o -type f ${FINDEXECTYPE} echo {} \;)
do
if [ "${INSTALLMODE}" == 'hard-copies' ]
then
cp ${CPOPTS} ${f} "${INSTALLDIR}/"
else
ln ${LNOPTS} ${f} "${INSTALLDIR}/"
fi
done
# copy and edit models
$_VERBOSE && echo "> installing dotfiles models in '${INSTALLDIR}/': first choose models to install then edit them ..."
for f in $(find "$(cd ${HERE}/home; pwd)" -maxdepth 1 -name "$MODELSGLOB" -prune -type f ${FINDEXECTYPE} echo {} \;)
do
ofn=`basename $f`
tfn="${ofn/.model}"
cp ${CPOPTS} ${f} "${INSTALLDIR}/${tfn}" && vim "${INSTALLDIR}/${tfn}";
done
# create the $BASHRCDIR dir
$_VERBOSE && echo "> creating directory '${INSTALLDIR}/${BASHRCDIR}/' ..."
mkdir -p "${INSTALLDIR}/${BASHRCDIR}"
# symlink all but models
$_VERBOSE && echo "> installing shell scripts dotfiles in '${INSTALLDIR}/${BASHRCDIR}/' ..."
for f in $(find "$(cd ${HERE}/home/${BASHRCDIR}; pwd)" -maxdepth 1 -name "$MODELSGLOB" -prune -o -type f ${FINDEXECTYPE} echo {} \;)
do
if [ "${INSTALLMODE}" == 'hard-copies' ]
then
cp ${CPOPTS} ${f} "${INSTALLDIR}/${BASHRCDIR}/"
else
ln ${LNOPTS} ${f} "${INSTALLDIR}/${BASHRCDIR}/"
fi
done
$_VERBOSE && echo "> installing shell scripts dotfiles alternatives in '${INSTALLDIR}/${BASHRCDIR}/' ..."
for f in $(find "$(cd ${HERE}/home/alt; pwd)" -maxdepth 1 -name "$MODELSGLOB" -prune -o -name "*.sh" -prune -type f ${FINDEXECTYPE} echo {} \;)
do
if [ "${INSTALLMODE}" == 'hard-copies' ]
then
cp ${CPOPTS} ${f} "${INSTALLDIR}/${BASHRCDIR}/"
else
ln ${LNOPTS} ${f} "${INSTALLDIR}/${BASHRCDIR}/"
fi
done
# copy and edit models
$_VERBOSE && echo "> installing shell scripts models in '${INSTALLDIR}/${BASHRCDIR}/': first choose models to install then edit them ..."
for f in $(find "$(cd ${HERE}/home/${BASHRCDIR}; pwd)" -maxdepth 1 -name "$MODELSGLOB" -prune -type f ${FINDEXECTYPE} echo {} \;)
do
ofn=`basename $f`
tfn="${ofn/.model}"
cp ${CPOPTS} ${f} "${INSTALLDIR}/${BASHRCDIR}/${tfn}" && vim "${INSTALLDIR}/${BASHRCDIR}/${tfn}";
done
$_VERBOSE && echo "> installing shell scripts models alternatives in '${INSTALLDIR}/${BASHRCDIR}/': first choose models to install then edit them ..."
for f in $(find "$(cd ${HERE}/home/alt; pwd)" -maxdepth 1 -name "*.sh.model" -prune -type f ${FINDEXECTYPE} echo {} \;)
do
ofn=`basename $f`
tfn="${ofn/.model}"
cp ${CPOPTS} ${f} "${INSTALLDIR}/${BASHRCDIR}/${tfn}" && vim "${INSTALLDIR}/${BASHRCDIR}/${tfn}";
done
$_VERBOSE && echo
echo "Dotfiles successfully installed."
echo "You may want to add alternatives you can find at '$(cd home/alt; pwd)' ..."
$_VERBOSE && echo
fi
# installation of binaries
if [ "$INSTALLTYPE" == 'bin' ]||[ "$INSTALLTYPE" == 'binaries' ]||[ "$INSTALLTYPE" == 'all' ]
then
$_VERBOSE && echo "> installing binaries in '${INSTALLDIR}/bin/' ..."
for f in $(find "$(cd ${HERE}/bin; pwd)" -maxdepth 1 \( -name "bin" -o -name "$MODELSGLOB" -prune \) -o ${FINDEXECTYPE} echo {} \;)
do
[ ! -d "${INSTALLDIR}/bin" ] && mkdir -p "${INSTALLDIR}/bin";
if [ "$INSTALLMODE" == 'hard-copies' ]
then
cp ${CPOPTS} ${f} "${INSTALLDIR}/bin/"
else
ln ${LNOPTS} ${f} "${INSTALLDIR}/bin/"
fi
done
$_VERBOSE && echo
echo "Binaries successfully installed."
$_VERBOSE && echo
fi
# installation of sub-directories
if [ "$INSTALLTYPE" == 'subdirs' ]||[ "$INSTALLTYPE" == 'subdirs' ]||[ "$INSTALLTYPE" == 'all' ]
then
$_VERBOSE && echo "> linking sub-directories in '${INSTALLDIR}/%s' ..."
for d in $(find "$(pwd)" -mindepth 1 -maxdepth 1 \( $ARGEXCLUDED -prune \) -o -type d ${FINDEXECTYPE} echo {} \;)
do
odn="$(basename "$d")"
installer="${d}/${AUTOINSTALLER}"
if [ ! -f "$installer" ]
then
$_VERBOSE && echo "> linking '${odn}' directory in '${INSTALLDIR}/${odn}/' ..."
if [ "$INSTALLMODE" == 'hard-copies' ]
then
cp ${CPOPTS} -r "${HERE}/${odn}" "${INSTALLDIR}/"
else
ln ${LNOPTS} "${HERE}/${odn}" "${INSTALLDIR}/"
fi
else
$_VERBOSE && echo "> calling internal installer in '${odn}' directory ..."
mkdir -p "${INSTALLDIR}/${odn}" && cd "${odn}" && ${installer} "${INSTALLDIR}/${odn}";
fi
done
fi
echo "## finished : well done bro' :)"
exit 0
# vim: autoindent tabstop=4 shiftwidth=4 expandtab softtabstop=4 filetype=sh