#!/bin/sh
# you need a shell that understands (( ... ))
# On irix sh==ksh, for Linux/Cygwin use bash
# reason: windows takes ages to create processes, and we need a=$a+1 a lot

# 
# script to automatically check out and build the whole OpenSG system 
# including tests and run them
#
# you need cvs in your path and doxygen/dot to make the docs
# search for LOCAL to see where it needs to be adapted to your local environment


# Command Line parsing

scriptname=$0
allargs=$@

extra_args=""
config_shell=sh
mail=echo
grep=grep

loginfo=""
make=make

# Are we restarting from a script update?
restarting=0

while test $# -gt 0 
do
    case $1 in
        --compiler)
            compiler=$2
            shift
            shift
            ;;
        --extra-args)
            extra_args="$extra_args $2"
            shift
            shift
            ;;
        --with-qt)
            qt_args="--with-qt=$2"
            qt_dir=$2
            shift
            shift
            ;;
        --with-glut)
            glut_args="--with-glut=$2"
            shift
            shift
            ;;
        --with-tif)
            tif_args="--with-tif=$2"
            shift
            shift
            ;;
        --with-jpg)
            jpg_args="--with-jpg=$2"
            shift
            shift
            ;;
        --with-png)
            png_args="--with-png=$2"
            shift
            shift
            ;;
        --par_jobs)
            par_jobs=$2
            shift
            shift
            ;;
        --upload-key )        
            do_copy="cd \$basedir; tar cf - \$out_files* | ssh -i $2 -l osgdb www.opensg.org tar xf - -C dailybuild_logs"
            shift
            shift
            ;;
        --no-doc )
            has_doxygen=0
            shift
            ;;
        --restart )
            restarting=1
            shift
            ;;
        --dist-name-suffix)
            suffix=.$2
            shift
            shift
            ;;
         *) 
            break 2
            ;;
    esac
done

# Script bootstrapping. Do this as early as possible to avoid problems with syntax errors in function defs

if test $restarting == 0
then
    # Get the latest version from the website

    wget -q http://www.dirkreiners.com/osgdb/dailybuild.new

    # check if the dailybuild script has changed and update myself
    if test -f dailybuild.new 
    then
        if ! diff -q $scriptname dailybuild.new >> /dev/null;
        then
            echo "dailybuild changed, restarting..."
            (chmod a+x dailybuild.new && mv dailybuild.new $scriptname && /bin/bash $scriptname --restart $allargs)
            exit 0
        else
            rm -f dailybuild.new
        fi
    else
        echo "Error downloading new dailybuild script, using old one!"
    fi
fi

# utility functions

cleanup ()
{
    write_html_tail $out_html.Main.html

    # compress the HTML files, as they tend to get pretty big
    gzip -9f $out_html*.html
    
    eval $do_copy 
    
    rm -fr $tmpdir
}

errorexit () 
{
    subsys=$1
    errfile=$2

    echo "<br><br>Errors found, dailybuild aborted!" >> $out_html.Main.html
    
    send_mail $subsys $errfile
    
    cleanup
    exit 1
}

normalexit ()
{
    cleanup
    exit 0
}

usage ()
{
    cat << EOF

Usage:    `basename $scriptname` -h

Options:
    -h        display this message

EOF
}

write_html_head ()
{
    outfile="$1"

    cat << EOF > $outfile
<html>
<head>
<title>OpenSG dailybuild $now on $host results</title>
</head>
<body>
<h1>OpenSG dailybuild $now on $host results</h1>
<p>
`date +%H:%M`: Start<br><br>
Dailybuild args: $allargs<br>
System: $system<br>
Compiler: $compiler <br>
Configuration: --with-compiler=$compiler $stl_args $glut_args $qt_args $tif_args $jpg_args $png_args $extra_args<br>
EOF
}

write_html_tail ()
{
    cat << EOF >> $1
<br><br>
`date +%H:%M`: Finish
</body>
</html>
EOF
}

send_mail ()
{
    subsys=$1
    errfile=$2
    cat << EOF > $out_mail
System: $system
Compiler: $compiler
Log: http://www.opensg.org/dailybuild_logs/$link_html.Main.html.gz

Failed in $subsys.

EOF

    if test -s $errfile;
    then
        cat $errfile >> $out_mail
    echo  >> $out_mail
    fi
    
    # LOCAL: choose your email adresses here
    $mail -s "OpenSG dailybuild on $host failed" opensg-dailybuilds@lists.sf.net < $out_mail
}

write_html_log ()
{
    file="$1"
    title=$2
    log=$3
    errfile=$4
    warnfile=$5
 
    cat << EOF > $file
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>OpenSG dailybuild $now $title on $host results</title>
<style type="text/css">
<!--
body            { background-color:#ffffff }
td.line         { text-align:right; vertical-align:top }
td.log          { font-family:monospace; vertical-align:top }
tr.normal       { }
tr.error        { background-color:#ff2020; color:#ffffff }
tr.warning      { background-color:#ff6600 }
//-->
</style>
</head>
<body>
<h1>OpenSG dailybuild $now $title on $host results</h1>
<p>
<a href=http://www.opensg.org/dailybuild_logs/$link_html.Main.html.gz>
Back to main log</a>
</p><p>
System: $system<br>
Compiler: $compiler<br>
</p>
EOF

    # This used to be done in the shell, but it just took too long,
    # especially on Windows.
    
    perl $tmpdir/analyze_log.pl $log $errfile $warnfile >> $file
 
    cat << EOF >> $file
</table>
</body>
</html>
EOF
}


checkerror ()
{
    title="$1"
    file="$2"
    do_exit="$3"
    
    retval=0
    
    # remove the expected errors
    # KLUDGE: for some unknown reason some <nul>s make it into the 
    # log file on MacOS. Remove them before they confuse grep.
    tr -d \0 < $file | $grep -n -i -w error | \
    $grep -v \
        -e "warning: nm returned a nonzero error status" \
        -e "GetLastError" \
        -e "Error (future)" \
        -e "(ignored)" \
        -e "error-type" \
        -e "error-constant" \
        -e "ERROR..." \
    > $log_errors || true
    tr -d \0 < $file | $grep -n -i -w failed | \
    $grep -v \
        -e "WIN32Window::init: failed" \
        -e "WIN32Window::activate: failed" \
        -e "inlining failed" \
        -e "failed in call to" \
        -e "failed: Connection timed out" \
        -e "Failed to create pthread" \
    >> $log_errors || true

    sort -un < $log_errors > ${log_errors}.2
    mv ${log_errors}.2 $log_errors
    
    # remove the expected warnings
    tr -d \0 < $file | $grep -n -i -w warning | \
    $grep -v \
        -e "not used for resolving any symbol" \
     > $log_warnings || true

    # write the log file
    write_html_log $out_html.$title.html $title $file $log_errors $log_warnings

    # check error file (empty -> ignore)
    if test \! -s $log_errors 
    then
    rm -f $log_errors
    echo "`date +%H:%M`: <a href=$link_html.$title.html.gz>$title</a> finished sucessfully.<br>" >> $out_html.Main.html
    else
     echo "`date +%H:%M`: <br><font color=#b00000><a href=$link_html.$title.html.gz>$title</a> had errors!</font><br>" >> $out_html.Main.html

    IFS=':'
    cat $log_errors | while read line err
    do
        echo "<a href=$link_html.$title.html.gz#$line>$line</a> : $err<br>" >> $out_html.Main.html
    done
    IFS=''

        if test x$do_exit != xno
        then
        errorexit $title $log_errors
        else
            false
            return
        fi
    fi
    
    true
}

# Main script starts here

# setup: error handling etc.

##set -e
##trap errorexit  ERR HUP INT QUIT
##trap normalexit EXIT

tmpdir=/tmp/osgdb.$$
mkdir $tmpdir
basedir=`pwd`

# Init perl script for log analysis
# Need it in this script, as it might be needed before CVS worked

cat > ${tmpdir}/analyze_log.pl << \EOF
#!/bin/perl

$logfile=$ARGV[0];
$errfile=$ARGV[1];
$warnfile=$ARGV[2];

@elist=();

if ( -r $errfile )
{
    print "<h4>Errors:</h4>\n";
    print "<table border=0>\n";

    open(IN, $errfile);

    while (<IN>)
    {
        /:/;
        $line = $`;
        $err  = $';
        $err  =~ s/\\/&#92/g;
        chomp($err);
       
        push(@elist, $line);
        
        print '<tr class="normal"><td class="line">';
        print "<a href=#$line>$line</a></td>";
        print "<td class=\"log\"> $err </td></tr>\n";       
    }
    
    print "</table>";
}
push(@elist, 0);

@wlist=();

if ( -r $warnfile )
{
    print "<h4>Warnings:</h4>\n";
    print "<table border=0>\n";

    open(IN, $warnfile);

    while (<IN>)
    {
        /:/;
        $line = $`;
        $warn = $';
        $warn =~ s/\\/&#92/g;
        chomp($warn);
        
        push(@wlist, $line);
        
        print '<tr class="normal"><td class="line">';
        print "<a href=#$line>$line</a></td>";
        print "<td class=\"log\"> $warn </td></tr>\n";       
    }
    
    print "</table>";
}
push(@wlist, 0);
 
print "<h4>Log:</h4>\n";
print "<table border=0>\n";

$acterr  = shift @elist;
$actwarn = shift @wlist;
$actline = 1;

open(IN, $logfile);
while (<IN>)
{
    if ( $actline == $acterr )
    {
        print '<tr class="error">';
        $acterr = shift @elist;
        if ( $actline == $actwarn )
        {
            $actwarn = shift @wlist;
        }
    }
    elsif ( $actline == $actwarn )
    {
        print '<tr class="warning">';
        $actwarn = shift @wlist;
    }
    else
    {
        print '<tr class="normal">';
    }

    chomp($_);
    $_ =~ s/&/&amp;/g;
    $_ =~ s/</&lt;/g;
    
    print "<td class=\"line\">$actline</td> <td class=\"log\">";
    print "<a name=\"$actline\">$_</a></td></tr>\n";

    $actline = $actline + 1;
}    
EOF

# ' Just to keep the syntax highlighting happy



# LOCAL change to your local directory
log_destination=/igd/a4/www/opensg/dailybuild_logs/

log_cvs=$tmpdir/opensg.cvs
log_configure=$tmpdir/opensg.configure
log_make=$tmpdir/opensg.make
log_test=$tmpdir/opensg.test
log_errors=$tmpdir/opensg.errors
log_warnings=$tmpdir/opensg.warnings
log_install=$tmpdir/opensg.install
log_dist=$tmpdir/opensg.dist
log_doc=$tmpdir/opensg.doc
out_mail=$tmpdir/opensg.mail
doc_flag=$tmpdir/opensg.doc_done

system="Not set yet"
host=`hostname`

# Check out the sources

#cleanup what's left
rm -rf OpenSG

# cvs needs to be in the path!
export CVS_RSH=ssh
rm -f $log_cvs

waittime=10
repeats=15

until cvs -z9 -ddirk@opensg.cvs.sourceforge.net:/cvsroot/opensg co -P OpenSG >> $log_cvs 2>&1
do
    echo "CVS problem, sleeping $waittime seconds and retrying!" >> $log_cvs
    sleep $waittime
    (( waittime = $waittime * 14 / 10 ))
    (( repeats = $repeats - 1 ))
    if test $repeats == 0
    then
        echo "CVS failed!" > $log_errors
        exit 1
    fi
done

# find the right parameters for this system

cd OpenSG
guess=`CommonConf/config.guess`
system=`CommonConf/config.sub $guess`
version=`cat VERSION`

# LOCAL: set the system specific params. 
# PATH needs to include doxygen and dot for doc to work

case $system in

    mips-sgi-irix6* )    

        compiler=${compiler:=CC}
        
        case "$compiler" in
        
            CC64)
                 # VRAC Irix 64 settings
          
                qt_dir=${qt_dir:="/home/users/dreiners/qt-64"}
                qt_args=${qt_args:="--with-qt=${qt_dir}"}
                glut_args=${glut_args:="--with-glut --with-glutincludedir=/usr/freeware/include/ --with-glutlibdir=/usr/freeware/lib64"}
                tif_args=${tif_args:="--with-tif --with-tifincludedir=/usr/freeware/include/ --with-tiflibdir=/usr/freeware/lib64"}
                jpg_args=${jpg_args:="--with-jpg --with-jpgincludedir=/usr/freeware/include/ --with-jpglibdir=/usr/freeware/lib64"}
                png_args=${png_args:="--with-png --with-pngincludedir=/usr/freeware/include/ --with-pnglibdir=/usr/freeware/lib64"}
                par_jobs=${par_jobs:=8}
                mail=/usr/sbin/Mail
                make=/home/users/dreiners/bin/IRIX64/make
                grep=/usr/freeware/bin/grep
                export PATH=${qt_dir}/bin/:$PATH
                loginfo="OS: `/bin/uname -aR` <br>"
                loginfo="$loginfo Compiler Version: `CC -version 2>&1` <br>"
                has_doxygen=${has_doxygen:=0}
                ;;           
        
            CC)
                 # VRAC Irix settings
          
                qt_dir=${qt_dir:="/home/users/dreiners/qt-32"}
                qt_args=${qt_args:="--with-qt=${qt_dir}"}
                glut_args=${glut_args:="--with-glut --with-glutincludedir=/usr/freeware/include/ --with-glutlibdir=/usr/freeware/lib32"}
                tif_args=${tif_args:="--with-tif --with-tifincludedir=/usr/freeware/include/ --with-tiflibdir=/usr/freeware/lib32"}
                jpg_args=${jpg_args:="--with-jpg --with-jpgincludedir=/usr/freeware/include/ --with-jpglibdir=/usr/freeware/lib32"}
                png_args=${png_args:="--with-png --with-pngincludedir=/usr/freeware/include/ --with-pnglibdir=/usr/freeware/lib32"}
                par_jobs=${par_jobs:=4}
                mail=/usr/sbin/Mail
                make=/home/users/dreiners/bin/IRIX64/make
                grep=/usr/freeware/bin/grep
                export PATH=${qt_dir}/bin/:$PATH
                loginfo="OS: `/bin/uname -aR` <br>"
                loginfo="$loginfo Compiler Version: `CC -version 2>&1` <br>"
                has_doxygen=${has_doxygen:=0}
                ;;           
        esac
        ;;

    hppa2.0w-hp* )    

        qt_dir=${qt_dir:="/igd/a4/software/HP-UX/packages/qt-x11-free-3.2.0b1"}
        qt_args=${qt_args:="--with-qt=${qt_dir}"}
        glut_args=${glut_args:="--with-glut --with-glutincludedir=/opt/graphics/OpenGL/contrib/libglut --with-glutlibdir=/opt/graphics/OpenGL/contrib/libglut"}
        tif_args=${tif_args:="--with-tif=/igd/a4/software/HP-UX/"}
        jpg_args=${jpg_args:="--with-jpg=/igd/a4/software/HP-UX/"}
        png_args=${png_args:="--with-png=/igd/a4/software/HP-UX/"}
        par_jobs=${par_jobs:=2}
        compiler=${compiler:=aCC}
        mail=/usr/bin/mailx
        # Need a decent grep here
        grep=/igd/a4/software/HP-UX/bin/grep
        export PATH=/igd/a4/software/HP-UX/bin/:$PATH
        loginfo="OS: `/bin/uname -a` <br>"
        loginfo="$loginfo Compiler Version: `aCC --version 2>&1` <br>"
        # Move HP-toxic stuff out of the way
        Common/prep_hp
        ;;
    
    *-pc-linux-gnu )    
        
        qt_dir=${qt_dir:="/usr/lib/qt3"}
        #qt_dir="/igd/a4/software/Linux/packages/qt-x11-commercial-3.1.2"
        qt_args=${qt_args:="--with-qt=${qt_dir}"}
        glut_args=${glut_args:="--with-glut"}
        tif_args=${tif_args:="--with-tif"}
        jpg_args=${jpg_args:="--with-jpg"}
        png_args=${png_args:="--with-png"}
        par_jobs=${par_jobs:=2}
        compiler=${compiler:=g++}
        config_shell=bash
        mail=/usr/bin/Mail
        export PATH=/igd/a4/software/Linux/bin/:$PATH
        loginfo="OS: `uname -a` (`cat /proc/version`) <br>"
        vopt="-v"
        if test $compiler = "icc";
        then
            vopt="-V"
        fi
        loginfo="$loginfo Compiler Version: `$compiler $vopt 2>&1` <br>"
        ;;

    *-pc-cygwin )    

        # Unpack the Supportlibs and use them
        mkdir ${basedir}/supportlibs >> $log_cvs 2>&1
        pushd ${basedir}/supportlibs >> $log_cvs 2>&1
        rm -rf * >> $log_cvs 2>&1
        unzip -o ../OpenSG/dist/win/supportlibs.zip >> $log_cvs 2>&1
        popd >> $log_cvs 2>&1

        # Makes ure we pick up cygwin commands before system commands
        # convert is a problem here
        export PATH=/usr/bin:$PATH
        
        glut_args=${glut_args:="--with-glut=`pwd`/../supportlibs"}
        tif_args=${tif_args:="--with-tif=`pwd`/../supportlibs"}
        jpg_args=${jpg_args:="--with-jpg=`pwd`/../supportlibs"}
        png_args=${png_args:="--with-png=`pwd`/../supportlibs"}
        qt_dir=${qt_dir:="`pwd`/../packages/qt-3.36"}
        if test ! -d $qt_dir
        then
            qt_dir=""
        fi
        qt_args=${qt_args:="--with-qt=${qt_dir}"}
        par_jobs=${par_jobs:=1}
        compiler=${compiler:=icl50}
        do_copy=${do_copy:='cd $basedir; tar cf - $out_files* | ssh -i /home/reiners/.ssh/id_dsa reiners@pc1107 tar xf - -C $log_destination'}
        config_shell=bash
        mail=`pwd`/../do_send_mail
        loginfo="OS: `uname -a` <br>"
        ;;

    ia64-unknown-linux-gnu )    
        
        glut_args=${glut_args:="--with-glut"}
        tif_args=${tif_args:="--with-tif"}
        jpg_args=${jpg_args:="--with-jpg"}
        qt_dir=${qt_dir:="/opt"}
        qt_args=${qt_args:="--with-qt=${qt_dir}"}
        png_args=${png_args:="--with-png"}
        par_jobs=${par_jobs:=2}
        compiler=${compiler:=g++}
        config_shell=bash
        mail=/usr/bin/Mail
        do_copy=${do_copy:='cd $basedir; tar cf - $out_files* | ssh -i /home/osgdb/.ssh/id_dsa reiners@pc1107 tar xf - -C $log_destination'}
        loginfo="OS: `uname -a` <br>"
        loginfo="$loginfo    `cat /proc/version` <br>"
        loginfo="$loginfo Compiler Version: `$compiler -v 2>&1` <br>"
        ;;

    *-apple-darwin* )    

        glut_args="--with-glut"
        par_jobs=${par_jobs:=1}
        compiler=${compiler:=cc}
        config_shell=sh
        mail=/usr/bin/Mail
        export PATH=$PATH
        loginfo="OS: `uname -a` <br>"
        # get rid of version number
        system=`echo $system | sed -e 's/darwin[0-9].[0-9]/darwin/' | sed -e 's/darwin.[0-9]/darwin/'`
        ;;

    x86_64-unknown-linux-gnu )    
        
        glut_args=${glut_args:="--with-glut"}
        tif_args=${tif_args:="--with-tif"}
        jpg_args=${jpg_args:="--with-jpg"}
        qt_dir=${qt_dir:="/opt"}
        qt_args=${qt_args:="--with-qt=${qt_dir}"}
        png_args=${png_args:="--with-png"}
        par_jobs=${par_jobs:=2}
        compiler=${compiler:=g++}
        config_shell=bash
        mail=/usr/bin/Mail
        do_copy=${do_copy:='cd $basedir; tar cf - $out_files* | ssh -i /home/osgdb/.ssh/id_dsa reiners@pc1107 tar xf - -C $log_destination'}
        loginfo="OS: `uname -a` <br>"
        loginfo="$loginfo    `cat /proc/version` <br>"
        loginfo="$loginfo Compiler Version: `$compiler -v 2>&1` <br>"
        ;;
    
   *)

        echo "Unknown system $system!"
        errorexit System /dev/null
        ;;
    
esac

# Only some machines can write the Web files, scp via one of them unless alternative set
do_copy=${do_copy:='cd $basedir; tar cf - $out_files* | ssh reiners@pc1107 tar xf - -C $log_destination'}

# set defaults if not set yet
suffix=${suffix:=""}
qt_args=${qt_args:=""}
glut_args=${glut_args:=""}
tif_args=${tif_args:=""}
jpg_args=${jpg_args:=""}
png_args=${png_args:=""}
par_jobs=${par_jobs:=1}

# Add QT dir to PATH & LD_LIBRARY_PATH
export PATH="${qt_dir}/bin:$PATH"
export LD_LIBRARY_PATH="${qt_dir}/lib:$LD_LIBRARY_PATH"

# Start output
# do it here so that $compiler and $system are set

now=`date '+%d.%m.%y %H:%M'`
today=`date '+%y%m%d'`

build_id=$today.$system.$compiler$suffix
out_files=opensg_dailybuild.$today
link_html=opensg_dailybuild.$build_id
out_html=$basedir/$link_html

rm -f $out_html.Main.html
write_html_head $out_html.Main.html 
echo $loginfo >> $out_html.Main.html 
echo '<p>' >> $out_html.Main.html 

if test $restarting == 0
then
    checkerror "Cvs" $log_cvs
fi

# create the unix source snapshot
# Create on linux, as needed for the SRPM

case $system in

    *-linux-* ) 
                        cd ..
                        tar cf - OpenSG | \
                            gzip -9f > $basedir/opensg_dailybuild.$today.source.tgz
                        cd OpenSG
                        ;;
esac

# configure it

## autoconf    removed for now, version problems
$config_shell ./configure --prefix=BUILD --with-compiler=$compiler $stl_args \
    $glut_args $qt_args $tif_args $jpg_args $png_args $extra_args 1>$log_configure 2>&1
checkerror "Configure" $log_configure

# start making the doc
# takes forever, do it in parallel
if type doxygen > /dev/null 2>&1
then
    has_doxygen=${has_doxygen:=1}
    if type latex > /dev/null 2>&1
    then
        has_latex=${has_latex:=1}
    fi

    if test x$has_latex = x1;
    then
        doxy_target="alldocs"
    else
        doxy_target="userdoc devdoc"
    fi
fi

if test x$has_doxygen = x1;
then
    ( rm -f $doc_flag ; echo $make $doxy_target 1>$log_doc 2>&1; $make $doxy_target 1>>$log_doc 2>&1 ; touch $doc_flag ) &
fi

# make the libs

cd Builds/$system-$compiler
export OSGSUBPARJOBS=$par_jobs

# create the windows source snapshot

case $system in


    *-pc-cygwin )    
                        # Need to make the FlexLexer.h
                        cd SystemLib
                        make ../Base/FlexLexer.h
                        cd .. 
                        # Make the dsps
                        $make dsp >> $log_install 2>&1
                        $make dsp7 >> $log_install 2>&1
                        cd ../../..
                        tar cf - --exclude OpenSG/Builds OpenSG | \
                            gzip -9f > $basedir/opensg_dailybuild.$today.win-source.tgz
                        cd OpenSG/Builds/$system-$compiler
                        ;;
esac

$make dbg 1>$log_make 2>&1
checkerror "DebugLibs" $log_make

$make opt 1>$log_make 2>&1
checkerror "OptLibs" $log_make

# locally install the libs

$make install >> $log_install 2>&1
export LD_LIBRARY_PATH="`pwd`/lib/dbg:$LD_LIBRARY_PATH"

# create the snapshot
case $system in

    *-pc-cygwin )    
                        # Include the .pdb files
                        mkdir pdb 
                        find . -name '*.pdb' | grep -v 'vc.*\.pdb' | xargs --replace mv {} pdb
                        tar cf - bin lib pdb include | gzip -9f > $out_html.snapshot.tgz
                        ;; 
    * )    
                        tar cf - bin lib include | gzip -9f > $out_html.snapshot.tgz
                        ;;
esac

checkerror "Install" $log_install


# make and run the tests that have gold test results
errcount=0

for i in *Test; 
do
    cd $i
    # keep going after errors to catch them all
    $make -j$par_jobs -k dbg 1>$log_test.$i 2>&1
    $make -j$par_jobs -k opt 1>>$log_test.$i 2>&1
    checkerror $i $log_test.$i no
    (( errcount = $errcount + $? ))
    
# We have no goldfiles for anything, so don't bother trying...
#    echo "Running ${i}s:<br>" >> $out_html.Main.html
#    # do we have testfiles?
#    if test `\ls -1 test* 2>/dev/null | wc -l` -ne 0 ;
#    then
#        for j in test*;
#        do
#            # find the gold file
#            echo -n "$j... " >> $out_html.Main.html
#            tloc=`find ../../.. -name $j.cpp | sed -e 's|[^/]*$||'`goldtests/$j.log
#            if test "$tloc" != "goldtests/$j.log" -a -r $tloc
#            then
#                $j > $log_test 2>&1
#                if test "`diff -q $log_test $tloc`" != "" ;
#                then
#                    echo " results differ:<br>" >> $out_html.Main.html
#                    echo -n "<pre>" >> $out_html.Main.html
#                    diff $log_test $tloc >> $out_html.Main.html
#                    echo "</pre>" >> $out_html.Main.html
#                else
#                    echo " ok.<br>" >> $out_html.Main.html
#                fi
#            else
#                echo " no gold test.<br>" >> $out_html.Main.html    
#            fi 
#        done
#    fi
    # free some space, OpenSG is big enough as is...
    $make distclean 1>>$log_test.$i 2>&1
    cd ..

done

# Wait for the docs to finish and copy them up
if test x$has_doxygen == x1 ;
then
     
    while ! test -r $doc_flag ; do sleep 30; done

    cd $basedir/OpenSG/Doc/Code
    
    case $system in

        *cygwin* ) 
            
            # This should really try to find the program. Works for now...
            /cygdrive/c/Program\ Files/HTML\ Help\ Workshop/hhc.exe user/html/index.hhp 1>>$log_doc 2>&1
            mv user/html/index.chm $out_files.OpenSG-$version-userdocs.chm
            # disabled for now, gives errors I don't have time to look for!!! *DR*
            #/cygdrive/c/Program\ Files/HTML\ Help\ Workshop/hhc.exe dev/html/index.hhp 1>>$log_doc 2>&1
            #mv dev/html/index.chm $out_files.OpenSG-$version-devdocs.chm
            ;;
        
        *)
            # Remove the CHM proto-files          
            rm user/html/index.hh* dev/html/index.hh* 
            ;;
    esac
    
    mv user/html OpenSG-$version-userdocs
    mv dev/html OpenSG-$version-devdocs
    
    tar cf $out_files.OpenSG-$version-userdocs.tar OpenSG-$version-userdocs
    tar cf $out_files.OpenSG-$version-devdocs.tar OpenSG-$version-devdocs
    
    gzip -9 $out_files.OpenSG-$version-userdocs.tar $out_files.OpenSG-$version-devdocs.tar
    
    if test x$has_latex == x1 ;
    then
        mv user/latex/StarterGuide.pdf $out_files.OpenSG-$version-StarterGuide.pdf
    fi

    # Move them up to be picked up by the dailybuild copy
    mv $out_files.* $basedir

    echo "`date +%H:%M`: Doc finished" >> $log_doc

    checkerror "Doc" $log_doc
fi

# make the binary dist

# no need to do these, done already
export OSG_DIST_NO_RECONFIGURE=yes
export OSG_DIST_NO_REBUILD=yes
export OSG_DIST_NO_REINSTALL=yes
export OSG_DIST_CLEANUP=yes

case $system in

    *cygwin* )    
        ;;
esac

if test $errcount -gt 0
then
    errorexit Tests NOFILE
fi

if test "x$qt_dir" != x -a -d "$qt_dir"
then
    qt_opt="--qt $qt_dir"
else
    qt_opt=""
fi

if test "x$qt_opt" = x
then
    export OSG_DIST_NO_EXAMPLEBUILD=1
    export OSG_DIST_NO_FCDEDIT=1
    echo Warning: qt_dir not found, skipping example and fcdEdit build for dist > $log_dist 2>&1
fi

cd $basedir
echo /bin/sh OpenSG/dist/make_bindist --compiler $compiler $qt_opt --today $today > $log_dist 2>&1
/bin/sh OpenSG/dist/make_bindist --compiler $compiler $qt_opt --today $today >> $log_dist 2>&1

checkerror "Dist" $log_dist

for i in OpenSG-$version-*
do
    mv $i $link_html.$i
done

# all done

echo "<br>All done, all right. Have a nice day." >>  $out_html.Main.html

cleanup
