clean 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #!/usr/bin/env bash
  2. #
  3. # Copyright (C) 2021 Leah Rowe <info@minifree.org>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. [ "x${DEBUG+set}" = 'xset' ] && set -v
  18. set -e
  19. if [ ! -d "www" ]; then
  20. printf "untitled error: no www/ directory. Exiting\n"
  21. exit 1
  22. fi
  23. # NOTE: the cleansite script does check for symlinks, but unlike in the build
  24. # script, an entire site won't simply be skipped for having symlinks.
  25. cleansite() {
  26. SITENAME="${1}"
  27. if [ ! -d "www/${SITENAME}/site" ]; then
  28. printf "Site '%s' has no site directory; skipping clean\n" "${SITENAME}"
  29. return 0
  30. fi
  31. if [ -L "www/${SITENAME}" ] || [ -L "www/${SITENAME}/site" ]; then
  32. printf "Site '%s' is a symlink directory. skipping clean\n" "${SITENAME}"
  33. return 0
  34. fi
  35. printf "Cleaning generated files from site: '%s'\n" "${SITENAME}"
  36. for f in $(find -L www/"${SITENAME}"/site/ -type f -name '*.html'); do
  37. if [ ! -f "${f}" ]; then continue; fi
  38. if [ -L "${f}" ]; then continue; fi
  39. if [ -f "${f%.html}.md" ] && [ ! -L "${f%.html}.md" ]; then
  40. rm -f "${f}"
  41. fi
  42. done
  43. for f in $(find -L "www/${SITENAME}/site" -type f -name "*.date"); do
  44. if [ ! -f "${f}" ]; then continue; fi
  45. if [ -L "${f}" ]; then continue; fi
  46. if [ "${f##*/}" = "footer.include.date" ] \
  47. || [ "${f##*/}" = "nav.include.date" ] \
  48. || [ "${f##*/}" = "template.include.date" ]; then
  49. continue
  50. fi
  51. rm -f "${f}"
  52. done
  53. rm -f www/"${SITENAME}"/site/feed.xml
  54. for f in $(find -L www/"${SITENAME}"/site/ -type f -name 'MANIFEST'); do
  55. if [ ! -f "${f}" ]; then continue; fi
  56. if [ -L "${f}" ]; then continue; fi
  57. for ff in "${f%MANIFEST}"{index.md,index.html,feed.xml}; do
  58. if [ ! -f "${ff}" ]; then continue; fi
  59. if [ -L "${ff}" ]; then continue; fi
  60. rm -f "${ff}"
  61. done
  62. done
  63. }
  64. if [ $# -gt 0 ]; then
  65. for site in "${@}"; do
  66. sitename="${site##*/}"
  67. cleansite "${sitename}"
  68. done
  69. else
  70. for sitedir in www/*; do
  71. cleansite "${sitedir##*/}"
  72. done
  73. fi