#!/bin/sh
##
##  PORTING.test -- check if WML builds and runs current
##  Copyright (c) 1998,1999 Ralf S. Engelschall, All Rights Reserved. 
## 

echon () {
    echo "$*" | awk '{ printf("%s", $0); }'
}

##
##  Determine platform parameters
##

#
#  WML Version [wmlvers]
#
wmlvers=`etc/newvers -l txt -d VERSION`
echo "This is Website META Language Platform Porting Test, Version $wmlvers"
echo "Copyright (c) 1996,1997,1998,1999 Ralf S. Engelschall."
echo ""

#
#   build user [builduser]
#
username="$LOGNAME"
if test ".$username" = .; then
    username="$USER"
    if test ".$username" = .; then
        username="`whoami 2>/dev/null | sed -e 's/\n$//'`"
        if test ".$username" = .; then
            username="`who am i 2>/dev/null | cut '-d ' -f1`"
            if test ".$username" = .; then
                username="unknown"
            fi
        fi
    fi
fi
hostname="`uname -n | sed -e 's/\n$//'`"
if test ".$hostname" = .; then
    hostname="`hostname | sed -e 's/\n$//'`"
    if test ".$hostname" = .; then
        hostname="unknown"
    fi
fi
hostname="`echo $hostname | sed -e 's/\..*//'`"
if test -f /etc/resolv.conf; then
    domainname="`grep domain /etc/resolv.conf | sed -e 's/.*domain//' -e 's/^[ 	]*//' -e 's/^  *//' -e 's/^\.//' -e 's/^/./'`"
    if test ".$domainname" = .; then
        domainname="`grep search /etc/resolv.conf | sed -e 's/.*search//' -e 's/^[ 	]*//' -e 's/^  *//' -e 's/ .*//' -e 's/^\.//' -e 's/^/./'`"
    fi
else
    domainname="";
fi
builduser="$username@$hostname$domainname"
echon "Built User [$builduser]: "
read rc
case $rc in
    "" ) ;;
    *  ) builduser=$rc ;;
esac

#
#   build date [builddate]
#
builddate="`date '+%d-%m-%Y' | sed -e 's/\n$//'`"
builddate="`date '+%d-%b-%Y' 2>/dev/null | sed -e 's/\n$//'`"
if [ ".$builddate" = . ]; then
    builddate="`date '+%d-%m-19%y' | sed -e 's/\n$//'`"
fi
echon "Build Date [$builddate]: "
read rc
case $rc in
    "" ) ;;
    *  ) builddate=$rc ;;
esac

#
#   GNU platform triple [gnutriple]
#
gnutriple=`etc/config.guess | sed -e 's/\n$//'`
echon "GNU Platform Triple [$gnutriple]: "
read rc
case $rc in
    "" ) ;;
    *  ) gnutriple=$rc       ;;
esac

#
#   Perl 5 Interpreter [perl/perlvers]
#
TMPFILE=/tmp/wml.$$.tmp
rm -f $TMPFILE
touch $TMPFILE
c=0
for dir in `echo $PATH | sed -e 's/:/ /g'` /tmp; do
    for perl in perl5 perl miniperl; do
         if test -f "$dir/$perl"; then
             if test -x "$dir/$perl"; then
                 perl="$dir/$perl"
                 version=`$perl -v | grep version | sed -e 's/.* version //' -e 's/ built.*//' -e 's/ with.*//'`
                 versionnum="`echo $version | sed -e 's/\.//g' -e 's/_//g'`"
                 versionnum=`expr $versionnum - $c`
                 echo "$versionnum $version $perl" >>$TMPFILE
             fi
         fi
    done
    c=`expr $c + 1`
done
perlvers="`cat $TMPFILE | sort -u | tail -1 | cut '-d ' -f2`"
perlprog="`cat $TMPFILE | sort -u | tail -1 | cut '-d ' -f3`"
rm -f $TMPFILE
echon "Perl Interpreter ($perlvers) [$perlprog]: "
read rc
case $rc in
    "" ) perl=$perlprog ;;
    *  ) perl=$rc       ;;
esac
perlvers=`$perl -v | grep version | sed -e 's/.* version //' -e 's/ built.*//' -e 's/ with.*//'`

#
#   ANSI C Compiler [cc/ccvers]
#
ccprog=`$perlprog -e 'use Config; print $Config{cc}'`
ccvers=`$ccprog -v 2>&1 | grep "gcc version" | awk '{ print $3; }'`
case $ccvers in
   "" ) ccvers="Vendor" ;;
   *  ) ccvers="GNU $ccvers" ;;
esac
echon "ANSI C Compiler ($ccvers) [$ccprog]: "
read rc
case $rc in
    "" ) cc=$ccprog ;;
    *  ) cc=$rc     ;;
esac
ccvers=`$cc -v 2>&1 | grep "gcc version" | awk '{ print $3; }'`
case $ccvers in
   "" ) ccvers="Vendor" ;;
   *  ) ccvers="GNU $ccvers" ;;
esac
cc="`echo $cc | sed -e 's/\(.* .*\)/\"\1\"/'`"


#
#  GNU Autoconf arguments [confargs]
#
wmlconf="--prefix=/tmp/wml-test --with-cc=$cc --with-perl=$perl"
wmlconfadd=""
echon "Additional GNU Autoconf options [$wmlconfadd]: "
read rc
case $rc in
    "" ) ;;
    *  ) wmlconfadd=$rc ;;
esac
wmlconf="$wmlconf $wmlconfadd"

#
#  construct entry for PORTING file
#
entry="$wmlvers : $perlvers : $ccvers : $gnutriple : $builduser : $builddate : $wmlconf"


##
##  GIVE SUMMARY INFORMATION 
##
echo ""
echo "  Summary of Porting Test Parameters:"
echo ""
echo "  Build User            : $builduser"
echo "  Build Date            : $builddate"
echo "  Perl 5 Interpreter    : $perlvers [$perl]"
echo "  ANSI C Compiler       : $ccvers [$cc]"
echo "  Platform              : $gnutriple"
echon "  Autoconf Options      : "
echo $wmlconf | $perl -e '$x = <STDIN>; $x =~ s|\s+--|\n                          --|sg; print $x;'
echo ""

##
##  TEST SUITE
##

rm -rf /tmp/wml-test 2>/dev/null || true
echo "$ ./configure $wmlconf"
eval ./configure $wmlconf
echo "$ make"
make 
if [ $? -ne 0 ]; then
    echo "** PORTING.test:Failed: 'make' failed";
    exit 1
fi
echo "$ make test"
make test
if [ $? -ne 0 ]; then
    echo "** PORTING.test:Failed: 'make test' failed";
    exit 1
fi
echo "$ make distclean"
make distclean
rm -rf /tmp/wml-test 2>/dev/null || true

echo ""
echo "*************************************************"
echo "*                CONGRATULATIONS!               *"
echo "*            Website META Language              *"
echo "*     built and ran correct on your platform    *"
echo "*************************************************"
echo ""
echo "The entry for the PORTING.L file:"
echo $entry
echo ""

##EOF##
