Poor man's XPath library https://pagure.io/shellfu-bash-pxpath

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/bin/bash
  2. # MKit - simple install helper
  3. # See LICENSE file for copyright and license details.
  4. mkit_import ini
  5. __deploy_item() {
  6. #
  7. # Deploy item and make it look like wanted
  8. #
  9. # usage: __deploy_item src dst [mode]
  10. #
  11. # Both src and dst must be names of actual items[1],
  12. # whereas dst must not exist. On update, dst is
  13. # usually to be replaced but that is uninstall's
  14. # job!
  15. #
  16. # [1] Ie. src=foo and dst=/foo/bar does *always*
  17. # mean that foo will become 'bar'. This is
  18. # different than traditional `cp` behavior,
  19. # when this depends if 'bar' already exists
  20. # as a directory.
  21. #
  22. # If mode is omitted or empty, MKIT_DEFAULT_MODE is
  23. # used instead.
  24. #
  25. # Directories are copied recursively, and mode is
  26. # applied only to files.
  27. #
  28. local src=$1 # source path
  29. local dst=$2 # destination path
  30. local mode=${3:-$MKIT_DEFAULT_MODE} # mode
  31. local item # each in directory
  32. if test -d "$src"; then
  33. find "$src" -type f \
  34. | while read -r item; do
  35. [[ $item =~ .skel$ ]] \
  36. && grep -q "${item%.skel}" "$MKIT_LOCAL/built.lst" \
  37. && continue
  38. __deploy_item "$item" "$dst${item#$src}" "$mode"
  39. done
  40. else
  41. test "$mode" == "SRC" && mode=$(stat -c "%a" "$src")
  42. __maybe install -DTvm "$mode" "$src" "$dst"
  43. fi
  44. }
  45. __get_dst() {
  46. #
  47. # Find out target path for src file $2 of group $1
  48. #
  49. local grp=$1 # deploy group
  50. local src=$2 # each source
  51. local dst=$3 # alternative destination name
  52. test -n "$dst" || dst=${src##*/}
  53. echo "$(__get_root "$grp")/$dst"
  54. }
  55. __get_root() {
  56. #
  57. # Find out target root for group $1
  58. #
  59. local grp=$1 # deploy group
  60. local root # root for this group
  61. local destdir # value of DESTDIR
  62. root=$(ini 1value "roots:$grp")
  63. destdir=$(ini 1value ENV:DESTDIR)
  64. destdir=${destdir%/}
  65. case $destdir:$root in
  66. *:) die "missing in config.ini: roots:$grp" ;;
  67. :*) echo "$root" ;;
  68. *:*) echo "$destdir/$root" ;;
  69. esac
  70. }
  71. __maybe() {
  72. #
  73. # Call the deploy command $1 $@ unless in dry mode
  74. #
  75. debug "$@"
  76. local cmd="$1"; shift
  77. $MKIT_DRY && return
  78. case "$cmd" in
  79. cp|rm|rmdir|chmod|mkdir) $cmd "$@" ;;
  80. install) command -p install "$@" ;;
  81. *) die "bad command called";;
  82. esac
  83. }
  84. install() {
  85. #
  86. # Install product
  87. #
  88. local group # each deploy group
  89. local mode # mode (group-specific)
  90. local src # each source path
  91. local dst # each (final absolute) destination path
  92. ini lskeys "files" \
  93. | sort \
  94. | uniq \
  95. | while read -r group; do
  96. mode=$(ini 1value "modes:$group")
  97. ini values "files:$group" \
  98. | while read -r src dst; do
  99. dst=$(__get_dst "$group" "$src" "$dst")
  100. __deploy_item "$src" "$dst" "$mode"
  101. done
  102. done
  103. test -f "$MKIT_LOCAL/autoclean" && clean
  104. true
  105. }
  106. uninstall() {
  107. #
  108. # Uninstall product
  109. #
  110. local group # each deploy group
  111. local src # each source path
  112. local dst # each (final absolute) destination path
  113. ini lskeys "files" \
  114. | sort \
  115. | uniq \
  116. | while read -r group; do
  117. ini values "files:$group" \
  118. | while read -r src dst; do
  119. dst=$(__get_dst "$group" "$src" "$dst")
  120. __maybe rm -vrf "$dst"
  121. done
  122. done
  123. }