#!/bin/sh
#
#$Id: buildmlink 1.18 1995/04/18 15:54:04 Ezra_Story Exp $

echo "Multilink Configure"
echo

# setup variables
cflags="CFLAGS = -O "
ldflags="LDFLAGS = -s "
ccomp="cc "
cextra=" "

echo "Finding C compiler..."

if gcc -c conftest.c ; then
    ccomp="gcc "
    cextra=" -traditional"
    echo "Has GCC."
else
    if cc -c conftest.c ; then
        echo "Has CC."
    else
        echo "No C compiler!"
        exit
    fi
fi
if [ -f grepout ] ; then rm grepout ; fi

echo
echo "Compiling test file..."

$ccomp conftest.o 2> conftest.out 1>&2
if [ -f a.out ] ; then rm a.out ; fi
if [ -f conftest.o ] ; then rm conftest.o ; fi

if grep -s youis conftest.out 1> /dev/null 2>&1 ; then

    echo "Known system,  Using defines in config.h"

    if grep -s youissgi conftest.out 1> /dev/null 2>&1 ; then
        ccomp="cc"
        cflags="$cflags -cckr"
    fi
    if grep -s youisaix conftest.out 1> /dev/null 2>&1 ; then
        cflags="$cflags -D_IS_AIX"
        ldflags="$ldflags -lbsd"
        cextra=" "
    fi
    if grep -s youissun conftest.out 1> /dev/null 2>&1 ; then
        if [ -c /dev/ptmx ] ; then
            ldflags="$ldflags -lsocket -lnsl"
            cflags="$cflags -D__svr4__"
        fi
    fi
    if grep -s youissolaris conftest.out 1> /dev/null 2>&1 ; then
        ldflags="$ldflags -lsocket -lnsl"
    fi
    if grep -s youislinux conftest.out 1> /dev/null 2>&1 ; then
        cflags=" "
    fi
    if grep -s youisosf conftest.out 1> /dev/null 2>&1 ; then
        cextra=" "
    fi
    if grep -s youisultrix conftest.out 1> /dev/null 2>&1 ; then
        cextra=" "
    fi

else

    echo "Unknown system.  Figuring out best guess configuration..."

#
# we assume if it needs libsocket, it needs libnsl too...
#
    
    if grep -s "socket" conftest.out 1> /dev/null 2>&1 ; then
        echo "Needs -lsocket -lnsl"
        ldflags="$ldflags -lsocket -lnsl"
    else
        echo "Doesnt need extra libs"
    fi
    if [ -f grepout ] ; then rm grepout ; fi

    if [ -f /usr/include/unistd.h ] ; then
        echo "Has unistd.h"
    else
        cflags="$cflags -DCF_NOUNISTD"
        echo "Doesnt have unistd"
    fi

    if [ -f /usr/include/sys/select.h ] ; then
        cflags="$cflags -DCF_SYSSELECT"
        echo "Has sys/select.h."
    else
        echo "Doesnt have sys/select.h"
    fi

    if [ ! -f /usr/include/strings.h ] ; then
        cflags="$cflags -DCF_STRING"
        echo "Doesnt have strings.h"
    else
        echo "Has strings.h"
    fi

    if [ -f /usr/include/termios.h ] ; then
        cflags="$cflags -DCF_TERMIO"
        echo "Uses termio"
    else
        echo "Uses sgtty"
    fi

    if [ -f /usr/include/sys/filio.h ] ; then
        echo "Has sys/filio.h"
    else
        echo "Doesnt have sys/filio.h"
        cflags="$cflags -DCF_NOFILIO"
    fi

    if grep -s ulong /usr/include/sys/types.h 1> /dev/null 2>&1 ; then
        cflags="$cflags -DCF_ULONG"
        echo "Defines ulong"
    else
        echo "Does not define ulong"
    fi

    if grep -s tcflush conftest.out 1> /dev/null 2>&1 ; then
        echo "Doesnt have tcflush()"
    else
        cflags="$cflags -DCF_TCFLUSH"
        echo "Has tcflush()"
    fi

    if grep -s tcgetattr conftest.out 1> /dev/null 2>&1 ; then
        echo "Doesnt have tcgetattr()"
    else
        cflags="$cflags -DCF_TCATTR"
        echo "Has tcgetattr()"
    fi

    if grep -s waitpid conftest.out 1> /dev/null 2>&1 ; then
        echo "Doesn't have waitpid()"
        grep "wait3" /usr/include/sys/*.h 1> grepout 2>&1
    else
        cflags="$cflags -DCF_WAITPID"
        echo "Has waitpid()"
        grep "waitpid" /usr/include/sys/*.h 1> grepout 2>&1
    fi

    if grep -s "union wait" grepout 1> /dev/null 2>&1 ; then
        cflags="$cflags -DCF_STRUCTWAIT"
        echo "Uses union wait"
    else
        echo "Doesnt use union wait"
    fi
    if [ -f grepout ] ; then rm grepout ; fi

    if grep -s putenv conftest.out 1> /dev/null 2>&1 ; then
        echo "Doesn't have putenv()"
    else
        cflags="$cflags -DCF_PUTENV"
        echo "Has putenv()"
    fi

    if grep -s setenv conftest.out 1> /dev/null 2>&1 ; then
        echo "Doesn't have setenv()"
    else
        cflags="$cflags -DCF_SETENV"
        echo "Has setenv()"
    fi

    #
    # I assume if /dev/ptmx isused..it must be honest to god
    # SYSV... lets hope I'm right :-)
    #

    if grep -s _getpty conftest.out 1> /dev/null 2>&1 ; then
        echo "Doesnt use IRIX ptys"
        if [ -c /dev/ptmx ] ; then
            cflags="$cflags -DSVR4 -DSYSV -DPTY_SYSV"
            echo "Uses System V ptys"
            echo "System V"
        else
            echo "Doesnt use System V ptys"
            if [ -c /dev/ptc ] ; then
                ldflags="$ldflags -lbsd"
                cflags="$cflags -DPTY_AIX"
                echo "Uses AIX ptys"
            else
                echo "Doesnt use AIX ptys"
                echo "Uses BSD ptys"
                cflags="$cflags -DPTY_SEARCH"
            fi
        fi
    else
        cflags="$cflags -DPTY_GETPTY"
# some non irix systems have _getpty()?
# ccomp="cc -cckr"
        echo "Uses IRIX-type ptys"
    fi

    if grep -s setsid conftest.out 1> /dev/null 2>&1 ; then
        echo "Doesnt have setsid()"
    else
        cflags="$cflags -DCF_SETSID"
        echo "Has setsid()"
    fi

    if grep -s bcopy conftest.out 1> /dev/null 2>&1 ; then 
        echo "Doesnt have bcopy()"
        cflags="$cflags -DCF_NOBCOPY"
    else
        echo "Has bcopy()"
    fi

    if [ -f conftest.out ] ; then rm conftest.out ; fi

    #
    # This is hacked
    #

    if grep -s sin_len /usr/include/netinet/in.h 1> /dev/null 2>&1 ; then
        echo "BSD 4.4 sockets"
        cflags="$cflags -DBSD44"
    fi
fi

echo "Configuration done. "
echo
echo "Building makefile..."

if [ -f makefile ] ; then
    rm makefile
fi

echo "#!/bin/make -f" > makefile
echo "#" >> makefile
echo "CC=$ccomp $cextra" >> makefile
echo "$cflags" >> makefile
echo "$ldflags" >> makefile
cat makefile.tail >> makefile

echo
echo "Building mlink..."
echo
if [ -f make.err ] ; then rm make.err ; fi
if make all 2> make.err ; then
    echo
    echo "Build was successful."
    echo
else
    echo
    echo "Build was unsuccessful."
    echo "Error file is in make.err"
    echo
fi
