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