You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

77 lines
1.2 KiB

13 years ago
13 years ago
13 years ago
  1. #!/bin/sh
  2. info() { echo "$$: $*"; }
  3. err() { echo "E: $$: $*" >&2; }
  4. set -u
  5. if [ "x-d" = "x${1:-}" ]; then
  6. BASE="$2"
  7. shift 2
  8. else
  9. BASE=.
  10. fi
  11. FILES="$(readlink -f "$BASE")"
  12. for x; do
  13. # clean
  14. #
  15. case "$x" in
  16. "$BASE") continue ;;
  17. "$BASE"/*) x="${x#$BASE/}" ;;
  18. ./*) x="${x#./}" ;;
  19. esac
  20. # find target (d1)
  21. #
  22. if [ -L "$FILES/$x" ]; then
  23. d0="$(readlink "$FILES/$x")"
  24. d1="$(readlink -f "$FILES/$x")"
  25. [ "$d1" = "${d1#$FILES/}" ] || d1="$d0"
  26. elif [ ! -d "$FILES/$x" ]; then
  27. d1="$FILES/$x"
  28. else
  29. continue
  30. fi
  31. # link dir
  32. #
  33. d="${x%/*}"
  34. if [ "$d" = "$x" ]; then
  35. d=
  36. elif [ ! -d "$HOME/$d/" ]; then
  37. info "$d: creating dir"
  38. mkdir -p "$HOME/$d/"
  39. fi
  40. # link
  41. #
  42. if [ -L "$HOME/$x" ]; then
  43. # preexisting link ($d1)
  44. d0="$(readlink "$HOME/$x")"
  45. [ "$d0" != "$d1" ] || continue
  46. err "$x: relinking, was [$d0]"
  47. elif [ ! -e "$HOME/$x" ]; then
  48. # new
  49. info "$x: linking"
  50. elif [ -d "$HOME/$x" ]; then
  51. # preexisting dir
  52. err "$x: importing directory and linking [$d1]"
  53. if ! rmdir "$HOME/$x" 2> /dev/null; then
  54. rm "$FILES/$x"
  55. mv "$HOME/$x" "$FILES/$x"
  56. fi
  57. else
  58. # preexisting file
  59. info "$x: importing changes and linking [$d1]"
  60. rm "$FILES/$x"
  61. cp -a "$HOME/$x" "$FILES/$x"
  62. fi
  63. ln -snf "$d1" "$HOME/$x"
  64. done