#!/bin/sh
#
# OpenSSL config: determine the operating system and run ./Configure
#
# "config -h" for usage information.
#
#          this is a merge of minarch and GuessOS from the Apache Group.
#          Originally written by Tim Hudson <tjh@cryptsoft.com>.

# Original Apache Group comments on GuessOS

# Simple OS/Platform guesser. Similar to config.guess but
# much, much smaller. Since it was developed for use with
# Apache, it follows under Apache's regular licensing
# with one specific addition: Any changes or additions
# to this script should be Emailed to the Apache
# group (apache@apache.org) in general and to
# Jim Jagielski (jim@jaguNET.com) in specific.
#
# Be as similar to the output of config.guess/config.sub
# as possible.

# First get uname entries that we use below

MACHINE=`(uname -m) 2>/dev/null` || MACHINE="unknown"
RELEASE=`(uname -r) 2>/dev/null` || RELEASE="unknown"
SYSTEM=`(uname -s) 2>/dev/null`  || SYSTEM="unknown"
VERSION=`(uname -v) 2>/dev/null` || VERSION="unknown"

# Now test for ISC and SCO, since it is has a braindamaged uname.
#
# We need to work around FreeBSD 1.1.5.1 
(
XREL=`uname -X 2>/dev/null | grep "^Release" | awk '{print $3}'`
if [ "x$XREL" != "x" ]; then
    if [ -f /etc/kconfig ]; then
	case "$XREL" in
	    4.0|4.1)
		    echo "${MACHINE}-whatever-isc4"; exit 0
		;;
	esac
    else
	case "$XREL" in
	    3.2v4.2)
		echo "whatever-whatever-sco3"; exit 0
		;;
	    3.2v5.0*)
		echo "whatever-whatever-sco5"; exit 0
		;;
	    4.2MP)
		if [ "x$VERSION" = "x2.1.1" ]; then
		    echo "${MACHINE}-whatever-unixware211"; exit 0
		else
		    echo "${MACHINE}-whatever-unixware2"; exit 0
		fi
		;;
	    4.2)
		echo "whatever-whatever-unixware1"; exit 0
		;;
	    5*)
		echo "${MACHINE}-sco-unixware7"; exit 0
		;;
	esac
    fi
fi
# Now we simply scan though... In most cases, the SYSTEM info is enough
#
case "${SYSTEM}:${RELEASE}:${VERSION}:${MACHINE}" in
    A/UX:*)
	echo "m68k-apple-aux3"; exit 0
	;;

    AIX:*)
	echo "${MACHINE}-ibm-aix"; exit 0
	;;

    dgux:*)
	echo "${MACHINE}-dg-dgux"; exit 0
	;;

    HI-UX:*)
	echo "${MACHINE}-hi-hiux"; exit 0
	;;

    HP-UX:*)
	HPUXVER=`echo ${RELEASE}|sed -e 's/[^.]*.[0B]*//'`
	case "$HPUXVER" in
	    11.*)
		echo "${MACHINE}-hp-hpux11"; exit 0
		;;
	    10.*)
		echo "${MACHINE}-hp-hpux10"; exit 0
		;;
	    *)
		echo "${MACHINE}-hp-hpux"; exit 0
		;;
	esac
	;;

    IRIX:5.*)
	echo "mips2-sgi-irix"; exit 0
	;;

    IRIX:6.*)
	echo "mips3-sgi-irix"; exit 0
	;;

    IRIX64:*)
	echo "mips4-sgi-irix64"; exit 0
	;;

    Linux:[2-9].*)
	echo "${MACHINE}-whatever-linux2"; exit 0
	;;

    Linux:1.*)
	echo "${MACHINE}-whatever-linux1"; exit 0
	;;

    LynxOS:*)
	echo "${MACHINE}-lynx-lynxos"; exit 0
	;;

    BSD/OS:4.*)  # BSD/OS always says 386
	echo "i486-whatever-bsdi4"; exit 0
	;;

    BSD/386:*:*:*486*|BSD/OS:*:*:*:*486*)
        case `/sbin/sysctl -n hw.model` in
	    Pentium*)
                echo "i586-whatever-bsdi"; exit 0
                ;;
            *)
                echo "i386-whatever-bsdi"; exit 0
                ;;
            esac;
	;;

    BSD/386:*|BSD/OS:*)
	echo "${MACHINE}-whatever-bsdi"; exit 0
	;;

    FreeBSD:*)
        VERS=`echo ${RELEASE} | sed -e 's/[-(].*//'`
        MACH=`sysctl -n hw.model`
        ARCH='whatever'
        case ${MACH} in
           *386*       ) MACH="i386"     ;;
           *486*       ) MACH="i486"     ;;
           Pentium\ II*) MACH="i686"     ;;
           Pentium*    ) MACH="i586"     ;;
           Alpha*      ) MACH="alpha"    ;;
           *           ) MACH="$MACHINE" ;;
        esac
        case ${MACH} in
           i[0-9]86 ) ARCH="pc" ;;
        esac
        echo "${MACH}-${ARCH}-freebsd${VERS}"; exit 0
        ;;

    NetBSD:*:*:*386*)
        echo "`sysctl -n hw.model | sed 's,.*\(.\)86-class.*,i\186,'`-whateve\r-netbsd"; exit 0
	;;

    NetBSD:*)
	echo "${MACHINE}-whatever-netbsd"; exit 0
	;;

    OpenBSD:*)
	echo "${MACHINE}-whatever-openbsd"; exit 0
	;;

    OSF1:*:*:*alpha*)
	echo "${MACHINE}-dec-osf"; exit 0
	;;

    QNX:*)
	case "$VERSION" in
	    423)
		echo "${MACHINE}-qssl-qnx32"
		;;
	    *)
		echo "${MACHINE}-qssl-qnx"
		;;
	esac
	exit 0
	;;

    Paragon*:*:*:*)
	echo "i860-intel-osf1"; exit 0
	;;

    Rhapsody:*)
	echo "ppc-apple-rhapsody"; exit 0
	;;

    SunOS:5.*)
	echo "${MACHINE}-sun-solaris2"; exit 0
	;;

    SunOS:*)
	echo "${MACHINE}-sun-sunos4"; exit 0
	;;

    UNIX_System_V:4.*:*)
	echo "${MACHINE}-whatever-sysv4"; exit 0
	;;

    *:4*:R4*:m88k)
	echo "${MACHINE}-whatever-sysv4"; exit 0
	;;

    DYNIX/ptx:4*:*)
	echo "${MACHINE}-whatever-sysv4"; exit 0
	;;

    *:4.0:3.0:3[34]?? | *:4.0:3.0:3[34]??,*)
	echo "i486-ncr-sysv4"; exit 0
	;;

    ULTRIX:*)
	echo "${MACHINE}-unknown-ultrix"; exit 0
	;;

    SINIX*|ReliantUNIX*)
	echo "${MACHINE}-siemens-sysv4"; exit 0
	;;

    POSIX-BC*)
	echo "${MACHINE}-siemens-sysv4"; exit 0   # Here, $MACHINE == "BS2000"
	;;

    machten:*)
       echo "${MACHINE}-tenon-${SYSTEM}"; exit 0;
       ;;

    library:*)
	echo "${MACHINE}-ncr-sysv4"; exit 0
	;;

    ConvexOS:*:11.0:*)
	echo "${MACHINE}-v11-${SYSTEM}"; exit 0;
	;;

esac

#
# Ugg. These are all we can determine by what we know about
# the output of uname. Be more creative:
#

# Do the Apollo stuff first. Here, we just simply assume
# that the existance of the /usr/apollo directory is proof
# enough
if [ -d /usr/apollo ]; then
    echo "whatever-apollo-whatever"
    exit 0
fi

# Now NeXT
ISNEXT=`hostinfo 2>/dev/null`
case "$ISNEXT" in
    *'NeXT Mach 3.3'*)
	echo "whatever-next-nextstep3.3"; exit 0
	;;
    *NeXT*)
	echo "whatever-next-nextstep"; exit 0
	;;
esac

# At this point we gone through all the one's
# we know of: Punt

echo "${MACHINE}-whatever-${SYSTEM}" 
exit 0
) 2>/dev/null | (

# ---------------------------------------------------------------------------
# this is where the translation occurs into SSLeay terms
# ---------------------------------------------------------------------------

PREFIX=""
SUFFIX=""
TEST="false"

# pick up any command line args to config
for i
do
case "$i" in 
-d*) PREFIX="debug-";;
-t*) TEST="true";;
-h*) TEST="true"; cat <<EOF
Usage: config [options]
 -d	Add a debug- prefix to machine choice.
 -t	Test mode, do not run the Configure perl script.
 -h	This help.

Any other text will be passed to the Configure perl script.
See INSTALL for instructions.

EOF
;;
*) options=$options" $i" ;;
esac
done

# figure out if gcc is available and if so we use it otherwise
# we fallback to whatever cc does on the system
GCCVER=`(gcc --version) 2>/dev/null`
if [ "$GCCVER" != "" ]; then
  CC=gcc
  # then strip off whatever prefix Cygnus prepends the number with...
  GCCVER=`echo $GCCVER | sed 's/^[a-z]*\-//'`
  # peak single digit before and after first dot, e.g. 2.95.1 gives 29
  GCCVER=`echo $GCCVER | sed 's/\([0-9]\)\.\([0-9]\).*/\1\2/'`
else
  CC=cc
fi

if [ "$SYSTEM" = "SunOS" ]; then
  # check for WorkShop C, expected output is "cc: blah-blah C x.x"
  CCVER=`(cc -V 2>&1) 2>/dev/null | \
  	egrep -e '^cc: .* C [0-9]\.[0-9]' | \
	sed 's/.* C \([0-9]\)\.\([0-9]\).*/\1\2/'`
  CCVER=${CCVER:-0}
  if [ $CCVER -gt 40 ]; then
    CC=cc	# overrides gcc!!!
    if [ $CCVER -eq 50 ]; then
      echo "WARNING! Detected WorkShop C 5.0. Do make sure you have"
      echo "         patch #107357-01 or later applied."
      sleep 5
    fi
  elif [ "$CC" = "cc" -a $CCVER -gt 0 ]; then
    CC=sc3
  fi
fi

if [ "${SYSTEM}-${MACHINE}" = "Linux-alpha" ]; then
  # check for Compaq C, expected output is "blah-blah C Vx.x"
  CCCVER=`(ccc -V 2>&1) 2>/dev/null | \
	egrep -e '.* C V[0-9]\.[0-9]' | \
	sed 's/.* C V\([0-9]\)\.\([0-9]\).*/\1\2/'`
  CCCVER=${CCCVER:-0}
  if [ $CCCVER -gt 60 ]; then
    CC=ccc	# overrides gcc!!! well, ccc outperforms inoticeably
		# only on hash routines and des, otherwise gcc (2.95)
		# keeps along rather tight...
  fi
fi

GCCVER=${GCCVER:-0}
CCVER=${CCVER:-0}

# read the output of the embedded GuessOS 
read GUESSOS

echo Operating system: $GUESSOS

# now map the output into SSLeay terms ... really should hack into the
# script above so we end up with values in vars but that would take
# more time that I want to waste at the moment
case "$GUESSOS" in
  mips2-sgi-irix)
	CPU=`(hinv -t cpu) 2>/dev/null | sed 's/^CPU:[^R]*R\([0-9]*\).*/\1/'`
	CPU=${CPU:-0}
	if [ $CPU -ge 4000 ]; then
		options="$options -mips2"
	fi
	OUT="irix-$CC"
	;;
  mips3-sgi-irix)
	CPU=`(hinv -t cpu) 2>/dev/null | sed 's/^CPU:[^R]*R\([0-9]*\).*/\1/'`
	CPU=${CPU:-0}
	if [ $CPU -ge 5000 ]; then
		options="$options -mips4"
	else
		options="$options -mips3"
	fi
	OUT="irix-mips3-$CC"
	;;
  mips4-sgi-irix64)
	echo "WARNING! If you wish to build 64-bit library, then you have to"
	echo "         invoke './Configre irix64-mips4-$CC' *manually*."
	echo "         Type Ctrl-C if you don't want to continue."
	read waste < /dev/tty
	options="$options -mips4"
	OUT="irix-mips3-$CC"
	;;
  alpha-*-linux2)
        ISA=`awk '/cpu model/{print$4}' /proc/cpuinfo`
	case ${ISA:-generic} in
	*[67])	OUT="linux-alpha+bwx-$CC" ;;
	*)	OUT="linux-alpha-$CC" ;;
	esac
	if [ "$CC" = "gcc" ]; then
	    case ${ISA:-generic} in
	    EV5|EV45)		options="$options -mcpu=ev5";;
	    EV56|PCA56)		options="$options -mcpu=ev56";;
	    EV6|EV67|PCA57)	options="$options -mcpu=ev6";;
	    esac
	fi
	;;
  mips-*-linux?) OUT="linux-mips" ;;
  ppc-*-linux2) OUT="linux-ppc" ;;
  ppc-apple-rhapsody) OUT="rhapsody-ppc-cc" ;;
  sparc64-*-linux2)
	#Before we can uncomment following lines we have to wait at least
	#till 64-bit glibc for SPARC is operational:-(
	#echo "WARNING! If you wish to build 64-bit library, then you have to"
	#echo "         invoke './Configure linux64-sparcv9' *manually*."
	#echo "         Type Ctrl-C if you don't want to continue."
	#read waste < /dev/tty
	OUT="linux-sparcv9" ;;
  sparc-*-linux2)
	KARCH=`awk '/type/{print$3}' /proc/cpuinfo`
	case ${KARCH:-sun4} in
	sun4u*)	OUT="linux-sparcv9" ;;
	sun4m)	OUT="linux-sparcv8" ;;
	sun4d)	OUT="linux-sparcv8" ;;
	*)	OUT="linux-sparcv7" ;;
	esac ;;
  *-*-linux2) OUT="linux-elf" ;;
  *-*-linux1) OUT="linux-aout" ;;
  sun4u*-sun-solaris2)
	ISA64=`(isalist) 2>/dev/null | grep sparcv9`
	if [ "$ISA64" != "" -a "$CC" = "cc" -a $CCVER -ge 50 ]; then
		echo "WARNING! If you wish to build 64-bit library, then you have to"
		echo "         invoke './Configure solaris64-sparcv9-cc' *manually*."
		echo "         Type Ctrl-C if you don't want to continue."
		read waste < /dev/tty
	fi
	OUT="solaris-sparcv9-$CC" ;;
  sun4m-sun-solaris2)	OUT="solaris-sparcv8-$CC" ;;
  sun4d-sun-solaris2)	OUT="solaris-sparcv8-$CC" ;;
  sun4*-sun-solaris2)	OUT="solaris-sparcv7-$CC" ;;
  *86*-sun-solaris2) OUT="solaris-x86-$CC" ;;
  *-*-sunos4) OUT="sunos-$CC" ;;
  alpha*-*-freebsd*) OUT="FreeBSD-alpha" ;;
  *-freebsd[3-9]*) OUT="FreeBSD-elf" ;;
  *-freebsd[1-2]*) OUT="FreeBSD" ;;
  *86*-*-netbsd) OUT="NetBSD-x86" ;;
  sun3*-*-netbsd) OUT="NetBSD-m68" ;;
  *-*-netbsd) OUT="NetBSD-sparc" ;;
  *86*-*-openbsd) OUT="OpenBSD-x86" ;;
  alpha*-*-openbsd) OUT="OpenBSD-alpha" ;;
  pmax*-*-openbsd) OUT="OpenBSD-mips" ;;
  *-*-openbsd) OUT="OpenBSD" ;;
  *86*-*-bsdi4) OUT="bsdi-elf-gcc" ;;
  *-*-osf) OUT="alpha-cc" ;;
  *-*-unixware7) OUT="unixware-7" ;;
  *-*-UnixWare7) OUT="unixware-7" ;;
  *-*-Unixware7) OUT="unixware-7" ;;
  *-*-unixware[1-2]*) OUT="unixware-2.0" ;;
  *-*-UnixWare[1-2]*) OUT="unixware-2.0" ;;
  *-*-Unixware[1-2]*) OUT="unixware-2.0" ;;
  BS2000-siemens-sysv4) OUT="BS2000-OSD" ;;
  RM*-siemens-sysv4) OUT="ReliantUNIX" ;;
  *-siemens-sysv4) OUT="SINIX" ;;
  *-hpux1*)	OUT="hpux-parisc-$CC"
		options="$options -D_REENTRANT" ;;
  *-hpux)	OUT="hpux-parisc-$CC" ;;
  # these are all covered by the catchall below
  # *-aix) OUT="aix-$CC" ;;
  # *-dgux) OUT="dgux" ;;
  *) OUT=`echo $GUESSOS | awk -F- '{print $3}'`;;
esac

# See whether we can compile Atalla support
if [ -f /usr/include/atasi.h ]
then
  options="$options -DATALLA"
fi

# gcc < 2.8 does not support -mcpu=ultrasparc
if [ "$OUT" = solaris-sparcv9-gcc -a $GCCVER -lt 28 ]
then
  echo "WARNING! Do consider upgrading to gcc-2.8 or later."
  sleep 5
  OUT=solaris-sparcv9-gcc27
fi
if [ "$OUT" = "linux-sparcv9" -a $GCCVER -lt 28 ]
then
  echo "WARNING! Falling down to 'linux-sparcv8'."
  echo "         Upgrade to gcc-2.8 or later."
  sleep 5
  OUT=linux-sparcv8
fi
# To start with $OUT is never i86pc-sun-solaris2. Secondly why
# ban *all* assembler implementation if it can't stand only one,
# SHA-0 implementation.
#if [ "$OUT" = "i86pc-sun-solaris2" ]
#then
#  ASM=`as -V /dev/null 2>&1`
#  case "$ASM" in
#    GNU*) ;;
#    *) options="$options no-asm" ; echo "WARNING: You need the GNU assembler to use OpenSSL assembler code." ; echo "Sun as is not supported on Solaris x86." ;;
#  esac
#fi

case "$GUESSOS" in
  i386-*) options="$options 386" ;;
esac

for i in bf cast des dh dsa hmac md2 md5 mdc2 rc2 rc4 rc5 ripemd rsa sha
do
  if [ ! -d crypto/$i ]
  then
    options="$options no-$i"
  fi
done

if [ -z "$OUT" ]; then
  OUT="$CC"
fi

if [ ".$PERL" = . ] ; then
	for i in . `echo $PATH | sed 's/:/ /g'`; do
		if [ -f "$i/perl5" ] ; then
			PERL="$i/perl5"
			break;
		fi;
	done
fi

if [ ".$PERL" = . ] ; then
	for i in . `echo $PATH | sed 's/:/ /g'`; do
		if [ -f "$i/perl" ] ; then
			if "$i/perl" -e 'exit($]<5.0)'; then
				PERL="$i/perl"
				break;
			fi;
		fi;
	done
fi

if [ ".$PERL" = . ] ; then
	echo "You need Perl 5."
	exit 1
fi

# run Configure to check to see if we need to specify the 
# compiler for the platform ... in which case we add it on
# the end ... otherwise we leave it off

$PERL ./Configure LIST | grep "$OUT-$CC" > /dev/null
if [ $? = "0" ]; then
  OUT="$OUT-$CC"
fi

OUT="$PREFIX$OUT"

$PERL ./Configure LIST | grep "$OUT" > /dev/null
if [ $? = "0" ]; then
  echo Configuring for $OUT

  if [ "$TEST" = "true" ]; then
    echo $PERL ./Configure $OUT $options
  else
    $PERL ./Configure $OUT $options
  fi
else
  echo "This system ($OUT) is not supported. See file INSTALL for details."
fi
)
