-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsymlink
executable file
·282 lines (232 loc) · 6.15 KB
/
symlink
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
#!/bin/bash
# Program: symlink
# Author: Harenome RANAIVOARIVONY RAZANAJATO
# Version: 1.3
# Date: November 16th 2017
# License: WTFPL v2
# This program is free software. It comes WITHOUT ANY WARRANTY, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# http://wtfpl.net/COPYING for more details.
################################################################################
# Global variables
################################################################################
PWD=`pwd`
YES_STRINGS="y Y yes YES"
BACKUP_DIR=$PWD/backups
CONFIG_DIR=$HOME
## Colors
COLOR_NORMAL="\x1b[0m"
COLOR_BOLD="\x1b[1m"
COLOR_DIM="\x1b[2m"
COLOR_UNDERLINED="\x1b[4m"
COLOR_INVERTED="\x1b[7m"
COLOR_SECTION="$COLOR_BOLD$COLOR_INVERTED"
COLOR_ERROR="$COLOR_BOLD\x1b[38;5;255m\x1b[48;5;196m"
COLOR_WARNING="$COLOR_BOLD\x1b[38;5;255m\x1b[48;5;208m"
COLOR_ENABLED="$COLOR_BOLD\x1b[32m"
COLOR_DISABLED="$COLOR_BOLD\x1b[31m"
## Variables for version and help printing
PROGRAM_NAME="symlink"
DATE="November 16th 2017"
VERSION="1.3"
LICENCE="This program is free software. It comes WITHOUT ANY WARRANTY, to
the extent permitted by applicable law. You can redistribute it
and/or modify it under the terms of the Do What The Fuck You Want
To Public License, Version 2, as published by Sam Hocevar. See
http://www.wtfpl.net for more details."
AUTHORS="Harenome Ranaivoarivony Razanajato"
VERSION_STRING="symlinks, version $VERSION ($DATE)
$LICENCE
Written by $AUTHORS."
HELP_STRING="\e[1m\e[7m NAME \e[0m
\t$PROGRAM_NAME — Link *.symlink dotfiles
\e[1m\e[7m SYNOPSIS \e[0m
\t\e[1m$0\e[0m [OPTIONS]...
\e[1m\e[7m OPTIONS \e[0m
\t\e[1m--no-backup\e[0m
\t\tdisable backups
\t\e[1m-f\e[0m,\e[1m --force-overwrite\e[0m
\t\tforce overwriting
\t\e[1m--verbose\e[0m
\t\tenable verbose mode
\t\e[1m-h\e[0m,\e[1m --help\e[0m
\t\tprint this help
\t\e[1m-v\e[0m,\e[1m --version\e[0m
\t\tprint the version
"
DO_BACKUPS=true
FORCE_OVERWRITE=false
VERBOSE=false
WRONG_ARGUMENT=false
################################################################################
# Functions
################################################################################
message ()
{
echo -en "$1" 1>&2
}
verbose ()
{
"$VERBOSE" && message "$*"
}
verbose_command ()
{
"$VERBOSE" || return
message "$COLOR_DIM❯ $COLOR_BOLD$1$COLOR_NORMAL"
shift
[[ $# -ne 0 ]] && message " $*\n" || message "\n"
}
error ()
{
message "$COLOR_ERROR ERROR $COLOR_NORMAL $*"
}
warning ()
{
message "$COLOR_WARNING WARNING $COLOR_NORMAL $*"
}
print_version ()
{
echo -e "$VERSION_STRING" 1>&2
}
print_help ()
{
echo -e "$HELP_STRING"
}
print_option ()
{
if $1; then
message "\t$COLOR_ENABLED✔$COLOR_NORMAL $2\n"
else
message "\t$COLOR_DISABLED✗$COLOR_NORMAL $2\n"
fi
}
print_var ()
{
message "\t$COLOR_BOLD●$COLOR_NORMAL $1: $2\n"
}
wrong_option ()
{
WRONG_OPTIONS=( ${WRONG_OPTIONS[@]} "$1" )
WRONG_ARGUMENT=true
}
backup ()
{
verbose_command "mkdir -p" "$BACKUP_DIR"
mkdir -p "$BACKUP_DIR"
verbose_command "cp -r" "$1" "$BACKUP_DIR/"
cp -r "$1" "$BACKUP_DIR/"
}
remove ()
{
"$DO_BACKUPS" && backup "$1"
verbose_command "rm -rf" "$1"
rm -rf "$1"
}
answer_is_yes ()
{
for YES in $YES_STRINGS; do
if [ "x$1" = "x$YES" ]; then
return 0;
fi
done
return 1;
}
create_link ()
{
"$LINK" || return
verbose_command "ln -sf" "$1" "$2"
ln -sf "$1" "$2"
}
ask ()
{
message "$1 [y/${COLOR_BOLD}N${COLOR_NORMAL}]\n"
message "$COLOR_BOLD>$COLOR_NORMAL "
read ANSWER
if answer_is_yes "$ANSWER"; then
return 0
else
return 1
fi
}
overwrite ()
{
local question="$COLOR_UNDERLINED$1$COLOR_NORMAL already exists. Overwrite?"
if "$FORCE_OVERWRITE" || ask "$question"; then
remove "$1"
LINK=true
fi
}
find_symlinks ()
{
find . -name "*.symlink" | sed 's/^\.\///;s/\.symlink$//' | sort
}
################################################################################
# Main
################################################################################
# Process command line arguments, if any.
if [ $# -ne 0 ]; then
# getopt and getopts are a bit painful
for ARG in $*; do
if [[ ${ARG:0:1} = "-" ]] && [[ ${ARG:0:2} != "--" ]]; then
case $ARG in
"-f" ) FORCE_OVERWRITE=true;;
"-v" ) print_version; exit 0;;
"-h" ) print_help; exit 0;;
* ) wrong_option "$ARG";;
esac
elif [[ ${ARG:0:2} = "--" ]]; then
case $ARG in
"--verbose" ) VERBOSE=true;;
"--no-backup" ) DO_BACKUPS=false;;
"--force-overwrite" ) FORCE_OVERWRITE=true;;
"--version" ) print_version; exit 0;;
"--help" ) print_help; exit 0;;
* ) wrong_option "$ARG";;
esac
fi
done
fi
# Print version, options, etc if the verbose is enabled.
if "$VERBOSE"; then
print_version
printf "\n" 1>&2
message "$COLOR_SECTION Options $COLOR_NORMAL\n\n"
print_option $VERBOSE "verbose"
print_option $DO_BACKUPS "backups"
print_option $FORCE_OVERWRITE "force-overwrite"
print_var "BACKUP_DIR" "$BACKUP_DIR"
print_var "CONFIG_DIR" "$CONFIG_DIR"
printf "\n" 1>&2
fi
# Find *.symlink files in the current directory.
SYMLINKS=$(find_symlinks)
# Various warnings and errors.
[[ -z ${SYMLINKS[@]} ]] && warning "no symlinks found\n"
[[ "$FORCE_OVERWRITE" = "true" ]] && [[ "$DO_BACKUPS" = "false" ]] &&
warning "backups disabled and force-overwrite enabled\n"
for OPTION in ${WRONG_OPTIONS[@]}; do
error "wrong option \"$OPTION\"\n"
done
"$WRONG_ARGUMENT" && exit 64
# Linking.
"$VERBOSE" && message "\n$COLOR_SECTION Dotfiles linking $COLOR_NORMAL\n\n"
for FILE in ${SYMLINKS[@]}; do
DIRNAME=`dirname $FILE`
SOURCE="$PWD/$FILE.symlink"
DESTINATION="$HOME/$FILE"
LINK=true
"$VERBOSE" && message "$COLOR_BOLD$FILE$COLOR_NORMAL\n"
if [ -L "$DESTINATION" ] && [ `readlink "$DESTINATION"` = "$SOURCE" ]; then
verbose_command "ignoring" "$FILE"
LINK=false
elif [ -e "$DESTINATION" ]; then
LINK=false
overwrite "$DESTINATION"
elif [ "x$DIRNAME" != "x." ]; then
mkdir -p "$HOME/$DIRNAME"
fi
"$LINK" && create_link "$SOURCE" "$DESTINATION"
done
exit 0