summaryrefslogtreecommitdiff
path: root/bin/rcup.in
blob: 910e53451a0a6a7b13bcad99ad747f79040f371e (plain)
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
#!@SHELL@

: ${RCM_LIB:=$(dirname "$0")/../share/rcm}
. "$RCM_LIB/rcm.sh"

print_ln_v() {
  $PRINT "handle_file_ln \"$1\" \"$2\""
}

print_cp_v() {
  $PRINT "handle_file_cp \"$1\" \"$2\""
}

print_rm_v() {
  :
}

link_or_copy() {
  $DEBUG "link_or_copy $1"
  local sigil="$1"

  if [ "x$sigil" = "xX" ]; then
    echo "$CP"
  else
    echo "$LN"
  fi
}

print_generated_preface() {
  cat <<PREFACE
#!/bin/sh
#
# Usage:
#
#    sh install.sh
#
# Environment variables: VERBOSE, CP, LN, MKDIR, RM, DIRNAME.
#
#    env VERBOSE=1 sh install.sh
#
# DO NOT EDIT THIS FILE
# 
# This file is generated by rcm(7) as:
#
#   rcup $@
#
# To update it, re-run the above command.
#
: \${VERBOSE:=0}
: \${CP:=/bin/cp}
: \${LN:=/bin/ln}
: \${MKDIR:=/bin/mkdir}
: \${RM:=/bin/rm}
: \${DIRNAME:=/usr/bin/dirname}
verbose() {
  if [ "\$VERBOSE" -gt 0 ]; then
    echo "\$@"
  fi
}
handle_file_cp() {
  if [ -e "\$2" ]; then
    printf "%s " "overwrite \$2? [yN]"
    read overwrite
    case "\$overwrite" in
      y)
        \$RM -rf "\$2"
        ;;
      *)
        echo "skipping \$2"
        return
        ;;
    esac
  fi
  verbose "'\$1' -> '\$2'"
  \$MKDIR -p "\$(\$DIRNAME "\$2")"
  \$CP -R "\$1" "\$2"
}
handle_file_ln() {
  if [ -e "\$2" ]; then
    printf "%s " "overwrite \$2? [yN]"
    read overwrite
    case "\$overwrite" in
      y)
        \$RM -rf "\$2"
        ;;
      *)
        echo "skipping \$2"
        return
        ;;
    esac
  fi
  verbose "'\$1' -> '\$2'"
  \$MKDIR -p "\$(\$DIRNAME "\$2")"
  \$LN -sf "\$1" "\$2"
}
PREFACE
}

link_file() {
  local src="$1"
  local dest="$2"
  local sigil="$3"

  if [ -h "$dest" ]; then
    $RM -f "$dest"
  fi

  action="$(link_or_copy "$sigil")"
  $DEBUG "$action $src $dest"
  $action "$src" "$dest"
}

replace_file() {
  local src="$1"
  local dest="$2"
  local sigil="$3"

  $DEBUG replace_file "$1" "$2" $3

  $RM -rf "$dest"
  link_file "$src" "$dest" "$sigil"
}

is_nested() {
  echo "$1" | sed "s:$DEST_DIR/::" | grep '/' >/dev/null
}

is_identical() {
  diff -c "$1" "$2" > /dev/null 2>&1
}

is_linked() {
  local src="$1"
  local dest="$2"

  if [ -h "$dest" ]; then
    local link_dest=$(readlink $dest)
    [ "$link_dest" = "$src" ]
  else
    return 1
  fi  
}

handle_dir() {
  local dest="$1"

  $DEBUG "handle_dir $dest"

  $MKDIR -p "$(dirname "$dest")"
}

handle_file() {
  local generate="$1"
  local src="$2"
  local dest="$3"
  local sigil="$4"

  $DEBUG "handle_file $1 $2 $3 $4"

  if [ "$generate" -ne 1 -a -e "$dest" ]; then
    if is_identical "$src" "$dest"; then
      if [ "x$sigil" != "xX" ] && ! is_linked "$src" "$dest"; then
        $VERBOSE "replacing identical but unlinked $dest"
        replace_file "$src" "$dest" "$sigil"
      else
        $VERBOSE "identical $dest"
      fi
    elif [ $REPLACE_ALL -eq 1 ]; then
      replace_file "$src" "$dest" "$sigil"
    else
      $PROMPT "overwrite ${dest}? [ynaq]"
      read overwrite
      case "$overwrite" in
        a) REPLACE_ALL=1
           replace_file "$src" "$dest" "$sigil"
           ;;
        y) replace_file "$src" "$dest" "$sigil" ;;
        q) exit 1 ;;
        *) $VERBOSE "skipping $dest" ;;
      esac
    fi
  else
    link_file "$src" "$dest" "$sigil"
  fi
}

show_help() {
  local exit_code=${1:-0}

  $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]"
  $PRINT "see rcup(1) and rcm(7) for more details"

  exit $exit_code
}

handle_command_line() {
  local arg_tags=
  local verbosity=0
  local version=0
  local run_hooks=1
  local dotfiles_dirs=
  local files=
  local excludes=
  local includes=
  local always_copy=0
  local symlink_dirs=
  local never_symlink_dirs=
  local hostname=
  local generate=0
  local args="$*"
  local undotted=
  local never_undotted=
  REPLACE_ALL=0
  GENERATE=

  while getopts :CVqvfghikKI:x:S:s:U:u:t:d:B: opt; do
    case "$opt" in
      B) hostname="$OPTARG" ;;
      C) always_copy=1 ;;
      d) dotfiles_dirs="$dotfiles_dirs $OPTARG" ;;
      f) REPLACE_ALL=1 ;;
      g) generate=1 ;;
      h) show_help ;;
      i) REPLACE_ALL=0 ;;
      I) includes="$includes $OPTARG" ;;
      k) run_hooks=1 ;;
      K) run_hooks=0 ;;
      q) verbosity=$(($verbosity - 1)) ;;
      t) arg_tags="$arg_tags $OPTARG" ;;
      S) symlink_dirs="$symlink_dirs $OPTARG";;
      s) never_symlink_dirs="$never_symlink_dirs $OPTARG";;
      U) undotted="$undotted $OPTARG";;
      u) never_undotted="$never_undotted $OPTARG";;
      v) verbosity=$(($verbosity + 1)) ;;
      V) version=1 ;;
      x) excludes="$excludes $OPTARG" ;;
      ?) show_help 64 ;;
    esac
  done
  shift $(($OPTIND-1))

  LN="ln_v"
  CP="cp_v"
  RM="rm_v"
  if [ $always_copy -eq 1 ]; then
    LN="$CP"
  fi
  if [ "$generate" -eq 1 ]; then
    LN="print_ln_v"
    CP="print_cp_v"
    RM="print_rm_v"
    print_generated_preface $args
  fi
  GENERATE="$generate"

  handle_common_flags rcup $version $verbosity
  hostname="$(determine_hostname "$hostname")"

  tags="${arg_tags:-$TAGS}"
  DOTFILES_DIRS="${dotfiles_dirs:-$DOTFILES_DIRS}"
  RUN_HOOKS=$run_hooks
  files=
  for file; do
    files="$files $(encode "$file")"
  done

  for tag in $tags; do
    LS_ARGS="$LS_ARGS -t $tag"
  done
  for dotfiles_dir in $DOTFILES_DIRS; do
    LS_ARGS="$LS_ARGS -d $dotfiles_dir"
  done
  for exclude in $excludes; do
    LS_ARGS="$LS_ARGS -x $exclude"
  done
  for include in $includes; do
    LS_ARGS="$LS_ARGS -I $include"
  done
  for symlink_dir in $symlink_dirs; do
    LS_ARGS="$LS_ARGS -S $symlink_dir"
  done
  for never_symlink_dir in $never_symlink_dirs; do
    LS_ARGS="$LS_ARGS -s $never_symlink_dir"
  done
  for undot in $undotted; do
    LS_ARGS="$LS_ARGS -U $undot"
  done
  for never_undot in $never_undotted; do
    LS_ARGS="$LS_ARGS -u $never_undot"
  done

  LS_ARGS="$LS_ARGS -B $hostname $files"

  $DEBUG "LS_ARGS: $LS_ARGS"
}

LS_ARGS=-F

handle_command_line "$@"
: ${DOTFILES_DIRS:=$DOTFILES_DIRS $DEFAULT_DOTFILES_DIR}

run_hooks pre up

dests_and_srcs="$(lsrc $LS_ARGS)"

saved_ifs="$IFS"
IFS='
'
for dest_and_src in $dests_and_srcs; do
  IFS=:
  set -- $dest_and_src
  IFS="$saved_ifs"
  dest="$1"
  src="$2"
  sigil="$3"

  if is_nested "$dest"; then
    handle_dir "$dest"
  fi

  handle_file "$GENERATE" "$src" "$dest" "$sigil"
done

IFS="$saved_ifs"
run_hooks post up