onair.cgi 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. #!/bin/sh
  2. # https://mro.name/radio-privatkopie
  3. #
  4. # redirect to the given broadcast according station and time.
  5. #
  6. # Expected URLs:
  7. #
  8. # http://rec.mro.name/app/onair.cgi/stations/b2?t=2021-10-10T17:05:00+02:00
  9. # http://rec.mro.name/stations/b2/now?t=2021-10-10T17:05:00+02:00
  10. #
  11. # https://vaneyckt.io/posts/safer_bash_scripts_with_set_euxo_pipefail/
  12. set -eu
  13. [ "" = "${QUERY_STRING}" ] && QUERY_STRING="t=$(date +'%FT%T%z' | sed -e 's/\(..\)$/:\1/')"
  14. case "${QUERY_STRING}" in
  15. t=*) readonly t0="$(echo "${QUERY_STRING}" | cut -c 3-)"
  16. ;;
  17. *) echo miss >> /tmp/onair.log
  18. exit 1
  19. ;;
  20. esac
  21. [ "" = "${PATH_INFO}" ] && {
  22. cat <<EOF
  23. Content-Type: text/xml; charset=utf-8
  24. <broadcasts xmlns="../../../../../assets/2013/radio-pi.rdf" xml:lang="de" modified="1970-01-01T00:00:01+00:00">
  25. <!-- empty at the time but should be each stations the currently airing broadcast -->
  26. </broadcasts>
  27. EOF
  28. exit 0
  29. }
  30. cd "..${PATH_INFO}" || exit 1
  31. # we're inside stations/<station>
  32. # find a the biggest match <= 'now'
  33. readonly dir="$(date -d "${t0}" +'%Y/%m/%d')"
  34. readonly sto="$(date -d "${t0}" +'%H%M')"
  35. for bc_str in $(ls --reverse "${dir}"/????.xml)
  36. do
  37. bc="$(basename "${bc_str}" .xml)"
  38. [ "${bc}" -le "${sto}" ] && {
  39. cat <<EOF
  40. Status: 302
  41. Location: ../../../..${PATH_INFO}/${bc_str}
  42. Content-Type: text/plain; charset=utf-8
  43. Redirecting to the according broadcast…
  44. EOF
  45. exit 0
  46. }
  47. done
  48. # none found, prbly far future.
  49. cat <<EOF
  50. Status: 404
  51. Content-Type: text/plain; charset=utf-8
  52. Not Found
  53. EOF