#!/bin/sh
#
#	$Id: Config,v 1.22 2002/04/06 04:53:07 forys Exp $
#
#	Config - edit skill Makefile filling in OSTYPE, COPTS, LIBS, and
#		possibly BINGRP and BINMOD.
#	Contributed by: Ric Anderson <ric@Opus1.COM>
#

#	GuessOS - guess the OS type for skill.
#		Set "OS" to OStype based on uname(1), /etc/motd,
#		or, failing that, the location of some files.
#	Contributed by: Ric Anderson <ric@Opus1.COM>
#
#	NOTE: GuessOS was merged into Config for Skill v4.1.
#	Run "Config -g" to emulate classic GuessOS behavior.
#

GuessOS=0
if [ $# -ne 0 ]; then
	if [ $# -eq 1 -a "$1" = "-g" ]; then
		GuessOS=1
	else
		echo "Usage: $0 [-g]"					>&2
		exit 1
	fi
fi

###
### GuessOS
###

OS=

#
# First try to use uname(1), then resort file sniffing.
UNAME_OS=`exec 2>/dev/null; uname -s`
UNAME_REV=`exec 2>/dev/null; uname -r`

if [ -n "${UNAME_OS}" -a -n "${UNAME_REV}" ]; then
	case "${UNAME_OS}" in
	    AIX)
		OS=aix3
		;;
	    FreeBSD)
		# tested nbsd44 back to 2.2.8 (probably works on prev revs)
		case "${UNAME_REV}" in
		    0.*|1.*|2.[0-1]*|2.2.[0-7]*)
			OS=bsd44
			;;
		    *)
			OS=nbsd44
			;;
		esac
		;;
	    HP-UX)
		case "${UNAME_REV}" in
		    *.6.*|*.7.*)
			OS=hpux7
			;;
		    *.8.*|*.9.*)
			OS=hpux9
			;;
		    *)
			OS=hpux10
			;;
		esac
		;;
	    IRIX*)
		case "${UNAME_REV}" in
		    3.*)
			OS=irix3
			;;
		    4.*)
			OS=irix4
			;;
		    5.*)
			OS=irix5
			;;
		    6.*)
			OS=irix6
			;;
		    *)
			OS=irix
			;;
		esac
		;;
	    Linux)
		OS=linux
		;;
	    NetBSD)
		# nbsd44 is believed to work back to NetBSD 0.8.
		OS=nbsd44
		;;
	    OpenBSD)
		# tested nbsd44 on OpenBSD 2.6-3.0 (may work on prev revs)
		case "${UNAME_REV}" in
		    2.6|2.7|2.8|2.9)
			OS=nbsd44
			;;
		    0.*|1.*|2.*)
			OS=bsd44
			;;
		    *)
			OS=nbsd44
			;;
		esac
		;;
	    OSF1)
		OS=mach3
		;;
	    SunOS)
		case "${UNAME_REV}" in
		    5.[01234])
			OS=sos5
			;;
		    4.1*)
			OS=sos4_1
			;;
		    4*)
			OS=sos4
			;;
		    3*)
			OS=sos3
			;;
		    *)
			OS=sos55
			;;
		esac
		;;
	    UNIX_SV*|UNIX_System_V*)
		OS=svr
		;;
	esac
fi

if [ -z "${OS}" ]; then
	case "`exec 2>/dev/null; head -2 /etc/motd`" in
	    *"IBM AIX Version 3"*)
		OS=aix3
		;;
	    *"IBM AIX Version 4"*)
		OS=aix3
		;;
	    *"DYNIX(R) V3."*)
		OS=dynix3
		;;
	    *"Sun UNIX 4.2 Release 3."*)
		OS=sos3
		;;
	    *"SunOS Release 4.1"*)
		OS=sos4_1
		;;
	    *"SunOS Release 4."*)
		OS=sos4
		;;
	    *"ULTRIX V4."*)
		OS=ultrix4
		;;
	    *"Ultrix"*"V2"*)
		OS=ultrix2
		;;
	    *"NeXT Mach 1"*)
		OS=next1
		;;
	    *"NeXT Mach 2"*)
		OS=next2
		;;
	    *"Mach 3"*)
		OS=mach3
		;;
	    *"Digital UNIX"*)
		OS=mach3
		;;
	    *"Tru64 UNIX"*)
		OS=mach3
		;;
	    *"OSF/1"*)
		OS=mach3
		;;
	    *"4.3 BSD UNIX"*)
		if [ -f /etc/ttylocal ]; then
		    OS=xinu43
		else
		    OS=bsd43
		fi
		;;
	    *"4.3 BSD Reno UNIX"*)
		OS=obsd44
		;;
	    *"4.4BSD"*)
		OS=obsd44
		;;
	    *"NetBSD"*)
		OS=bsd44
		;;
	    *"FreeBSD"*)
		OS=bsd44
		;;
	    *"BSDI"*)
		OS=bsd44
		;;
	    *"UMIPS"*)
		OS=umips
		;;
	    *"Umax 4.2"*)
		OS=umax-42
		;;
	#
	# RATS!!!  Try to sniff out a file or two.
	#
	    *)
		if [ -f /usr/lib/libNeXT_s.a ]; then
		    OS=next2
		elif [ -f /mach ]; then
		    OS=mach2
		elif [ -f /bin/universe ]; then
		    OS=osx
		elif [ -f /bin/4d ]; then
		    OS=irix
		elif [ -f /bin/hp9000s800 ]; then
		    OS=hpux
		elif [ -d /etc/aix -o -d /etc/aixdwm ]; then
		    OS=aix3
		elif [ -f /usr/bin/cat ]; then
		    OS=sos4
		elif [ -f /etc/nd ]; then
		    OS=sos3
		elif [ -f /etc/ttylocal ]; then
		    OS=xinu43
		elif [ -f /Umax.image ]; then
		    OS=umax42
		elif [ -d /usr/ibm ]; then
		    OS=aos43
		else
		    OS=unknown
		fi
		;;
	esac
fi

#
# Check if only doing GuessOS
if [ "$GuessOS" -eq 1 ]; then
	echo "$OS"
	exit 0
fi

###
### Config
###

OSTYPE=
COPTS=
LIBS=
BINGRP=
BINMOD=

case "$OS" in
    bsd42)
	OSTYPE=bsd-43 COPTS=-DNO_UID_T BINGRP=kmem BINMOD=2755
	;;

    bsd43)
	OSTYPE=bsd-43 BINGRP=kmem BINMOD=2755
	;;

    bsd44)
	OSTYPE=bsd-44 LIBS=-lkvm BINGRP=kmem BINMOD=2755
	;;

    nbsd44)
	OSTYPE=nbsd-44
	;;

    sos3)
	OSTYPE=sunos-3
	;;

    sos4)
	OSTYPE=sunos-40 LIBS=-lkvm
	;;

    sos4_1)
	OSTYPE=sunos-41 LIBS=-lkvm
	;;

    sos5)
	OSTYPE=sys-5r4
	;;

    sos55)
	OSTYPE=sys-5r4 COPTS=-DSYSV_REGEX
	;;

    dynix3)
	OSTYPE=dynix-3
	;;

    osx*)
	OSTYPE=osx-4
	;;

    svr*)
	OSTYPE=sys-5r4
	;;

    hpux6)
	OSTYPE=hpux-70 COPTS=-DNO_UID_T
	;;

    hpux7)
	OSTYPE=hpux-70
	;;

    hpux8|hpux9)
	OSTYPE=hpux-91
	;;

    hpux*)
	OSTYPE=hpux-10
	;;

    linux*)
	OSTYPE=linux-1 COPTS=-O3 LIBS="-s -N"
	;;

    mach2)
	OSTYPE=mach-26 LIBS=-lsys
	;;

    mach3)
	OSTYPE=mach-3
	;;

    next1)
	OSTYPE=mach-26
	;;

    next*)
	OSTYPE=next-2
	;;

    umips*)
	OSTYPE=umips-21 LIBS=-lmld
	;;

    ultrix2)
	OSTYPE=ultrix-22
	;;

    ultrix4)
	OSTYPE=ultrix-4
	;;

    aos43)
	OSTYPE=aos-43
	;;

    aix3)
	OSTYPE=aix-3
	;;

    irix3)
	OSTYPE=irix-3 LIBS=-lmld
	;;

    irix4)
	OSTYPE=irix-4 LIBS=-lmld
	;;

    irix*)
	OSTYPE=irix-5
	;;

    umax42)
	OSTYPE=umax-42 COPTS=-DNO_UID_T
	;;

    *)
	echo "Config: unknown system type: $OS"				>&2
	exit 1
	;;
esac
echo "Configuring as ${OS}: OSTYPE=${OSTYPE} COPTS=${COPTS} LIBS=${LIBS}"
chmod u+w Makefile
ed - Makefile <<EOS
/^OSTYPE=/c
OSTYPE=	${OSTYPE}
.
/^COPTS=/c
COPTS=	${COPTS}
.
/^LIBS=/c
LIBS=	${LIBS}
.
w
q
EOS
if [ -n "$BINGRP" ]; then
	echo "Setting default group/mode to BINGRP=${BINGRP} BINMOD=${BINMOD}"
	ed - Makefile <<EOB
/^BINGRP=/c
BINGRP=	${BINGRP}
.
/^BINMOD=/c
BINMOD=	${BINMOD}
.
w
q
EOB
fi
exit 0
