#!/bin/sh
# $NetBSD: makeindex.sh,v 1.21 2023/04/09 07:09:32 rillig Exp $
#
# NAME
# makeindex.sh - regenerate the index.html file with latest news and
# events
#
# SYNOPSIS
# env WEB_PREFIX=<dir> CHANGES_DIR=<dir> EVENTS_DIR=<dir> makeindex.sh
#
# DESCRIPTION
# makeindex.sh regenerates the index.html file with the latest
# items from the Changes directory and gallery/events.xml file.
#
# ENVIRONMENT
# WEB_PREFIX
# An absolute pathname pointing to the top-level directory
# of the htdocs.
#
# HTDOCS_RELTOPDIR
# A relative path pointing to the top-level directory of
# the web site.
#
# CHANGES_DIR
# The directory where the changes are located.
#
# EVENTS_DIR
# The directory where the events are located.
#
set -eu
fail() {
echo "$0: ERROR: $1" 1>&2
exit 1
}
checkfile() {
if [ ! -f "$1" ]; then
fail "\"$1\" does not exist."
fi
}
# Check that all environment variables are properly defined.
for var in WEB_PREFIX CHANGES_DIR EVENTS_DIR HTDOCS_RELTOPDIR; do
eval "value=\${${var}-}"
if [ "$value" = "" ]; then
fail "$var must be set and non-empty."
elif [ ! -d "$value" ]; then
fail "$var must point to an existing directory."
fi
done
checkfile "index.html"
# Check that the index file has all necessary category tags.
for category in NEWS EVENTS; do
for start_end in START END; do
tag="<!-- ${category}::${start_end} -->"
if grep "$tag" "index.html" 1>/dev/null; then
: "Fine."
else
echo "$0: error: The tag $tag is missing in `pwd`/index.html." 1>&2
exit 1
fi
done
done
# Extract the head from the page.
awk "(NR == 1), /<!-- NEWS::START -->/ { print }" \
< "index.html" \
> "index.tmp"
checkfile "index.chg"
# Extract the 4 latest changes.
sed -e 's,\(.*\),<li>&</li>,' \
-e '4q' \
< "index.chg" \
>> "index.tmp"
# Extract the middle part from the page.
awk "/<!-- NEWS::END -->/, /<!-- EVENTS::START -->/ { print }" \
< "index.html" \
>> "index.tmp"
# Extract the 4 next upcoming events.
checkfile "${EVENTS_DIR}/events.chg"
if [ -s "${EVENTS_DIR}/events.chg" ]; then
cat >> "index.tmp" <<EOF
<h3>Upcoming Events</h3>
<ul>
EOF
sed -e 's,\(.*\),<li>&</li>,' \
-e '4q' \
< "${EVENTS_DIR}/events.chg" \
>> "index.tmp"
cat >> "index.tmp" <<EOF
<li>
<span class="more">
<a href="gallery/events.html">» Archive</a>
</span>
</li>
</ul>
EOF
fi
# Extract the tail from the page.
awk "/<!-- EVENTS::END -->/, 0 { print }" \
< "index.html" \
>> "index.tmp"
# Replace the index page with the newly generated one.
mv -f "index.tmp" "index.html"
CVSweb <webmaster@jp.NetBSD.org>