#! /bin/sh
#
# Dynamically determine the PLATFORM identifier for S-PLUS utilities.
#

mach_fld=${1-machine}
tmpdir=${S_TMP:-$SHOME/tmp}

if [ "$mach_fld" = "list" ]
then
    echo HP300; echo HP700; echo HP800
    echo DECSTATION; echo VAX; echo OSF1_DECALPHA
    echo IRIS4D
    echo SUN3; echo SUN386I; echo SUNOS4_SPARC; echo SUNOS5_SPARC
    echo IBMRS6000
    echo APOLLO
    echo CONVEX
    echo UNIX386; echo DOS386 ; echo LINUX
    echo NEXT
    exit 0
fi

uname='echo idunno'
for d in /bin /usr/bin /usr/5bin /sys5.3/bin
do
   if [ -r "$d/uname" ]
   then uname="$d/uname"; break
   fi
done
uname_m="`$uname -m`:`$uname -s`"
defines=""

mtype=idunno
case "$uname_m" in
    9000/[34]*:HP-UX) mtype=HP300;;
    9000/7*:HP-UX) mtype=HP700;;
    9000/[68]*:HP-UX) mtype=HP800;;
    alpha:OSF1) mtype=OSF1_DECALPHA;;
    RISC:ULTRIX) mtype=DECSTATION;;
    *:ULTRIX) mtype=VAX;;
    IP*:IRIX*) mtype=IRIS4D;;
    sun3*:SunOS) mtype=SUN3;;
    sun4*:SunOS) case "`uname -r`" in
       4.*) mtype=SUNOS4_SPARC;;
       *) mtype=SUNOS5_SPARC;;
    esac;;
    sun:SunOS)
        if /usr/bin/sun386 2>/dev/null
        then mtype=SUN386I
        elif /usr/bin/sun2 2>/dev/null
        then mtype=SUN2
        fi
    ;;
    *:AIX) case "`file /bin/sh`" in
        *RISC?System/6000*) mtype=IBMRS6000;;
    esac;;
esac

[ "$mtype" = "idunno" ] &&
cat <<'EOCAT' |
#if defined(u3b2) /*(*/
#define MACHINE U3B2
#endif /*)*/

#if defined(apollo)  /*(*/
#define MACHINE APOLLO
#endif /*)*/

#if defined(convex) /*(*/
#define MACHINE CONVEX
#endif /*)*/

#if !defined(sun) && defined(unix) && defined(i386) /*(*/
#define MACHINE LINUX
#endif /*)*/

#if defined(i386) && !defined(unix) /*(*/
#define MACHINE DOS386
#endif /*)*/

#if defined (NeXT) /*(*/
#define MACHINE NEXT
#endif /*)*/

#ifdef MACHINE
machine: MACHINE
#else
machine: idunno
#endif
EOCAT
/lib/cpp ${defines} -P 2>/dev/null |
sed -n -e '/^'$mach_fld':/s/'$mach_fld': *//p' -e 's/[ 	]//g' >$tmpdir/mtype.$$ &&
cppout=`cat $tmpdir/mtype.$$` &&
rm -f $tmpdir/mtype.$$ &&
mtype=$cppout

if [ -z "$mtype" ] || [ "$mtype" = "idunno" ]
then
    if [ -r /usr/bin/arch ]
    then mtype=`/usr/bin/arch`
    else mtype="`$uname -m`"
    fi
    case "$mtype" in
        DN[0-9]*) case "`file /bin/sh`" in
            *apollo?m68k*) mtype=APOLLO;;
            *) if /usr/apollo/bin/prism 2>/dev/null
               then mtype=APOLLO
               fi
            ;;
        esac;;

        3B2) mtype=U3B2;;
    	i[234]86) mtype=UNIX386
#		    if [ -d /usr/lib/sco ]
#		    then mtype=sco-$mtype
#		    fi
        ;;
        *) # Uggghhh!!!
            if [ -d /NextLibrary ]
            then mtype=NEXT
		    fi
        ;;
    esac
fi

echo $mtype
