#! /bin/sh
# This script is generated by configure for the purpose of reporting on
# the port of MPICH to a new architecture.  This does ONLY the sections
# on building the libraries, and places them into docmake.tex
#
# To simplify testing and a few special cases, this script takes a few options
# -author="name"        Use instead of 'whoami'
# -nomake               Do not run make
# -noperf               Do not run performance tests
# -file=name            Output LaTeX file (default is doc/doc1.tex)
#
# If the make fails, exits with status 1; else exits with zero.
author="$LOGNAME"
nomake=0
noperf=0
filename="doc/doc1make.tex"
for arg in "$@" ; do
    case $arg in 
	-author=* )
	author="`echo $arg|sed -e 's/-author=//' -e 's/_/ /'`"
	;;
	-file=* )
	filebase=`echo $arg|sed 's/-file=//'`
        filename="${filebase}make.tex"
	;;
 	-nomake )
	nomake=1
	;;
 	-noperf )
	noperf=1
	;;
	-echo)
	set -x
	;;
	*)
	if test -n "$arg" ; then
	    echo "portbuild: Unknown argument ($arg)"
	    exit 1
 	fi
	;;
    esac
done
#
# Remove the old filename
/bin/rm -f $filename
#
############################################################################
# Some initial comments...
# sh is not reliable about processing redirects of stdin within if...else..fi
# For such cases, we place the output text within a routine, defined
# outside of the if..else..fi, and called conditionally.
############################################################################
#
############################################################################
# Do the build and comment on it...
############################################################################
# Next, we run the make and time used to do the port
starthour=`date +%H`
startmin=`date +%M`
makefailed=0
if test $nomake = 0 ; then
    make >make.log 2>&1
    if [ $? != 0 ] ; then
	makefailed=1
    fi
fi
endhour=`date +%H`
endmin=`date +%M`
if test $endhour -lt $starthour ; then
    endhour=`expr $endhour + 24`
fi
totaltime=`expr $endhour \* 60 + $endmin - $starthour \* 60 - $startmin`
hours=`expr $totaltime / 60`
mins=`expr $totaltime - $hours \* 60`
if test $mins -lt 10 ; then
    mins="0$mins"
fi
if test $hours -lt 2 ; then
    if test $hours -lt 1 ; then
        hours=""
    else
        hours="one hour"
    fi
else
    hours="$hours hours"
fi
if test $mins -lt 2 ; then
    if test $mins -lt 1 ; then
        mins=""
    else
        mins="one minute"
    fi
else
    mins="$mins minutes"
fi
if test -n "$hours" ; then
    mins="and $mins"
fi
if test $nomake = 0 ; then 
    cat >>$filename <<.
\section{Building MPICH}
MPICH was built using the \code{make --no-print-directory} program.
It took $hours $mins to build the MPI libraries.
.
    #
    # Extract the error messages from the make.log file
    /bin/rm -rf doc/doc1make.log
    util/finderrors make.log > doc/doc1make.log
    if test -s doc/doc1make.log ; then
        echo "\\par Error messages from make" >> $filename
	# This is the only PORTABLE way to get \b... into a file!!!
	cat >> $filename <<.
\begin{verbatim}
.
        cat doc/doc1make.log >> $filename
        cat >> $filename <<.
\end{verbatim}
.
    else
        echo "Make detected no errors during the build." >> $filename
    fi
    if test $makefailed = 1 ; then
        echo "The make failed to complete." >> $filename
	exit 1
    fi
else
    cat >>$filename <<.
\section{Building MPICH}
MPICH was not built using the the automated build and document program.
.
fi
exit 0
