-
Notifications
You must be signed in to change notification settings - Fork 0
/
install
executable file
·131 lines (114 loc) · 2.55 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
#!/usr/bin/env bash
#
# dotfiles/install
#
# Usage:
# cd ~/.dotfiles
# ./install -n to see what would happen
#
#
#
MY_HOME=${HOME}
#
usage() {
cat <<EOF
usage $0 [options]
OPTIONS:
-h Show this message
-n Show what this script would do (but don't do it).
EOF
}
#
# List of exluded files
excluded=(README.md LICENSE `basename $0` configuration)
## Parse command line switches
while getopts "hn" option
do
case $option in
h)
usage
exit
;;
n)
DRY_RUN=1
;;
?)
usage
exit 1
;;
esac
done
shift $(($OPTIND -1))
args=${*} # remaining args
# ----------------------------------------------------------- utilities
info_indent=" "
info () {
printf "%s\033[00;34m..\033[0m %s\n" "${info_indent}" "$1"
}
task () {
printf "\033[00;34m * %-12s \033[0m: " $1
}
success () {
printf "[\033[00;32mOK\033[0m] $1\n"
}
warn () {
printf "[\033[0;31mWARN\033[0m] $1\n"
}
fail () {
printf "\033[2K [\033[0;31mFAIL\033[0m] $1\n"
echo ''
exit
}
# ----------------------------------------------------------- backup
# backup stuff
#
backup_dir=$MY_HOME/.dotfiles_backup
timestamp=$(date '+%Y%m%d_%H%M%S')
if ! [[ -x "${backup_dir}" ]]; then
echo "-> Creating a backup directory for existing dotfiles."
mkdir -p ${backup_dir}
fi
backup() {
local old_file=$1
local basename=$(basename $old_file)
local backup_file="${backup_dir}/${basename#.}-${timestamp}"
#
info "Looking for backups"
local -a bfiles=($(find ${backup_dir} -name "${basename#.}*"))
local n=${#bfiles[@]}
if [[ $n -gt 0 ]]; then
local last_backup=${bfiles[((${n}-1))]}
if $(diff "${old_file}" "${last_backup}" > /dev/null); then
info "Matches '${last_backup}'"
rm ${old_file} && info "=> Removed current file."
return 0
fi
fi
mv $old_file $backup_file && \
info "Backed up to ${backup_file}."
}
# ------------------------------------------------------------ main
# main
#
files=(`dirname $0`/*)
for file in ${files[@]}; do
f_basename=$(basename $file)
if [[ "${excluded[@]}" =~ "${f_basename}" ]]; then
continue;
fi
target="${MY_HOME}/.${f_basename}"
# shortcut for `basename`
task "${target##*/}"
if [ -e $target ]; then
if $(diff "${file}" "${target}" > /dev/null) ; then
success "up-to-date"
continue
else
warn "Found real file '${file}'!"
backup $target
printf "$info_indent"
fi
fi
ln -s $file $target && \
success "Installed link to ${file}."
done