index.sh 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/bin/sh
  2. #
  3. # Copyright (c) 2015 Marcus Rohrmoser, http://purl.mro.name/recorder
  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}")/../htdocs/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 | egrep -hoe '[^/]+/[0-9]{4}/[0-9]{2}/[0-9]{2}$')"
  45. date="$(pwd | egrep -hoe '[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 --nowarning --noout "${xml}" || { echo "not well-formed: ${xml}" 1>&2 && continue ; }
  56. # timezone fix each xml in place?
  57. grep -hoe "^\s*<broadcast\s[^>]*" "${xml}" >> "${dst}"~ # start tag without closing >
  58. echo " modified='$(date --reference="${xml}" +\%F'T'\%T\%:z)'>" >> "${dst}"~ # additional attribute: file modification time
  59. grep 'DC.identifier' "${xml}" 1>/dev/null 2>/dev/null || { # amend identifier if missing.
  60. echo " <meta content='${prefix}/$(basename "${xml}" .xml | sed -e "s/&/\&amp;/g" -e "s/'/\&apos;/g")' name='DC.identifier'/>" >> "${dst}"~
  61. }
  62. grep -v '^<!-- ' "${xml}" | grep -v "<meta content='' " | tail -n +4 >> "${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 --nowarning --nsclean --format --encode "UTF-8" --relaxng "../../../../../app/pbmi2003-recmod2012/broadcast.rng" --output "${dst}" "${dst}"~ && rm "${dst}"~
  69. if [ 0 -eq $? ] ; then
  70. gzip --best "${dst}"
  71. echo "${1}/${dst}.gz"
  72. else
  73. rm "${dst}"
  74. fi
  75. fi
  76. shift
  77. done
  78. fi