#!/bin/sh
# Extract details of declared FEATUREM (feature months), if any
#   $1 must be the .*.HTML body source or some head portion of it.
#   $2 can optionally be the month 1--12 (1, 2, ... 12) being tested for.
if [ $# -lt 1 ]; then
    echo "Missing source name." 1>&2
    exit 2
fi

MONTH=""
if [ $# -gt 1 ]; then
    MONTH="$2"
fi

# If $2 is not set (or ""),
#     returns the set of feature months raw if FEATUREM present, else empty.
# If $2 is set to a month M or MM
#     returns NONE if FEATUREM is absent (or specifies no months),
#     else the month if FEATUREM present and the month is present,
#     else empty. 
# 
# This second case allows easy rejection of articles not targeted at
# a given month, but accepting them if no per-month FEATUREM is specified.

# Extract the raw feature months or nothing if no FEATUREM present.
MONTHS="`sed < $1 -n -e 's/^ *<!-- *FEATUREM  *\([ 0-9]*\) *--> *$/\1/p' | head -1`"

# No month specified.
if [ "X" = "X$MONTH" ]; then
    echo $MONTHS
    exit 0
fi

# Asked for a month where none are specified by as FEATUREM tag.
if [ "X" = "X$MONTHS" ]; then
    echo NONE
    exit 1
fi

for m in $MONTHS; do
   # Echo the month if a numeric match is found.
   if [ $MONTH -eq $m ]; then
       echo $MONTH
       exit 0
   fi
done

# No match found by month in those listed in FEATUREM tag.
exit 0
