| •1 |
#!/bin/sh |
| 2 |
|
| 3 |
: ${RCM_LIB:=$(dirname "$0")/../share/rcm} |
| 4 |
. "$RCM_LIB/rcm.sh" |
| 5 |
|
| 6 |
pushdir() { |
| 7 |
DIR_STACK="$DIR_STACK:$PWD/$1" |
| 8 |
$DEBUG "cd'ing to $1 from $PWD with stack $DIR_STACK" |
| 9 |
cd -- "$1" |
| 10 |
} |
| 11 |
|
| 12 |
popdir() { |
| 13 |
current="$(echo "$DIR_STACK" | sed -e 's/.*://g')" |
| 14 |
prior="$(echo "$DIR_STACK" | sed -e "s|:$current$||" | sed -e 's/.*://g')" |
| 15 |
DIR_STACK="$(echo "$DIR_STACK" | sed -e 's/:[^:]*$//')" |
| 16 |
$DEBUG "cd'ing to $prior from $PWD with stack $DIR_STACK" |
| 17 |
cd -- "$prior" |
| 18 |
} |
| 19 |
|
| 20 |
build_path() { |
| 21 |
local dest="$1" |
| 22 |
local file="$2" |
| 23 |
local dotted=$3 |
| 24 |
|
| 25 |
if [ $dotted -eq 1 ]; then |
| 26 |
echo "$dest/$file" |
| 27 |
else |
| 28 |
echo "$dest/.$file" |
| 29 |
fi |
| 30 |
} |
| 31 |
|
| 32 |
file_join() { |
| 33 |
local result= |
| 34 |
|
| 35 |
for file; do |
| 36 |
if [ "x$file" != "x." ]; then |
| 37 |
if [ "x$result" = "x" ]; then |
| 38 |
result="$file" |
| 39 |
else |
| 40 |
result="$result/$file" |
| 41 |
fi |
| 42 |
fi |
| 43 |
done |
| 44 |
|
| 45 |
echo "$result" |
| 46 |
} |
| 47 |
|
| 48 |
show_dir() { |
| 49 |
local dir="$1" |
| 50 |
local dest_dir="$2" |
| 51 |
local dotfiles_dir="$3" |
| 52 |
local dotfiles_subdir="$4" |
| 53 |
local dotted=$5 |
| 54 |
local exclude_file_globs="$6" |
| 55 |
local include_file_globs="$7" |
| 56 |
local symlink_dirs_file_globs="$8" |
| 57 |
local mk_dirs_file_globs="$9" |
| 58 |
local dest_path="$(build_path "$dest_dir" "$dir" $dotted)" |
| 59 |
|
| 60 |
$DEBUG "show_dir $1 $2 $3 $4 $5 $6 $7 $8 $9" |
| 61 |
|
| 62 |
$VERBOSE "recurring on $dest_path" |
| 63 |
|
| 64 |
pushdir "$dir" |
| 65 |
for f in *; do |
| 66 |
$DEBUG "handling the file $f" |
| 67 |
next_dir="$(file_join "$dotfiles_subdir" "$dir")" |
| 68 |
handle_file "$f" "$dest_path" "$dotfiles_dir" "$next_dir" 1 "$exclude_file_globs" "$include_file_globs" "$symlink_dirs_file_globs" "$mk_dirs_file_globs" |
| 69 |
done |
| 70 |
popdir |
| 71 |
} |
| 72 |
|
| 73 |
sigil_for() { |
| 74 |
local file="$1" |
| 75 |
local symlink_dirs_file_globs="$2" |
| 76 |
local copy_always=0 |
| 77 |
local symlink_dirs=0 |
| 78 |
|
| 79 |
for copy_file in $COPY_ALWAYS; do |
| 80 |
$DEBUG "copy_file: $copy_file" |
| 81 |
$DEBUG "file: $file" |
| 82 |
|
| 83 |
case "$file" in |
| 84 |
$copy_file) |
| 85 |
copy_always=1 |
| 86 |
break |
| 87 |
;; |
| 88 |
esac |
| 89 |
done |
| 90 |
|
| 91 |
if [ -n "$symlink_dirs_file_globs" ]; then |
| 92 |
symlink_dirs=1 |
| 93 |
fi |
| 94 |
|
| 95 |
if [ $copy_always -eq 1 ]; then |
| 96 |
echo 'X' |
| 97 |
elif [ $symlink_dirs -eq 1 ]; then |
| 98 |
echo '$' |
| 99 |
else |
| 100 |
echo '@' |
| 101 |
fi |
| 102 |
} |
| 103 |
|
| 104 |
show_file() { |
| 105 |
local file="$1" |
| 106 |
local dest_dir="$2" |
| 107 |
local dotfiles_dir="$3" |
| 108 |
local dotfiles_subdir="$4" |
| 109 |
local dotted=$5 |
| 110 |
local symlink_dirs_file_globs="$6" |
| 111 |
local dest_file="$(build_path "$dest_dir" "$file" $dotted)" |
| 112 |
local src_file="$(file_join "$dotfiles_subdir" "$file")" |
| 113 |
local abs_src_file="$(file_join "$dotfiles_dir" "$src_file")" |
| 114 |
local output="$dest_file:$abs_src_file" |
| 115 |
|
| 116 |
if [ $SHOW_SIGILS -eq 1 ]; then |
| 117 |
sigil="$(sigil_for "$src_file" "$symlink_dirs_file_globs")" |
| 118 |
output="$output:$sigil" |
| 119 |
fi |
| 120 |
|
| 121 |
if echo "$DEST_STACK:" | grep -v ":$dest_file:" >/dev/null; then |
| 122 |
DEST_STACK="$DEST_STACK:$dest_file" |
| 123 |
$PRINT "$output" |
| 124 |
else |
| 125 |
$VERBOSE "skipping hidden file $output" |
| 126 |
fi |
| 127 |
} |
| 128 |
|
| 129 |
handle_file() { |
| 130 |
local file="$1" |
| 131 |
local dest_dir="$2" |
| 132 |
local dotfiles_dir="$3" |
| 133 |
local dotfiles_subdir="$4" |
| 134 |
local dotted=$5 |
| 135 |
local exclude_file_globs="$6" |
| 136 |
local include_file_globs="$7" |
| 137 |
local symlink_dirs_file_globs="$8" |
| 138 |
local mk_dirs_file_globs="$9" |
| 139 |
|
| 140 |
$DEBUG "handle_file $1 $2 $3 $4 $5 $6 $7 $8" "$9" |
| 141 |
|
| 142 |
if [ ! -e "$file" ]; then |
| 143 |
$VERBOSE "skipping non-existent file $file" |
| 144 |
elif is_excluded "$dotfiles_subdir/$file" "$exclude_file_globs" "$include_file_globs"; then |
| 145 |
$VERBOSE "skipping excluded file $file" |
| 146 |
elif [ -d "$file" ] && is_excluded "$dotfiles_subdir/$file" "$symlink_dirs_file_globs" "$mk_dirs_file_globs"; then |
| 147 |
show_file "$file" "$dest_dir" "$dotfiles_dir" "$dotfiles_subdir" $dotted "$symlink_dirs_file_globs" |
| 148 |
elif [ -d "$file" ]; then |
| 149 |
show_dir "$file" "$dest_dir" "$dotfiles_dir" "$dotfiles_subdir" $dotted "$exclude_file_globs" "$include_file_globs" "$symlink_dirs_file_globs" "$mk_dirs_file_globs" |
| 150 |
else |
| 151 |
show_file "$file" "$dest_dir" "$dotfiles_dir" "$dotfiles_subdir" $dotted |
| 152 |
fi |
| 153 |
} |
| 154 |
|
| 155 |
is_metafile() { |
| 156 |
host_portion="$(echo "$1" | sed -e 's/host-.*/host-/')" |
| 157 |
tag_portion="$(echo "$1" | sed -e 's/tag-.*/tag-/')" |
| 158 |
|
| 159 |
[ "x$host_portion" = 'xhost-' -o "x$tag_portion" = 'xtag-' -o "x$1" = "xhooks" ] |
| 160 |
} |
| 161 |
|
| 162 |
dotfiles_dir_excludes() { |
| 163 |
local dotfiles_dir="$1" |
| 164 |
local excludes="$2" |
| 165 |
|
| 166 |
$DEBUG "dotfiles_dir_excludes $dotfiles_dir" |
| 167 |
$DEBUG " with excludes: $excludes" |
| 168 |
|
| 169 |
set -o noglob |
| 170 |
for exclude in $excludes; do |
| 171 |
if echo "$exclude" | grep ':' >/dev/null; then |
| 172 |
dotfiles_dir_pat="$(echo "$exclude" | sed 's/:.*//')" |
| 173 |
file_glob="$(echo "$exclude" | sed 's/.*://')" |
| 174 |
|
| 175 |
if [ "x$dotfiles_dir_pat" != "x*" ] && is_relative "$dotfiles_dir_pat"; then |
| 176 |
dotfiles_dir_pat="$PWD/$dotfiles_dir_pat" |
| 177 |
fi |
| 178 |
|
| 179 |
if [ "x$dotfiles_dir_pat" = "x*" -o "x$dotfiles_dir_pat" = "x$dotfiles_dir" ]; then |
| 180 |
echo "$file_glob" |
| 181 |
fi |
| 182 |
else |
| 183 |
echo "$exclude" |
| 184 |
fi |
| 185 |
done |
| 186 |
set +o noglob |
| 187 |
} |
| 188 |
|
| 189 |
is_excluded() { |
| 190 |
local file="$1" |
| 191 |
local exclude_file_globs="$2" |
| 192 |
local include_file_globs="$3" |
| 193 |
local base_file="$(basename "$file")" |
| 194 |
|
| 195 |
$DEBUG "is_excluded $file $exclude_file_globs $include_file_globs" |
| 196 |
|
| 197 |
for exclude_file_glob in $exclude_file_globs; do |
| 198 |
$DEBUG "file: $file" |
| 199 |
$DEBUG "exclude_file_glob: $exclude_file_glob" |
| 200 |
|
| 201 |
is_single_excluded "$file" "$exclude_file_glob" "$include_file_globs" |
| 202 |
ret=$? |
| 203 |
if [ $ret -eq 0 -o $ret -eq 1 ]; then |
| 204 |
return $ret |
| 205 |
fi |
| 206 |
|
| 207 |
is_single_excluded "$base_file" "$exclude_file_glob" "$include_file_globs" |
| 208 |
ret=$? |
| 209 |
if [ $ret -eq 0 -o $ret -eq 1 ]; then |
| 210 |
return $ret |
| 211 |
fi |
| 212 |
done |
| 213 |
|
| 214 |
return 1 |
| 215 |
} |
| 216 |
|
| 217 |
is_single_excluded() { |
| 218 |
local file="$1" |
| 219 |
local exclude_file_glob="$2" |
| 220 |
local include_file_globs="$3" |
| 221 |
|
| 222 |
case "$file" in |
| 223 |
$exclude_file_glob) |
| 224 |
for include_file_glob in $include_file_globs; do |
| 225 |
case "$file" in |
| 226 |
$include_file_glob) return 1;; |
| 227 |
esac |
| 228 |
done |
| 229 |
|
| 230 |
return 0 |
| 231 |
;; |
| 232 |
esac |
| 233 |
|
| 234 |
return 2 |
| 235 |
} |
| 236 |
|
| 237 |
show_help() { |
| 238 |
local exit_code=${1:-0} |
| 239 |
|
| 240 |
$PRINT "Usage: lsrc [-FhqVv] [-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]" |
| 241 |
$PRINT "see lsrc(1) and rcm(7) for more details" |
| 242 |
|
| 243 |
exit $exit_code |
| 244 |
} |
| 245 |
|
| 246 |
handle_command_line() { |
| 247 |
local arg_tags= |
| 248 |
local verbosity=0 |
| 249 |
local version=0 |
| 250 |
local show_sigils=0 |
| 251 |
local dotfiles_dirs= |
| 252 |
local excludes= |
| 253 |
local includes= |
| 254 |
local symlink_dirs= |
| 255 |
local never_symlink_dirs= |
| 256 |
local hostname= |
| 257 |
local undotted= |
| 258 |
local never_undotted= |
| 259 |
|
| 260 |
while getopts :FVqvhI:x:B:S:s:U:u:t:d: opt; do |
| 261 |
case "$opt" in |
| 262 |
F) show_sigils=1;; |
| 263 |
h) show_help ;; |
| 264 |
I) includes="$(append_variable "$includes" "$OPTARG")" ;; |
| 265 |
t) arg_tags="$(append_variable "$arg_tags" "$OPTARG")" ;; |
| 266 |
v) verbosity=$(($verbosity + 1));; |
| 267 |
q) verbosity=$(($verbosity - 1));; |
| 268 |
d) dotfiles_dirs="$(append_variable "$dotfiles_dirs" "$OPTARG")" ;; |
| 269 |
V) version=1;; |
| 270 |
x) excludes="$(append_variable "$excludes" "$OPTARG")" ;; |
| 271 |
S) symlink_dirs="$(append_variable "$symlink_dirs" "$OPTARG")" ;; |
| 272 |
s) never_symlink_dirs="$(append_variable "$never_symlink_dirs" "$OPTARG")" ;; |
| 273 |
U) undotted="$(append_variable "$undotted" "$OPTARG")" ;; |
| 274 |
u) never_undotted="$(append_variable "$never_undotted" "$OPTARG")" ;; |
| 275 |
B) hostname="$OPTARG";; |
| 276 |
?) show_help 64 ;; |
| 277 |
esac |
| 278 |
done |
| 279 |
shift $(($OPTIND-1)) |
| 280 |
|
| 281 |
handle_common_flags lsrc $version $verbosity |
| 282 |
HOSTNAME="$(determine_hostname "$hostname")" |
| 283 |
SHOW_SIGILS=$show_sigils |
| 284 |
TAGS="${arg_tags:-$TAGS}" |
| 285 |
DOTFILES_DIRS="${dotfiles_dirs:-$DOTFILES_DIRS}" |
| 286 |
EXCLUDES="${excludes:-$EXCLUDES}" |
| 287 |
INCLUDES="${includes:-$INCLUDES}" |
| 288 |
SYMLINK_DIRS="${symlink_dirs:-$SYMLINK_DIRS}" |
| 289 |
MK_DIRS="${never_symlink_dirs:-$MK_DIRS}" |
| 290 |
UNDOTTED="${undotted:-$UNDOTTED}" |
| 291 |
NEVER_UNDOTTED="${never_undotted:-$NEVER_UNDOTTED}" |
| 292 |
FILES="$@" |
| 293 |
|
| 294 |
$DEBUG "TAGS: $TAGS" |
| 295 |
$DEBUG "DOTFILES_DIRS: $DOTFILES_DIRS" |
| 296 |
} |
| 297 |
|
| 298 |
DEST_STACK= |
| 299 |
|
| 300 |
handle_command_line "$@" |
| 301 |
|
| 302 |
: ${DOTFILES_DIRS:=$DOTFILES_DIRS $DEFAULT_DOTFILES_DIR} |
| 303 |
$DEBUG "DOTFILES_DIRS: $DOTFILES_DIRS" |
| 304 |
|
| 305 |
: ${COPY_ALWAYS:=""} |
| 306 |
$DEBUG "COPY_ALWAYS: $COPY_ALWAYS" |
| 307 |
|
| 308 |
: ${SYMLINK_DIRS:=""} |
| 309 |
$DEBUG "SYMLINK_DIRS: $SYMLINK_DIRS" |
| 310 |
|
| 311 |
: ${MK_DIRS:=""} |
| 312 |
$DEBUG "MK_DIRS: $MK_DIRS" |
| 313 |
|
| 314 |
: ${UNDOTTED:=""} |
| 315 |
$DEBUG "UNDOTTED: $UNDOTTED" |
| 316 |
|
| 317 |
: ${NEVER_UNDOTTED:=""} |
| 318 |
$DEBUG "NEVER_UNDOTTED: $NEVER_UNDOTTED" |
| 319 |
|
| 320 |
relative_root_dir="$PWD" |
| 321 |
|
| 322 |
for DOTFILES_DIR in $DOTFILES_DIRS; do |
| 323 |
cd -- "$relative_root_dir" |
| 324 |
|
| 325 |
DOTFILES_DIR="$(eval echo "$DOTFILES_DIR")" |
| 326 |
|
| 327 |
if is_relative $DOTFILES_DIR; then |
| 328 |
DOTFILES_DIR="$PWD/$DOTFILES_DIR" |
| 329 |
fi |
| 330 |
|
| 331 |
if [ ! -d "$DOTFILES_DIR" ]; then |
| 332 |
$VERBOSE "skipping non-existent directory: $DOTFILES_DIR" |
| 333 |
continue |
| 334 |
fi |
| 335 |
|
| 336 |
exclude_file_globs="$(dotfiles_dir_excludes "$DOTFILES_DIR" "$EXCLUDES")" |
| 337 |
$DEBUG "exclude_file_globs: $exclude_file_globs" |
| 338 |
include_file_globs="$(dotfiles_dir_excludes "$DOTFILES_DIR" "$INCLUDES")" |
| 339 |
symlink_dirs_file_globs="$(dotfiles_dir_excludes "$DOTFILES_DIR" "$SYMLINK_DIRS")" |
| 340 |
$DEBUG "symlink_dirs_file_globs: $symlink_dirs_file_globs" |
| 341 |
mk_dirs_file_globs="$(dotfiles_dir_excludes "$DOTFILES_DIR" "$MK_DIRS")" |
| 342 |
$DEBUG "mk_dirs_file_globs: $mk_dirs_file_globs" |
| 343 |
undotted_file_globs="$(dotfiles_dir_excludes "$DOTFILES_DIR" "$UNDOTTED")" |
| 344 |
$DEBUG "undotted_file_globs: $undotted_file_globs" |
| 345 |
never_undotted_file_globs="$(dotfiles_dir_excludes "$DOTFILES_DIR" "$NEVER_UNDOTTED")" |
| 346 |
$DEBUG "never_undotted_file_globs: $never_undotted_file_globs" |
| 347 |
|
| 348 |
cd -- "$DOTFILES_DIR" |
| 349 |
DIR_STACK=":$DOTFILES_DIR" |
| 350 |
|
| 351 |
host_files="$DOTFILES_DIR/host-$HOSTNAME" |
| 352 |
if [ -d "$host_files" ]; then |
| 353 |
pushdir "$(basename "$host_files")" |
| 354 |
for escaped_file in ${FILES:-*}; do |
| 355 |
file="$(decode "$escaped_file")" |
| 356 |
dotted=0 |
| 357 |
if is_excluded "$file" "$undotted_file_globs" "$never_undotted_file_globs"; then |
| 358 |
dotted=1 |
| 359 |
fi |
| 360 |
|
| 361 |
handle_file "$file" "$DEST_DIR" "$host_files" . "$dotted" "$exclude_file_globs" "$include_file_globs" "$symlink_dirs_file_globs" "$mk_dirs_file_globs" |
| 362 |
done |
| 363 |
popdir |
| 364 |
fi |
| 365 |
|
| 366 |
cd -- "$DOTFILES_DIR" |
| 367 |
|
| 368 |
for tag in $TAGS; do |
| 369 |
if [ -d "tag-$tag" ]; then |
| 370 |
pushdir "$(basename "tag-$tag")" |
| 371 |
for escaped_file in ${FILES:-*}; do |
| 372 |
file="$(decode "$escaped_file")" |
| 373 |
$DEBUG "TAG: $tag, exclude_file_globs: $exclude_file_globs" |
| 374 |
dotted=0 |
| 375 |
if is_excluded "$file" "$undotted_file_globs" "$never_undotted_file_globs"; then |
| 376 |
dotted=1 |
| 377 |
fi |
| 378 |
handle_file "$file" "$DEST_DIR" "$DOTFILES_DIR/tag-$tag" . "$dotted" "$exclude_file_globs" "$include_file_globs" "$symlink_dirs_file_globs" "$mk_dirs_file_globs" |
| 379 |
done |
| 380 |
popdir |
| 381 |
fi |
| 382 |
done |
| 383 |
|
| 384 |
cd -- "$DOTFILES_DIR" |
| 385 |
|
| 386 |
for escaped_file in ${FILES:-*}; do |
| 387 |
file="$(decode "$escaped_file")" |
| 388 |
dotted=0 |
| 389 |
if is_metafile "$file"; then |
| 390 |
continue |
| 391 |
fi |
| 392 |
|
| 393 |
if is_excluded "$file" "$undotted_file_globs" "$never_undotted_file_globs"; then |
| 394 |
dotted=1 |
| 395 |
fi |
| 396 |
|
| 397 |
handle_file "$file" "$DEST_DIR" "$DOTFILES_DIR" . "$dotted" "$exclude_file_globs" "$include_file_globs" "$symlink_dirs_file_globs" "$mk_dirs_file_globs" |
| 398 |
done |
| 399 |
done |