#!/bin/sh

#    hyperproc version 0.1
#    =====================

#    Copyright (C) 2008 Paul Mobbs and the
#    Free Range electrohippie collective

#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.

#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.

#    For a copy of the GNU General Public License see 
#    <http://www.fraw.org.uk/fraw_admin/copyright.shtml>.




# display program banner
echo
echo
echo ------------------------------------
echo Hypermail Multiple Mailbox Processor
echo ------------------------------------
echo


proginfo() {
		echo 'hyperproc is a script that stiches together the hypermail program'
		echo 'to process multiple mbox files in a directory.'
		echo
	    }

comformat() {
		echo 'Command format:'
		echo ' hyperproc.sh [source dir.] [output dir.] {archive title} {index file} '
		echo
	    }

progop() {
		echo 'Source and output directories are required - the index file name and'
		echo 'title string are optional, and will default to "index.html" and'
		echo '"Mailbox Archive" if not specified.'
		echo
		echo 'If the output directory exists the program will terminate.'
		echo
	    }


# check for number of parameters/help request
if [ $# -gt 0 ]; then
	if [ $1 = '-h' ]; then
		proginfo
		comformat
		progop
		echo
		echo
		exit 0
	fi
else
	echo 'Error: Too few parameters (use -h for help)'
	comformat
	echo
	echo
	exit 0
fi
if [ $# -lt 2 ]; then
	echo 'Error: Too few parameters (use -h for help)'
	comformat
	echo
	echo
	exit 0
fi

# check parameter errors
if [ -z $1 ]; then
	echo 'Error: no source directory specified (use -h for help)'
	comformat
	echo
	echo
	exit 0
fi
if [ -z $2 ]; then
	echo 'Error: no output directory specified (use -h for help)'
	comformat
	echo
	echo
	exit 0
fi


# process input values
cwd=$( pwd )
source=$cwd/$1/
output=$cwd/$2/
index='index.html'
title='Mailbox Archive'
if [ $# -gt 2 ]; then
	title=$3
fi
if [ $# -gt 3 ]; then
	index=$4
fi
index=$output$index

# check directory errors
if [ -e $source ]; then
	if [ -e $output ]; then
		echo 'Error: output directory "'$output'" exists (cannot overwrite)'
		echo
		comformat
		echo
		echo
		exit 0    
	fi
else
	echo 'Error: source directory "'$source'" does not exist'
	echo
	comformat
	echo
	echo
	exit 0    
fi


# display program variables
echo Source Directory: $source
echo Output Directory: $output
echo Directory Index : $index
echo Title String    : $title
echo
echo Processing each file in $source:
echo


# create output directory
mkdir $output


# generate index page header
echo '<html><head><title>'$title'</title><style>' >> $index
echo "body { margin: 40px; Background-color: white; }" >> $index
echo "div { color: black; font-size: 16px; font-family: arial, helvetica, sans-serif; }" >> $index
echo "h1 { color: green; font-size: 24px; }" >> $index
echo "a { color: blue; }" >> $index
echo "a:hover { color: red; }" >> $index
echo "</style></head><body><div>" >> $index
echo '<h1>'$title'</h1>' >> $index


# process mailbox files list
cd $source
for file in *; do
	if [ -f $file ]; then
		echo Processing $file

		# create mailbox directory and process file
		mkdir "$output$file"
		hypermail -m $source$file -d $output$file -x -a ../index.html $file

		# generate link in index file
		echo '<a href="'$file'/index.html">'$file'</a><br>' >> $index

	fi
done
cd $cwd


# output index foot
echo "</div></body></html>" >> $index


# end the program
echo
echo Run complete
echo
echo
echo

exit 0




