delpage 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #!/bin/bash
  2. #
  3. # delpage
  4. #
  5. # Page deletion helper for the Dragora GNU/Linux-Libre website
  6. # (https://www.dragora.org)
  7. #
  8. #
  9. # Copyright (C) 2021 Michael Siegel
  10. #
  11. # Licensed under the Apache License, Version 2.0 (the "License");
  12. # you may not use this file except in compliance with the License.
  13. # You may obtain a copy of the License at
  14. #
  15. # http://www.apache.org/licenses/LICENSE-2.0
  16. #
  17. # Unless required by applicable law or agreed to in writing, software
  18. # distributed under the License is distributed on an "AS IS" BASIS,
  19. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  20. # See the License for the specific language governing permissions and
  21. # limitations under the License.
  22. #### INCLUDES ####
  23. . include/constants || exit 1
  24. . include/subroutines || exit 1
  25. #### CONSTANTS ####
  26. #### GLOBAL PARAMETERS ####
  27. target=
  28. #### FUNCTIONS ####
  29. ## Retrievers
  30. ## User queries and input parsers
  31. _prompt_target() {
  32. local input=
  33. while true
  34. do
  35. if [[ -z "$input" ]]
  36. then
  37. printf '%s\n' 'Please select a page to delete:'
  38. _show_pagetree -del
  39. printf '%s\n' "$HELP_TIP_INTERACTIVE"
  40. fi
  41. read -rp "$PROMPT" input
  42. if [[ -z "$input" ]]
  43. then
  44. continue
  45. elif [[ "$input" = "$HELP_COMMAND" ]]
  46. then
  47. _show_help "$FUNCNAME"
  48. elif [[ ! "$input" =~ ^[0123456789]+$ ]]
  49. then
  50. _perr "invalid selection -- '$input'"
  51. printf '%s\n' "$HELP_TIP_INTERACTIVE"
  52. else
  53. if [[ ! "$input" -gt 0 || ! "$input" -lt "${#pagetree[@]}" ]]
  54. then
  55. _perr "invalid selection -- '$input'"
  56. printf '%s\n' "$HELP_TIP_INTERACTIVE"
  57. else
  58. break
  59. fi
  60. fi
  61. done
  62. target="${pagetree[$input]%/}"
  63. }
  64. _prompt_confirm() {
  65. local input=
  66. while true
  67. do
  68. if [[ -z "$input" ]]
  69. then
  70. printf '%s\n' "Going to remove '$target'. Are you sure? [y/n]"
  71. printf '%s\n' "$HELP_TIP_INTERACTIVE"
  72. fi
  73. read -rp "$PROMPT" input
  74. if [[ -z "$input" ]]
  75. then
  76. continue
  77. elif [[ "$input" = "$HELP_COMMAND" ]]
  78. then
  79. _show_help "$FUNCNAME"
  80. elif [[ ! "$input" =~ ^y|n$ ]]
  81. then
  82. _perr "invalid input -- '$input'"
  83. else
  84. break
  85. fi
  86. done
  87. if [[ "$input" = n ]]
  88. then
  89. return 1
  90. else
  91. return 0
  92. fi
  93. }
  94. ## Checks
  95. ## Actions
  96. _del_page() {
  97. local target_full=
  98. local target_list=()
  99. local input=
  100. [[ -z "$target" ]] && \
  101. { _perr "${FUNCNAME}: target is empty"; return 1; }
  102. local lang_dir=
  103. for lang_dir in $(_get_lang_dirs)
  104. do
  105. target_full="${PAGES_DIR:?}/$lang_dir/$target"
  106. if [[ -e "$target_full" ]] && [[ ! -d "$target_full" ]]
  107. then
  108. _perr "'$target_full' should be a directory, but isn't."
  109. return 1
  110. fi
  111. target_list+=("$target_full")
  112. done
  113. unset lang_dir
  114. local t=
  115. for t in "${target_list[@]}"
  116. do
  117. if [[ ! -e "$t" ]]
  118. then
  119. _perr "Skipping non-existent directory '$t'."
  120. else
  121. rm -rf -- "$t" || return 1 # Provide error message?
  122. printf '%s\n' "Removed page '$t'."
  123. fi
  124. done
  125. unset t
  126. }
  127. _del_page_nav() {
  128. local grep_result=
  129. grep -q -- "^[[:space:]]*${target}/[[:space:]]*$" "$NAVTREE_FILE"
  130. grep_result="$?"
  131. if [[ "$grep_result" -gt 1 ]] # grep(1) uses return values > 1 for errors.
  132. then
  133. _perr "Could not look up '$target' in '$NAVTREE_FILE'."
  134. return 1
  135. elif [[ "$grep_result" -eq 0 ]]
  136. then
  137. { cp -a -- "$NAVTREE_FILE" "${NAVTREE_FILE}.old" && \
  138. sed -- "\,^[[:space:]]*${target}/[[:space:]]*\$, d" \
  139. "${NAVTREE_FILE}.old" > "$NAVTREE_FILE"; } || \
  140. { _perr "Could not remove '$target' from site navigation."; \
  141. return 1; }
  142. printf '%s\n' "Removed '$target' from site navigation."
  143. fi
  144. }
  145. _show_help() {
  146. case "$1" in
  147. # _prompt_target)
  148. # :
  149. # ;;
  150. global)
  151. cat <<'EOF'
  152. Remove a page interactively
  153. Usage:
  154. delpage
  155. Options:
  156. --help
  157. Show this help text and exit
  158. EOF
  159. ;;
  160. *)
  161. printf '%s\n' "Sorry, no help text available."
  162. ;;
  163. esac
  164. }
  165. #### MAIN ####
  166. if [[ "$1" = '--help' ]]
  167. then
  168. _show_help global
  169. exit
  170. fi
  171. ## Environment checks
  172. _env_checks || _abort
  173. ## Action
  174. _get_pagetree
  175. _prompt_target
  176. if _prompt_confirm
  177. then
  178. _del_page || { _perr "Aborted."; _abort; }
  179. _del_page_nav || _abort
  180. fi