#!/bin/sh

archivebaseuri="gopher://bitreich.org/9/tgtimes/archive"

releases="$(find . -type d | tail -n +2 | cut -d'/' -f 2-)"

cat news.atom.head.xml

updated="$(date +%FT%T%z)"
printf "\t<updated>%s</updated>\n" "${updated}"

for release in $releases;
do
       uri="${archivebaseuri}/${release}"
       printf "\t\t<entry>\n"
       printf "\t\t<id>%s</id>\n" "${uri}"
       printf "\t\t<title><![CDATA[%s]]></title>\n" "${release}"
       printf "\t\t<author><name>The Gopher Times Authors</name></author>\n"

       cd ${release}
       txtedition=$(find . -type f -name "*.txt" | cut -d'/' -f 2-)
       pdfedition=$(find . -type f -name "*.pdf" | cut -d'/' -f 2-)
       printf "\t\t<content type=\"text\"><![CDATA["
       cat $txtedition
       printf "]]></content>\n"

       lastmodified=$(stat --printf="%Y" ${txtedition})
       lastupdated="$(date +%FT%T%z -d @${lastmodified})"
       printf "\t\t<updated>%s</updated>\n" "${lastupdated}"
       mimetype="$(file --mime-type -b "${pdfedition}")"
       printf "\t\t<link type=\"%s\" href=\"%s/%s\" />\n" \
                       "${mimetype}" \
                       "${uri}" \
                       "${pdfedition}"
       cd ..
       printf "\t\t</entry>\n"
done

cat news.atom.footer.xml