Debian Codegraph @

bin/rcup.in

raw ◀ ▶
1 Debian patch touch this file
•1 #!/bin/sh
2
3 : ${RCM_LIB:=$(dirname "$0")/../share/rcm}
4 . "$RCM_LIB/rcm.sh"
5
6 print_ln_v() {
7 $PRINT "handle_file_ln \"$1\" \"$2\""
8 }
9
10 print_cp_v() {
11 $PRINT "handle_file_cp \"$1\" \"$2\""
12 }
13
14 print_rm_v() {
15 :
16 }
17
18 link_or_copy() {
19 $DEBUG "link_or_copy $1"
20 local sigil="$1"
21
22 if [ "x$sigil" = "xX" ]; then
23 echo "$CP"
24 else
25 echo "$LN"
26 fi
27 }
28
29 print_generated_preface() {
30 cat <<PREFACE
31 #!/bin/sh
32 #
33 # Usage:
34 #
35 # sh install.sh
36 #
37 # Environment variables: VERBOSE, CP, LN, MKDIR, RM, DIRNAME.
38 #
39 # env VERBOSE=1 sh install.sh
40 #
41 # DO NOT EDIT THIS FILE
42 #
43 # This file is generated by rcm(7) as:
44 #
45 # rcup $@
46 #
47 # To update it, re-run the above command.
48 #
49 : \${VERBOSE:=0}
50 : \${CP:=/bin/cp}
51 : \${LN:=/bin/ln}
52 : \${MKDIR:=/bin/mkdir}
53 : \${RM:=/bin/rm}
54 : \${DIRNAME:=/usr/bin/dirname}
55 verbose() {
56 if [ "\$VERBOSE" -gt 0 ]; then
57 echo "\$@"
58 fi
59 }
60 handle_file_cp() {
61 if [ -e "\$2" ]; then
62 printf "%s " "overwrite \$2? [yN]"
63 read overwrite
64 case "\$overwrite" in
65 y)
66 \$RM -rf "\$2"
67 ;;
68 *)
69 echo "skipping \$2"
70 return
71 ;;
72 esac
73 fi
74 verbose "'\$1' -> '\$2'"
75 \$MKDIR -p "\$(\$DIRNAME "\$2")"
76 \$CP -R "\$1" "\$2"
77 }
78 handle_file_ln() {
79 if [ -e "\$2" ]; then
80 printf "%s " "overwrite \$2? [yN]"
81 read overwrite
82 case "\$overwrite" in
83 y)
84 \$RM -rf "\$2"
85 ;;
86 *)
87 echo "skipping \$2"
88 return
89 ;;
90 esac
91 fi
92 verbose "'\$1' -> '\$2'"
93 \$MKDIR -p "\$(\$DIRNAME "\$2")"
94 \$LN -sf "\$1" "\$2"
95 }
96 PREFACE
97 }
98
99 link_file() {
100 local src="$1"
101 local dest="$2"
102 local sigil="$3"
103
104 if [ -h "$dest" ]; then
105 $RM -f "$dest"
106 fi
107
108 action="$(link_or_copy "$sigil")"
109 $DEBUG "$action $src $dest"
110 $action "$src" "$dest"
111 }
112
113 replace_file() {
114 local src="$1"
115 local dest="$2"
116 local sigil="$3"
117
118 $DEBUG replace_file "$1" "$2" $3
119
120 $RM -rf "$dest"
121 link_file "$src" "$dest" "$sigil"
122 }
123
124 is_identical() {
125 diff -c "$1" "$2" > /dev/null 2>&1
126 }
127
128 is_linked() {
129 local src="$1"
130 local dest="$2"
131
132 if [ -h "$dest" ]; then
133 local link_dest=$(readlink $dest)
134 [ "$link_dest" = "$src" ]
135 else
136 return 1
137 fi
138 }
139
140 handle_dir() {
141 local dest="$1"
142
143 $DEBUG "handle_dir $dest"
144
145 $MKDIR -p "$(dirname "$dest")"
146 }
147
148 handle_file() {
149 local generate="$1"
150 local src="$2"
151 local dest="$3"
152 local sigil="$4"
153
154 $DEBUG "handle_file $1 $2 $3 $4"
155
156 if [ "$generate" -ne 1 -a -e "$dest" ]; then
157 if is_identical "$src" "$dest"; then
158 if [ "x$sigil" != "xX" ] && ! is_linked "$src" "$dest"; then
159 $VERBOSE "replacing identical but unlinked $dest"
160 replace_file "$src" "$dest" "$sigil"
161 else
162 $VERBOSE "identical $dest"
163 fi
164 elif [ $REPLACE_ALL -eq 1 ]; then
165 replace_file "$src" "$dest" "$sigil"
166 else
167 $PROMPT "overwrite ${dest}? [ynaq]"
168 read overwrite
169 case "$overwrite" in
170 a) REPLACE_ALL=1
171 replace_file "$src" "$dest" "$sigil"
172 ;;
173 y) replace_file "$src" "$dest" "$sigil" ;;
174 q) exit 1 ;;
175 *) $VERBOSE "skipping $dest" ;;
176 esac
177 fi
178 else
179 link_file "$src" "$dest" "$sigil"
180 fi
181 }
182
183 show_help() {
184 local exit_code=${1:-0}
185
186 $PRINT "Usage: rcup [-CfhiKkqVv] [-B HOSTNAME] [-d DOT_DIR] [-I EXCL_PAT] [-S EXCL_PAT] [-s EXCL_PAT] [-t TAG] [-U EXCL_PAT] [-u EXCL_PAT] [-x EXCL_PAT]"
187 $PRINT "see rcup(1) and rcm(7) for more details"
188
189 exit $exit_code
190 }
191
192 handle_command_line() {
193 local arg_tags=
194 local verbosity=0
195 local version=0
196 local run_hooks=1
197 local dotfiles_dirs=
198 local files=
199 local excludes=
200 local includes=
201 local always_copy=0
202 local symlink_dirs=
203 local never_symlink_dirs=
204 local hostname=
205 local generate=0
206 local args="$*"
207 local undotted=
208 local never_undotted=
209 REPLACE_ALL=0
210 GENERATE=
211
212 while getopts :CVqvfghikKI:x:S:s:U:u:t:d:B: opt; do
213 case "$opt" in
214 B) hostname="$OPTARG" ;;
215 C) always_copy=1 ;;
216 d) dotfiles_dirs="$(append_variable "$dotfiles_dirs" "$OPTARG")" ;;
217 f) REPLACE_ALL=1 ;;
218 g) generate=1 ;;
219 h) show_help ;;
220 i) REPLACE_ALL=0 ;;
221 I) includes="$(append_variable "$includes" "$OPTARG")" ;;
222 k) run_hooks=1 ;;
223 K) run_hooks=0 ;;
224 q) verbosity=$(($verbosity - 1)) ;;
225 t) arg_tags="$(append_variable "$arg_tags" "$OPTARG")" ;;
226 S) symlink_dirs="$(append_variable "$symlink_dirs" "$OPTARG")" ;;
227 s) never_symlink_dirs="$(append_variable "$never_symlink_dirs" "$OPTARG")";;
228 U) undotted="$(append_variable "$undotted" "$OPTARG")" ;;
229 u) never_undotted="$(append_variable "$never_undotted" "$OPTARG")" ;;
230 v) verbosity=$(($verbosity + 1)) ;;
231 V) version=1 ;;
232 x) excludes="$(append_variable "$excludes" "$OPTARG")" ;;
233 ?) show_help 64 ;;
234 esac
235 done
236 shift $(($OPTIND-1))
237
238 LN="ln_v"
239 CP="cp_v"
240 RM="rm_v"
241 if [ $always_copy -eq 1 ]; then
242 LN="$CP"
243 fi
244 if [ "$generate" -eq 1 ]; then
245 LN="print_ln_v"
246 CP="print_cp_v"
247 RM="print_rm_v"
248 print_generated_preface $args
249 fi
250 GENERATE="$generate"
251
252 handle_common_flags rcup $version $verbosity
253 hostname="$(determine_hostname "$hostname")"
254
255 tags="${arg_tags:-$TAGS}"
256 DOTFILES_DIRS="${dotfiles_dirs:-$DOTFILES_DIRS}"
257 RUN_HOOKS=$run_hooks
258 files=
259 for file; do
260 files="$files $(encode "$file")"
261 done
262
263 for tag in $tags; do
264 LS_ARGS="$LS_ARGS -t \"$tag\""
265 done
266 for dotfiles_dir in "$DOTFILES_DIRS"; do
267 LS_ARGS="$LS_ARGS -d \"$dotfiles_dir\""
268 done
269 for exclude in "$excludes"; do
270 LS_ARGS="$LS_ARGS -x \"$exclude\""
271 done
272 for include in "$includes"; do
273 LS_ARGS="$LS_ARGS -I \"$include\""
274 done
275 for symlink_dir in "$symlink_dirs"; do
276 LS_ARGS="$LS_ARGS -S \"$symlink_dir\""
277 done
278 for never_symlink_dir in "$never_symlink_dirs"; do
279 LS_ARGS="$LS_ARGS -s \"$never_symlink_dir\""
280 done
281 for undot in "$undotted"; do
282 LS_ARGS="$LS_ARGS -U \"$undot\""
283 done
284 for never_undot in "$never_undotted"; do
285 LS_ARGS="$LS_ARGS -u \"$never_undot\""
286 done
287
288 LS_ARGS="$LS_ARGS -B \"$hostname\" $files"
289
290 $DEBUG "LS_ARGS: $LS_ARGS"
291 }
292
293 LS_ARGS=-F
294
295 handle_command_line "$@"
296 : ${DOTFILES_DIRS:=$DOTFILES_DIRS $DEFAULT_DOTFILES_DIR}
297
298 run_hooks pre up
299 pre_up_ret=$?
300
301 if [ "$pre_up_ret" -ne 0 ]; then
302 exit "$pre_up_ret"
303 fi
304
305 dests_and_srcs="$(eval "lsrc $LS_ARGS")"
306
307 saved_ifs="$IFS"
308 IFS='
309 '
310 for dest_and_src in $dests_and_srcs; do
311 IFS=:
312 set -- $dest_and_src
313 IFS="$saved_ifs"
314 dest="$1"
315 src="$2"
316 sigil="$3"
317
318 if is_nested "$dest"; then
319 handle_dir "$dest"
320 fi
321
322 handle_file "$GENERATE" "$src" "$dest" "$sigil"
323 done
324
325 IFS="$saved_ifs"
326 run_hooks post up