index.sh 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #!/bin/sh
  2. #
  3. # Copyright (c) 2015 Marcus Rohrmoser, https://mro.name/radio-privatkopie
  4. #
  5. # Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  6. # associated documentation files (the "Software"), to deal in the Software without restriction,
  7. # including without limitation the rights to use, copy, modify, merge, publish, distribute,
  8. # sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in all copies or
  12. # substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  15. # NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES
  17. # OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  18. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  19. #
  20. # MIT License http://opensource.org/licenses/MIT
  21. #
  22. #
  23. #
  24. # generate a index.xml for either
  25. # - the accoding day-directories (commandline params) or
  26. # - all day-directories
  27. # by aggregating all broadcast xml files.
  28. #
  29. xmllint --version 2>/dev/null || { echo "Please install xmllint" && exit 1; }
  30. cwd="$(pwd)"
  31. cd "$(dirname "${0}")" && script_dir="$(pwd)"
  32. script_path="${script_dir}/$(basename "${0}")"
  33. cd "${cwd}" && cd "$(dirname "${0}")/../pages/stations" || { echo "foo" 1>&2 && exit 1; }
  34. base_dir="$(pwd)"
  35. recursion_blocker="recursion_blocker"
  36. if [ "" = "${1}" ] ; then
  37. ls -fds */????/??/?? | sort | xargs -P 1 "${script_path}" "${recursion_blocker}"
  38. else
  39. if [ "${recursion_blocker}" = "${1}" ] ; then shift ; fi
  40. while [ "" != "${1}" ]
  41. do
  42. cd "${cwd}/${1}"
  43. if [ 0 -eq $? ] ; then
  44. prefix="$(pwd | grep -hoEe '[^/]+/[0-9]{4}/[0-9]{2}/[0-9]{2}$')"
  45. date="$(pwd | grep -hoEe '[0-9]{4}/[0-9]{2}/[0-9]{2}$' | tr '/' '-')"
  46. dst="index.xml"
  47. # http://stackoverflow.com/a/7046926
  48. cat > "${dst}"~ <<END_OF_XML_PREAMBLE
  49. <?xml-stylesheet type='text/xsl' href='../../../app/broadcasts2html.xslt'?>
  50. <!-- unorthodox relative namespace to enable http://www.w3.org/TR/grddl-tests/#sq2 without a central server -->
  51. <broadcasts date='${date}' xmlns='../../../../../assets/2013/radio-pi.rdf'>
  52. END_OF_XML_PREAMBLE
  53. for xml in ????.xml
  54. do
  55. xmllint \
  56. --noout \
  57. --nowarning \
  58. "${xml}" || { echo "not well-formed: ${xml}" 1>&2 && continue ; }
  59. grep -vF '<!-- ' < "${xml}" \
  60. | grep -vF '<?xml ' \
  61. | grep -vF '<?xml-stylesheet ' \
  62. >> "${dst}"~ # rest of the broadcast xml file
  63. done
  64. echo "</broadcasts>" >> "${dst}"~
  65. # timezone fix resulting xml - add colon in case
  66. # sed --in-place --posix --regexp-extended --expression 's/([0-9]{4}-[0-9]{2}-[0-9]{2})[T ]([0-9]{2}:[0-9]{2}:[0-9]{2}[+-][0-9]{2})00/\1T\2:00/g' "${dst}"~
  67. printf "%s/" "${1}" 1>&2
  68. xmllint \
  69. --encode "UTF-8" \
  70. --format \
  71. --nowarning \
  72. --nsclean \
  73. --output "${dst}" \
  74. --relaxng "../../../../../app/pbmi2003-recmod2012/broadcast.rng" \
  75. "${dst}"~ \
  76. && rm "${dst}"~
  77. if [ 0 -eq $? ] ; then
  78. gzip --best "${dst}"
  79. echo "${1}/${dst}.gz"
  80. else
  81. rm "${dst}"
  82. fi
  83. fi
  84. shift
  85. done
  86. fi