#!/bin/bash

POD2HTML='pod2html'
POD2HTMLOPT='--backlink="Top" --css=http://search.cpan.org/s/style.css'
CSS="http://search.cpan.org/s/style.css"
BACKLINK="Top"
DPATH='../'
WEBPATH='/doc/'

for k in $@ ; do
# 	echo $k
	opt=`echo $k | sed -r s/^--//`
# 	echo "    $opt"
	option=`echo $opt | awk -F= '{print $1}'`
	value=`echo $opt | awk -F= '{print $2}'`
# 	echo "       $option"
# 	echo "       $value"
	if [ $option = "css" ] ; then
# 		echo "[OPT] setting CSS to $value"
		CSS=$value
	fi
	if [ $option = "backlink" ] ; then
# 		echo "[OPT] setting BACKLINK to $value"
		BACKLINK=$value
	fi
	if [ $option = "pod2htmlopt" ] ; then
# 		echo "[OPT] setting POD2HTML to pod2html $value"
		POD2HTMLOPT=$value
	fi
	if [ $option = "path" ] ; then
		DPATH=$value
	fi
	if [ $option = "webpath" ] ; then
		WEBPATH=$value
	fi
done

DATE=`date +%d%m%Y`

echo "Saving HTML doc in $DPATH"
echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">' > $DPATH/index.html
echo '<html xmlns="http://www.w3.org/1999/xhtml">' >> $DPATH/index.html
echo "<!-- generated with options : css=${CSS} webpath=${WEBPATH} path=${DPATH} backlink=${BACKLINK} -->" >> $DPATH/index.html
echo '<head>' >> $DPATH/index.html
echo '<title>Index of slack-get 1.0.x API doc</title>' >> $DPATH/index.html
echo '<link rel="stylesheet" href="sg_doc.css" type="text/css" />' >> $DPATH/index.html
echo '</head>' >> $DPATH/index.html
echo '<body>' >> $DPATH/index.html
echo "You can found here the complete documentation of the slack-get 1.0.X API.<br/> This version is the most complete doc you can found on the slack-get API due to the fact each time I build the project the new doc is automatically uploaded on the website :-)<br/><br/>" >> $DPATH/index.html
echo "You can download the slackget10 version which have been used to build this documentation  <a href='http://ftp.infinityperl.org/slack-get/Devel/slackget10-dev-${DATE}.tar.gz'>here</a> but remember that this is a hot DEV version and it is a possibly completly bugged version<br/><br/>" >> $DPATH/index.html
for k in `ls -1 lib/*.pm lib/slackget10/*.pm lib/slackget10/*/*.pm lib/slackget10/*/*/*.pm lib/slackget10/*/*/*/*.pm` ; do 
	FILE=`basename $k | sed -r s/.pm$//` ;
	DIR=`dirname $k | sed -r s?^lib/??`;
	if [ $DIR = 'lib' ]; then
		DIR=''
	fi
# 	echo "DIRNAME=$DIR"
	PM=`echo $DIR/$FILE | sed -r s/.pm$// | sed -r s?/?::?g`
# 	echo "PM=$PM"
	echo "<a href='$WEBPATH/$DIR/$FILE.html'><ul>$PM</ul></a><br/>" >> $DPATH/index.html
	echo "$POD2HTML --infile=$k --outfile=$DPATH/$DIR/$FILE.html --css=$CSS --backlink=$BACKLINK" ;
	mkdir -p $DPATH/$DIR
	$POD2HTML --infile=$k --outfile=$DPATH/$DIR/$FILE.html  --css=$CSS --backlink=$BACKLINK --htmlroot=$WEBPATH; 
done

echo "</body></html>" >> $DPATH/index.html
echo "Uploading on server"
scp -P 23 -r $DPATH arno@192.168.0.20:/home/virtual/www/slackget/
echo "+ Building TAR archive"
make realclean
perl Makefile.PL && make tardist && mv `ls slackget10-*.tar.gz` slackget10-dev-${DATE}.tar.gz
echo "+ Uploading archive on server"
scp -P 23 slackget10-dev-${DATE}.tar.gz arno@192.168.0.20:/home/virtual/ftp/slack-get/Devel