#!/bin/sh

# This script expects two arguments:
#  <PRESENTATION_VERSION> <NUM_MODS> <MODULE_SOURCE_FILE>
# and expects MODULE_SOURCE_FILE names of the
# form "module-${number}.content".

version="$1"
shift
nmods="$1"
shift
module="`basename "$1" .content | cut -d - -f 2`"
shift

nextmodnum="$(($module+1))"
if [ -f "module-${nextmodnum}.content" ]; then
    nextmodtitle="`cat module-"$nextmodnum".title | tr ' /' __`"
    nextmodfile="lojban-thing-module-${nextmodnum}-${nextmodtitle}.html"
    nextmodlink="`echo -n '<a href="'"${nextmodfile}"'">next module</a>' | sed -e 's|/|//|g' -e s/\\/\\/g`"
else
    nextmodlink="no more modules" # or a link to a module index
fi

sed -e "s/__VERSION__/${version}/g" -e "s/__MODULE__/${module}/g" \
    -e "s/__MODULES__/${nmods}/g" -e "s/__NEXT_MODULE_LINK__/${nextmodlink}/g" header
cat module-"${module}".content
cat footer

