#!/usr/bin/env bash

set -e

name=`basename "$0"`
dir=`dirname "$0"`
bin=`cd "$dir" && pwd`

die () {
        echo >&2 "$1"
        exit 1
}

usage () {
        die "usage: $name"
}

case "$#" in
0)
;;
*)
        usage
;;
esac

uname=`uname`
arch=

case "$uname" in
AIX)
        HOST_OS='aix'
        arch=`/usr/sbin/lsattr -a type -F deflt -l proc0`
        case $arch in
        POWER) arch=powerpc64 ;;
        esac
;;
CYGWIN*)
        HOST_OS='cygwin'
;;
Darwin)
        HOST_OS='darwin'
;;
*FreeBSD*)
        HOST_OS='freebsd'
;;
GNU)
        HOST_OS='hurd'
;;
HP-UX)
	HOST_OS='hpux'
;;
Linux)
        HOST_OS='linux'
;;
MINGW*)
        HOST_OS='mingw'
;;
NetBSD*)
        HOST_OS='netbsd'
;;
OpenBSD*)
        HOST_OS='openbsd'
;;
SunOS)
        HOST_OS='solaris'
	for arch in sparc amd64 i386 `uname -m`; do
	        optisa $arch > /dev/null && break
	done
;;
*)
        die "Unknown OS $uname."
;;
esac

if [ -z "$arch" ]; then
        arch=`uname -m`
fi

case "$arch" in
alpha*)
# not certain about this one; no alpha access
        HOST_ARCH=alpha
;;
x86_64*)
        HOST_ARCH=amd64
;;
i?86_64)
        HOST_ARCH=amd64
;;
amd64)
        HOST_ARCH=amd64
;;
arm*)
        HOST_ARCH=arm
;;
parisc*)
        HOST_ARCH=hppa
;;
9000/*)
        HOST_ARCH=hppa
;;
ia64*)
        HOST_ARCH=ia64
;;
m68k*)
        HOST_ARCH=m68k
;;
mips*)
# big-endian and little-endian detect via headers
        HOST_ARCH=mips
;;
powerpc64)
	HOST_ARCH=powerpc64
;;
powerpc)
        HOST_ARCH=powerpc
;;
ppc*)
        HOST_ARCH=powerpc
;;
Power*)
        HOST_ARCH=powerpc
;;
s390*)
        HOST_ARCH=s390
;;
sparc*)
        HOST_ARCH=sparc
;;
sun*)
        HOST_ARCH=sparc
;;
i?86*)
        HOST_ARCH=x86
;;
*)
        die "Unknown arch $arch."
;;
esac

echo "HOST_OS=$HOST_OS HOST_ARCH=$HOST_ARCH"
