#! /bin/csh -f
#
# This C-shell script will determine the type of machine one is operating on,
# and will print the platform type.
#
#   Copyright (C) 1996-2004  Richard Gooch
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#
#   Richard Gooch may be reached by email at  karma-request@atnf.csiro.au
#   The postal address is:
#     Richard Gooch, c/o ATNF, P. O. Box 76, Epping, N.S.W., 2121, Australia.

# Written by		Richard Gooch   2-MAY-1996

# Updated by		Richard Gooch   26-APR-2002

# Updated by		Richard Gooch   25-SEP-2003: Switched away from using
# uname -a because it's fragile.

# Last updated by	Richard Gooch   13-MAY-2004: Added support for x86_64.

# $Id: uname_to_platform,v 2.1 2011/04/21 00:44:57 cal103 Exp $


set unknown = "UNKNOWN_unknown"

# Search for uname programme. Must be sure to avoid the GNU version which does
# not output the same as the vendor's version (and the vendor *is* correct).
foreach i (/bin/uname /usr/bin/uname)
    if ( ("$?uname_path" == "0") && (-x $i) ) then
	set uname_path = $i
    endif
end
if ("$?uname_path" == "0") then
    echo "$unknown"
    exit 1
endif

set os_uname = "`$uname_path -s`"

# Linux
if ("$os_uname" == "Linux") then
    set OS = "Linux"
    set machine = "`$uname_path -m`"
    # uname -p yields "unknown" on many machines
    switch ("$machine")
      case "i386":
      case "i486":
      case "i586":
      case "i686":
	set MACHINE = "i386"
	breaksw
      case "sparc":
      case "sparc64":
	set MACHINE = "sparc"
	breaksw
      case "ppc":
	set MACHINE = "powerpc"
	breaksw
      case "x86_64":
	set MACHINE = "amd64"
	breaksw
      default:
	echo "$unknown"
	exit 2
	breaksw
    endsw
    echo "${MACHINE}_${OS}"
    exit 0
endif

# 64 bit IRIX
if ("$os_uname" == "IRIX64") then
    echo "mips4_IRIX6"
    exit 0
endif

# 32 bit IRIX
if ("$os_uname" == "IRIX") then
    set os_rev = `$uname_path -r | sed -e 's/\./ /'`
    echo "mips2_IRIX${os_rev[1]}"
    exit 0
endif

# SunOS 4.x and Solaris 2.x
if ("$os_uname" == "SunOS") then
    # Sun, in their arrogance, call Solaris 2 "SunOS 5.x". Nobody is fooled.
    set os_rev = `$uname_path -r | sed -e 's/\./ /'`
    switch ("$os_rev[1]")
      case "4":
	set OS = "SunOS"
	if (`$uname_path -m | fgrep -c "sun4"` == "0") then
	    echo "$unknown"
	    exit 2
	endif
	set MACHINE = "sparc"
	breaksw
      case "5":
	set OS = "Solaris"
	if ("`$uname_path -p`" != "sparc") then
	    echo "$unknown"
	    exit 2
	endif
	set MACHINE = "sparc"
	breaksw
      default:
	echo "$unknown"
	exit 2
	breaksw
    endsw
    echo "${MACHINE}_${OS}"
    exit 0
endif

# OSF/1
if ("$os_uname" == "OSF1") then
    set OS = "OSF1"
    set machine = "`$uname_path -m`"
    switch ("$machine")
      case "alpha":
	set MACHINE = "alpha"
	breaksw
      default:
	echo "$unknown"
	exit 2
	breaksw
    endsw
    echo "${MACHINE}_${OS}"
    exit 0
endif

# HP/UX
if ("$os_uname" == "HP-UX") then
    echo "hp9000_HPUX"
    exit 0
endif

# ULTRIX
if ("$os_uname" == "ULTRIX") then
    echo "mips1_ULTRIX"
    exit 0
endif

# AIX
if ("$os_uname" == "AIX") then
    echo "rs6000_AIX"
    exit 0
endif

# Darwin (FreeBSD on Macintosh)
if ("$os_uname" == "Darwin") then
    echo "${MACHTYPE}_Darwin"
    exit 0
endif

# Don't know what this is
echo "$unknown"
exit 2

# Sample output of uname -a
#
#Linux workaholix 1.3.88 #1 Mon Apr 15 12:05:07 EST 1996 i586
#IRIX64 raptor 6.0.1 03302045 IP21 mips
#SunOS phoenix 5.5 Generic sun4m sparc SUNW,SPARCsystem-600
#SunOS lynx 4.1.3 11 sun4m
#OSF1 kaputar.atnf.csiro.au V3.2 148 alpha
#HP-UX rzmws7 A.09.01 A 9000/720 2002130964 two-user license
#ULTRIX quiros 4.4 0 RISC
#AIX cass 1 4 000055587100
#Linux localhost.localdomain 2.4.18-0.8a #1 Sat Mar 9 22:54:21 EST 2002 ppc unknown
#Linux sable 2.6.6 #1 Thu May 13 20:05:59 MDT 2004 x86_64 GNU/Linux
