openbsd-webzine/issues/tools/sitemap.sh

41 lines
829 B
Bash
Raw Normal View History

#!/bin/sh
2021-09-29 19:12:27 +00:00
# print to stdout a sitemap.xml
set -e
usage()
{
2021-09-29 19:21:50 +00:00
echo "usage $0 <domain.tld> <site_directory>"
2021-09-29 19:12:27 +00:00
exit 1
}
test $# -eq 2 || usage
ndd="$1"
2021-09-29 19:12:27 +00:00
# sitemap header
2021-09-29 19:21:50 +00:00
cat <<EOF
<?xml version="1.0" encoding="UTF-8"?>
2021-09-29 19:28:39 +00:00
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
2021-09-29 19:21:50 +00:00
EOF
2021-09-29 19:12:27 +00:00
# list all pages in site_directory and print last mod time
cd "$2"
find . -name \*.html | cut -d'/' -f2- | sort | while read -r page
do
2022-07-02 13:23:07 +00:00
if test -f /bsd
then
printf '<url><loc>%s</loc><lastmod>%s</lastmod></url>\n' \
2021-09-29 19:12:27 +00:00
"https://${ndd}/${page}"\
2022-07-02 13:23:07 +00:00
"$(date -r $(stat -f %m) +%Y-%m-%d)"
else
set -x
printf '<url><loc>%s</loc><lastmod>%s</lastmod></url>\n' \
"https://${ndd}/${page}"\
"$(env LC_TIME=C TZ=UTC date +%Y-%m-%d)"
fi
done
2021-09-29 19:21:50 +00:00
echo "</urlset>"
2021-09-29 19:12:27 +00:00
exit