#!/bin/bash

#set -xv

#BASEDIR=/home/pupman/htroot/htdocs/listarchives
BASEDIR=/srv/www/htdocs/pupman.com/listarchives
YEAR=$(date +%Y)
MONTH=$(date +%b)
HTMLFILE=${BASEDIR}/${YEAR}idx.html

while getopts ":y:" opt; do
  case $opt in
    y ) YEAR=$OPTARG
        ;;
    \? ) echo "Usage: $0 [year]"
  esac
done

echo YEAR: $YEAR

if [ ! -e ${BASEDIR}/${YEAR} ]; then
  mkdir ${BASEDIR}/${YEAR}
  chown nobody:nobody ${BASEDIR}/${YEAR}
fi

if [ ! -e $YEAR/$MONTH ]; then
  mkdir -p ${BASEDIR}/${YEAR}/${MONTH}
  chown nobody:nobody ${BASEDIR}/${YEAR}/${MONTH}
fi

echo '<html>' > $HTMLFILE
echo "<center><H3>/listarchives/${YEAR} List Archives</H3></center>" >> $HTMLFILE
echo "<ul>" >> $HTMLFILE

for i in $(ls -tr ${BASEDIR}/${YEAR} | sort -M); do
echo "<li><a href="/listarchives/${YEAR}/${i}/threads.html" target="view_window">${i} archives</a>" >> $HTMLFILE
done

echo "<li><a href=/listarchives/${YEAR}.zip target="view_window">Zipped archives</a>" >> $HTMLFILE

echo "</ul>" >> $HTMLFILE
echo '</html>' >> $HTMLFILE

#
# Make the zip file
#
cd $BASEDIR
zip -q -r ${YEAR}.zip ${YEAR}

