#!/bin/sh
#
# This script takes source files of amforth and call php script which
# creates HTML pages. The script may be run by hand on regulerly fox
# example once an hour or day.
#
# (c) miho 2007 / www.mlab.cz

# Configuration
# =============
# WorkingDir      - existing directory with rw access
# SvnPath         - path ro subversion server with source files
# SvnInfoFile     - name of file to put info about version in
# WebForthInPath  - name of directory with HTML templates and scripts
# WebForthOutPath - name of directory to put HTML pages in
WorkingDir=/home/miho/amforth
SvnPath=https://amforth.svn.sourceforge.net/svnroot/amforth/trunk
SvnInfoFile=.svn-info.txt
WebForthInPath=/var/www/Articles/Forth
WebForthOutPath=/var/www/Articles/Forth

# Start of script
# ===============
DateFile=/tmp/amforth.date
if [ -f $DateFile ]; then
    tail -n 400 $DateFile > $DateFile. 2> /dev/nul
    rm $DateFile 2> /dev/nul
    mv $DateFile. $DateFile 2> /dev/nul
fi
date >> $DateFile 2> /dev/nul

# Create Log file
# ===============
if [ ! -d $WorkingDir ]; then
    echo "--> ERROR: No Workig Directory"
    exit 1
fi
cd $WorkingDir
if [ ! -d $WorkingDir/Log ]; then
    mkdir $WorkingDir/Log 2> /dev/nul
    if (($?)); then
        echo "--> ERROR: Unable Create Working Directory"
        exit 1
    fi
fi
LogFile=$WorkingDir/Log/amforth.log
LogFileGenerate=$WorkingDir/Log/GenerateHTML.log
echo "$0" > $LogFile
echo "-------------------" >> $LogFile

# Remove export directory
# =======================
echo "Performing: remove export directory" | tee -a $LogFile
rm -rf $WorkingDir/source-exported | tee -a $LogFile 2>> $LogFile
if (($?)); then
    echo "--> ERROR: Unable Remove Export Directory" | tee -a $LogFile
    exit 1
fi
echo "-------------------" >> $LogFile

# svn checkout
# ============
echo "Performing: svn checkout" | tee $LogFile
svn co $SvnPath $WorkingDir/source >> $LogFile 2>> $LogFile
if (($?)); then
    echo "--> ERROR: svn commit" | tee -a $LogFile
    exit 1
fi
echo "-------------------" >> $LogFile

# svn export
# ==========
echo "Performing: svn export" | tee -a $LogFile
svn export $WorkingDir/source $WorkingDir/source-exported >> $LogFile 2>> $LogFile
if (($?)); then 
    echo "--> ERROR: svn export" | tee -a $LogFile
    exit 1
fi
echo "-------------------" >> $LogFile

# svn info
# ========
echo "Performing: svn info" | tee -a $LogFile
Result=`svn info $WorkingDir/source` 2>> $LogFile
echo "$Result" >> $LogFile
if (($?)); then
    echo "--> ERROR: svn export" | tee -a $LogFile
    exit 1
fi
echo "$Result" > $WorkingDir/source-exported/$SvnInfoFile
echo "-------------------" >> $LogFile

# Create HTML
# ===========
echo "Performing: GenerateHTML" | tee -a $LogFile
if [ ! -d $WorkingDir/HTML ]; then
    mkdir $WorkingDir/HTML 2>> $LogFile
fi
php $WebForthInPath/PHP/GenerateHTML.php \
    EnableApache="0" \
    SourceDir="$WorkingDir/source-exported/" \
    SourceAsm="amforth.asm" \
    TemplateDir="$WebForthInPath/Templates/" \
    DestinationDir="$WebForthOutPath/HTML/" \
    > $LogFileGenerate 2>> $LogFile
if (($?)); then
    echo "--> ERROR: GenerateHTML.php" | tee -a $LogFile
    exit 1
fi
echo "-------------------" >> $LogFile

# Copy result to Web Directory
# ============================
echo "Performing: copy HTML" | tee -a $LogFile
rm -rf $WebForthOutPath/HTML | tee -a $LogFile 2>> $LogFile
if (($?)); then
    echo "--> ERROR: Unable Remove HTML Directory" | tee -a $LogFile
    exit 1
fi
if [ ! -d $WebForthOutPath/HTML ] || [ -d $WebForthOutPath ]; then
        mkdir $WebForthOutPath/HTML | tee -a $LogFile 2>> $LogFile
fi
cp -r $WorkingDir/HTML/* $WebForthOutPath/HTML | tee -a $LogFile 2>> $LogFile
echo "-------------------" >> $LogFile
if (($?)); then
    echo "--> ERROR: Unable Copy HTML Directory" | tee -a $LogFile
    exit 1
fi

# Finish
# ======
echo "O.K."
echo "O.K." >> /tmp/amforth.date
exit 0