#!/bin/sh
# $Id: configure,v 1.38 2008/06/15 07:08:49 paulus Exp $

# Where to install stuff by default
DESTDIR=/usr/local
SYSCONF=/etc

#  if [ -d /NextApps ]; then
#    system="NeXTStep"
#  else
  system=`uname -s`
  release=`uname -r`
  arch=`uname -m`
#  fi
state="unknown"

case $system in
  Linux)
    makext="linux";
    ksrc="linux";
    state="known";;
  SunOS)
    karch=`/usr/bin/isainfo -k`
    case $release in
#      [0-3]*)	state="ancient";;
#      4*)	state="known"; ksrc="sunos4"; makext="sunos4";;
      5.[7-9]*|5.[1-9][0-9]*)	state="known"; ksrc="solaris"; makext="sol2";
              case "$karch" in
		amd64)		archvariant='-64x';;
		sparcv9)	archvariant='-64';;
		*)	;;
	      esac;;
      5.[1-6]*)	state="known"; ksrc="solaris"; makext="sol2";;
    esac
    if [ "$1" = gcc ]; then
	shift
	usegcc=gcc
    fi
    if [ "$1" = 32 ]; then
	shift
	archvariant=
    fi
    if [ -x /opt/SUNWspro/bin/cc -a "$usegcc" != gcc ] &&
       /opt/SUNWspro/bin/cc -flags >/dev/null 2>&1; then
      if [ "$archvariant" = "-64x" ]; then
        ( cd /tmp; echo "int x;" > ppp$$.c
	  /opt/SUNWspro/bin/cc -c -errwarn -xchip=opteron -xarch=amd64 ppp$$.c >/dev/null 2>&1 || (
	    echo "WorkShop C is unable to make 64 bit modules, and your $karch system needs"
	    echo "them.  Consider upgrading cc on this machine."
	    rm -f ppp$$.c
	    exit 1
	  ) || exit 1
	  rm -f ppp$$.c ppp$$.o
        ) || exit 1
      fi
    elif gcc --version >/dev/null 2>&1; then
      archvariant=gcc$archvariant
      compiletype=.gcc
      if [ "$archvariant" = "gcc-64" -o"$archvariant" = "gcc-64x" ]; then
        ( cd /tmp; touch ppp$$.c
	  gcc -c -m64 ppp$$.c >/dev/null 2>&1 || (
	    echo "gcc is unable to make 64 bit modules, and your $karch system needs them."
	    echo "Consider upgrading gcc on this machine, or switching to Sun WorkShop."
	    rm -f ppp$$.c
	    exit 1
	  ) || exit 1
	  rm -f ppp$$.c ppp$$.o
        ) || exit 1
      fi
    else
      echo "C compiler not found; hoping for the best."
    fi;;
  NetBSD|FreeBSD|ULTRIX|OSF1|NeXTStep|SINIX-?|UNIX_SV|UNIX_System_V)
    state="notincluded";;
#    NetBSD)
#      makext="bsd";
#      case $release in
#        0.*)	state="ancient";;
#        1.0*)	state="ancient";;
#        1.1*)	state="known"; ksrc="netbsd-1.1";;
#        1.2*)	state="known"; ksrc="netbsd-1.2"; makext="netbsd-1.2";;
#        1.[3-9]*|[2-9]*)
#  		state="late"; ksrc="netbsd-1.2";;
#      esac;;
#    ULTRIX)
#      makext="ultrix";
#      case $release in
#        [0-3]*)	state="ancient";;
#        4.[01]*)	state="early"; ksrc="ultrix";;
#        4.[234])	state="known"; ksrc="ultrix";;
#      esac;;
#    OSF1)
#      makext="osf";
#      case $release in
#        V1.*)   state="neolithic"; ksrc="osf1";;
#        V[23].*)	state="neolithic"; ksrc="osf1";;
#        V4.*)	state="known"; ksrc="osf1";;
#        V[5-9]*) state="late"; ksrc="osf1";;
#      esac;;
#    FreeBSD)
#      makext="bsd";
#      case $release in
#        1.*)	state="known"; ksrc="freebsd-old";;
#        2.[01]*)	state="known"; ksrc="freebsd-2.0";;
#        2.2.[2-7]*) state="late"; ksrc="freebsd-2.0";;
#        2.2.8*)   state="known"; ksrc="freebsd-2.2.8";;
#        3.[0-1]*)	state="known"; ksrc="freebsd-3.0";;
#      esac;;
#    NeXTStep)
#      makext="NeXT";
#      ksrc="NeXT";
#      state="known";;
#    SINIX-?)
#      case $release in
#        5.4[01]) state=known; ksrc=svr4; makext=svr4;;
#        5.4[2-9]) state=late; ksrc=svr4; makext=svr4;;
#      esac;;
#    # Intel SVR4 systems come with a bug in the uname program.  Unless
#    # your provider fixed the bug, or you get a fix for it, uname -S will
#    # overwrite the system name with the node name!
#    UNIX_SV|UNIX_System_V|`uname -n`)
#      case $release in
#        4.0) state=known; ksrc=svr4; makext=svr4;;
#        4.2) state=late; ksrc=svr4; makext=svr4;;
#      esac;;
esac

if [ -d "$ksrc" ]; then :; else
  state="notincluded"
  unset ksrc
fi

case $state in
  neolithic) 
    echo "This is a newer release on an outdated OS ($system)."
    echo " This software may or may not work on this OS."
    echo " You may want to download an older version of PPP for this OS.";;
  ancient)
    echo "This is an old release of a supported OS ($system)."
    echo "This software cannot be used as-is on this system,"
    echo "but you may be able to port it.  Good luck!"
    exit;;
  early)
    echo "This is an old release of a supported OS ($system)."
    echo "This software should install and run on this system,"
    echo "but it hasn't been tested.";;
  late)
    echo "This is a newer release of $system than is supported by"
    echo "this software.  It may or may not work.";;
  unknown)
    echo "This software has not been ported to $system.  Sorry.";;
  notincluded)
    echo "Support for $system has not been included"
    echo "in this distribution.  Sorry.";;
  known)
    echo "Configuring for $system";;
esac

# Parse arguments
while [ $# -gt 0 ]; do
    arg=$1
    val=
    shift
    case $arg in
	*=*)
	    val=`expr "x$arg" : 'x[^=]*=\(.*\)'`
	    arg=`expr "x$arg" : 'x\([^=]*\)=.*'`
	    ;;
	--prefix|--sysconf)
	    if [ $# -eq 0 ]; then
		echo "error: the $arg argument requires a value" 1>&2
		exit 1
	    fi
	    val=$1
	    shift
	    ;;
    esac
    case $arg in
	--prefix)	DESTDIR=$val ;;
	--sysconfdir)	SYSCONF=$val ;;
    esac
done

mkmkf() {
    rm -f $2
    if [ -f $1 ]; then
	echo "  $2 <= $1"
	sed -e "s,@DESTDIR@,$DESTDIR,g" -e "s,@SYSCONF@,$SYSCONF,g" $1 >$2
    fi
}

if [ -d "$ksrc" ]; then
    echo "Creating Makefiles."
    mkmkf $ksrc/Makefile.top Makefile
    mkmkf $ksrc/Makedefs$compiletype Makedefs.com
    for dir in pppd pppstats chat pppdump pppd/plugins pppd/plugins/rp-pppoe \
	       pppd/plugins/radius pppd/plugins/pppoatm \
	       pppd/plugins/pppol2tp; do
	mkmkf $dir/Makefile.$makext $dir/Makefile
    done
    if [ -f $ksrc/Makefile.$makext$archvariant ]; then
	mkmkf $ksrc/Makefile.$makext$archvariant $ksrc/Makefile
    fi
else
  echo "Unable to locate kernel source $ksrc"
  exit 1
fi
