#!/bin/sh
#
#	aegis - project change supervisor
#	Copyright (C) 1997-1999, 2001-2003 Peter Miller;
#	All rights reserved.
#
#	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 2 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.
#
#	You should have received a copy of the GNU General Public License
#	along with this program; if not, write to the Free Software
#	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
#
# MANIFEST: aegis.cgi.in
#
# @configure_input@
#
prefix=@prefix@
exec_prefix=@exec_prefix@
bindir=@bindir@
libdir=@libdir@
datadir=@datadir@

# architecture neutral... e.g. AEGIS_PATH
if test -r $datadir/aegis.cgi.conf; then
. $datadir/aegis.cgi.conf
fi

# architecture specific... e.g. PATH
if test -r $libdir/aegis.cgi.conf; then
. $libdir/aegis.cgi.conf
fi

extra=
lang=en
file=proj_list
for arg in $*
do
    case "$arg" in
    *\;* | -* | *=* | *@-* | *\ * )
       	;;
    *@*)
       	arg=`echo "$arg" | sed -e 's|@|=|'`
       	eval "$arg"
       	;;
    -*)
       	;;
    *)
       	extra="$extra $arg"
       	;;
    esac
done

Project="$project"
Change="$change"
test ! -z "$project" && project="--project=$project"
test ! -z "$change" && change="--change=$change"

if test "$file" = "rect"
then
    exec $bindir/aerect $extra
fi

if test "$file" = "aedist"
then
    test -z "$project" && exit 1
    test -z "$change" && change="--baseline"
    $bindir/aedist --send $project $change -ndh -naa \
		-o /tmp/aegis.$$.ae > /tmp/aegis.$$ 2>&1
    status=$?
    if test $status -ne 0
    then
	echo 'Content-Type: text/html'
	echo ''
	echo '<html><head><title>Error</title></head><body><h1>'
	echo 'Error</h1>The command'
	echo '<blockquote><tt>'
	echo $bindir/aedist --send $project $change
	echo '</tt></blockquote>'
	echo "terminated with exit status $status."
	echo 'The following text was produced <blockquote><pre>'
	sed 's|<|\&lt;|g' < /tmp/aegis.$$
	echo '</pre></blockquote>'
	echo '<hr>'
	echo 'This page was generated'
	date
	echo '</body></html>'
	rm /tmp/aegis.$$*
	exit 1
    fi

    cat /tmp/aegis.$$.ae
    rm /tmp/aegis.$$*
    exit 0
fi

if test "$file" = "aepatch"
then
    test -z "$project" && exit 1
    test -z "$change" && change="--baseline"
    $bindir/aepatch --send $project $change -naa \
       	-o /tmp/aegis.$$.ae > /tmp/aegis.$$ 2>&1
    status=$?
    if test $status -ne 0
    then
       	echo 'Content-Type: text/html'
       	echo ''
       	echo '<html><head><title>Error</title></head><body><h1>'
       	echo 'Error</h1>The command'
       	echo '<blockquote><tt>'
       	echo $bindir/aepatch --send $project $change
       	echo '</tt></blockquote>'
       	echo "terminated with exit status $status."
       	echo 'The following text was produced <blockquote><pre>'
       	sed 's|<|\&lt;|g' < /tmp/aegis.$$
       	echo '</pre></blockquote>'
       	echo '<hr>'
       	echo 'This page was generated'
       	date
       	echo '</body></html>'
       	rm /tmp/aegis.$$*
       	exit 1
    fi

    cat /tmp/aegis.$$.ae
    rm /tmp/aegis.$$*
    exit 0
fi

if test "$file" = "aetar"
then
    test -z "$project" && exit 1
    test -z "$change" && change="--baseline"
    $bindir/aetar --send $project $change \
       	-o /tmp/aegis.$$.ae > /tmp/aegis.$$ 2>&1
    status=$?
    if test $status -ne 0
    then
       	echo 'Content-Type: text/html'
       	echo ''
       	echo '<html><head><title>Error</title></head><body><h1>'
       	echo 'Error</h1>The command'
       	echo '<blockquote><tt>'
       	echo $bindir/aetar --send $project $change
       	echo '</tt></blockquote>'
       	echo "terminated with exit status $status."
       	echo 'The following text was produced <blockquote><pre>'
       	sed 's|<|\&lt;|g' < /tmp/aegis.$$
       	echo '</pre></blockquote>'
       	echo '<hr>'
       	echo 'This page was generated'
       	date
       	echo '</body></html>'
       	rm /tmp/aegis.$$*
       	exit 1
    fi

    echo 'Content-Type: application/x-tar-gz'
    echo 'Content-Transfer-Encoding: 8bit'
    echo ''
    cat /tmp/aegis.$$.ae
    rm /tmp/aegis.$$*
    exit 0
fi


if test "$file" = "diff"
then
    test -z "$project" && exit 1
    test -z "$extra" && exit 1

    File=$extra;

    cmdcmd="$bindir/aereport -f $datadir/en/html/file_diff.rpt $delta1 \
	$delta2 $File /tmp/aegis.$$.1 /tmp/aegis.$$.2 $project $change -unf"

    # the script will work out where to get the file from
    cmd="`$cmdcmd 2> /tmp/aegis.$$.log`";
    status=$?

    if test $status -ne 0
    then
       	echo 'Content-Type: text/html'
       	echo ''
       	echo '<html><head><title>Error</title></head><body><h1>'
       	echo 'Error</h1>The command'
       	echo '<blockquote><tt>'
       	echo $cmdcmd ;
       	echo '</tt></blockquote>'
       	echo "terminated with exit status $status."
       	echo 'The following text was produced <blockquote><pre>'
       	sed 's|<|\&lt;|g' < /tmp/aegis.$$.log
       	echo '</pre></blockquote>'
       	echo '<hr>'
       	echo 'This page was generated'
       	date
       	echo '</body></html>'
       	rm -f /tmp/aegis.$$.*
       	exit 1
    fi
    rm -f /tmp/aegis.$$.*

    eval "$cmd 2> /tmp/aegis.$$.log" > /tmp/aegis.$$.out
    status=$?
    if test $status -ne 0
    then
       	echo 'Content-Type: text/html'
       	echo ''
       	echo '<html><head><title>Error</title></head><body><h1>'
       	echo 'Error</h1>The command'
       	echo '<blockquote><tt>'
	echo $cmd
       	echo '</tt></blockquote>'
       	echo "terminated with exit status $status."
       	echo 'The following text was produced <blockquote><pre>'
       	sed 's|<|\&lt;|g' < /tmp/aegis.$$.log
       	echo '</pre></blockquote>'
       	echo '<hr>'
       	echo 'This page was generated'
       	date
       	echo '</body></html>'
       	rm -f /tmp/aegis.$$.*
       	exit 1
    fi
    rm /tmp/aegis.$$.log

    echo 'Content-Type: text/html'
    echo ''
    echo '<html><head><title>'
    echo "Project \"$Project\","
    test ! -z "$Change" && echo "Change $Change,"
    echo "Differences in file $File" | sed 's/</&lt;/'
    echo '</title></head><body><h1 align=center>'
    echo "Project \"$Project\","
    test ! -z "$Change" && echo "Change $Change,"
    echo '<br>'
    echo "Differences in file $File" | sed 's/</&lt;/'
    echo "<br>Delta $delta1, Delta $delta2"
    echo '</h1>'
    echo '<pre>'
    sed 's|<|\&lt;|g' < /tmp/aegis.$$.out
    echo '</pre>'
    echo '<hr>'
    echo 'This page was generated'
    date
    echo '</body></html>'
    rm -f /tmp/aegis.$$.*
    exit 0
fi

if test "$file" = "pre"
then
    test -z "$project" && exit 1
    test -z "$extra" && exit 1

    Delta="-delta $delta"
    test -z "$delta" && Delta="";

    File="$extra"
    if test -z "$change"
    then
       	$bindir/aegis -cp -ind $project -baserel $File $Delta \
	    -o /tmp/aegis.$$ 2> /tmp/aegis.$$.log
       	status=$?
       	if test $status -ne 0
       	then
	    echo 'Content-Type: text/html'
	    echo ''
	    echo '<html><head><title>Error</title></head><body><h1>'
	    echo 'Error</h1>The command'
	    echo '<blockquote><tt>'
	    echo $bindir/aegis -cp -ind -baserel "$Delta" $project $extra
	    echo '</tt></blockquote>'
	    echo "terminated with exit status $status."
	    echo 'The following text was produced <blockquote><pre>'
	    sed 's|<|\&lt;|g' < /tmp/aegis.$$.log
	    echo '</pre></blockquote>'
	    echo '<hr>'
	    echo 'This page was generated'
	    date
	    echo '</body></html>'
	    rm -f /tmp/aegis.$$ /tmp/aegis.$$.log
	    exit 1
       	fi
       	rm -f /tmp/aegis.$$.log
    else
       	# the script will work out where to get the file from
       	cmd="`$bindir/aereport -f $datadir/en/html/cp_command.rpt $File \
	    /tmp/aegis.$$ -unf $project $change 2> /tmp/aegis.$$.log`"

       	status=$?
       	if test $status -ne 0
       	then
	    echo 'Content-Type: text/html'
	    echo ''
	    echo '<html><head><title>Error</title></head><body><h1>'
	    echo 'Error</h1>The command'
	    echo '<blockquote><tt>'
	    echo $bindir/aereport -f \
	     	$datadir/en/html/cp_command.rpt $File \
	     	/tmp/aegis.$$ -unf $project $change
	    echo '</tt></blockquote>'
	    echo "terminated with exit status $status."
	    echo 'The following text was produced <blockquote><pre>'
	    sed 's|<|\&lt;|g' < /tmp/aegis.$$.log
	    echo '</pre></blockquote>'
	    echo '<hr>'
	    echo 'This page was generated'
	    date
	    echo '</body></html>'
	    rm /tmp/aegis.$$.log
	    exit 1
       	fi
       	rm /tmp/aegis.$$.log

       	eval "$cmd 2> /tmp/aegis.$$.log"
       	status=$?
       	if test $status -ne 0
       	then
	    echo 'Content-Type: text/html'
	    echo ''
	    echo '<html><head><title>Error</title></head><body><h1>'
	    echo 'Error</h1>The command'
	    echo '<blockquote><tt>'
	    echo $cmd | sed 's|<|\&lt;|g'
	    echo '</tt></blockquote>'
	    echo "terminated with exit status $status."
	    echo 'The following text was produced <blockquote><pre>'
	    sed 's|<|\&lt;|g' < /tmp/aegis.$$.log
	    echo '</pre></blockquote>'
	    echo '<hr>'
	    echo 'This page was generated'
	    date
	    echo '</body></html>'
	    rm -f /tmp/aegis.$$.log /tmp/aegis.$$
	    exit 1
       	fi
	rm /tmp/aegis.$$.log
    fi

    echo 'Content-Type: text/html'
    echo ''
    echo '<html><head><title>'
    echo "Project \"$Project\","
    test ! -z "$Change" && echo "Change $Change,"
    x=`echo $File | sed 's/</&lt;/'`
    echo "File $x"
    echo '</title></head><body><h1 align=center>'
    echo "Project \"$Project\","
    test ! -z "$Change" && echo "Change $Change,"
    echo "<br>File <i>$x</i>"
    echo '</h1>'
    echo '<pre>'
    sed 's|<|\&lt;|g' < /tmp/aegis.$$
    echo '</pre>'
    echo '<hr>'
    echo 'This page was generated'
    date
    echo '</body></html>'
    rm /tmp/aegis.$$
    exit 0
fi

$bindir/aereport --file $datadir/$lang/html/$file.rpt \
    $project $change $extra --page-width=1000 --unformatted \
    > /tmp/aegis.$$ 2>&1

status=$?
if test $status -ne 0
then
    echo 'Content-Type: text/html'
    echo ''
    echo '<html><head><title>Error</title></head><body><h1>'
    echo 'Error</h1>The command'
    echo '<blockquote><tt>'
    echo $bindir/aereport --file $datadir/$lang/html/$file.rpt \
       	$project $change $extra --page-width=1000 --unformatted
    echo '</tt></blockquote>'
    echo "terminated with exit status $status."
    echo 'The following text was produced <blockquote><pre>'
    sed 's|<|\&lt;|g' < /tmp/aegis.$$
    echo '</pre></blockquote>'
    echo '<hr>'
    echo 'This page was generated'
    date
    echo '</body></html>'
else
    cat /tmp/aegis.$$
fi
rm /tmp/aegis.$$
exit 0
