#!/bin/sh
# ----------------------------------------------------------------------------
# - afnix-guess                                                              -
# - afnix platform guess program                                             -
# ----------------------------------------------------------------------------
# - This program is  free software;  you can  redistribute it and/or  modify -
# - it provided that this copyright notice is kept intact.                   -
# -                                                                          -
# - 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. In not event shall -
# - the copyright holder be  liable for  any direct, indirect, incidental or -
# - special damages arising in any way out of the use of this software.      -
# ----------------------------------------------------------------------------
# - author (c) 1999-2012 amaury   darsch                                     -
# - author (c) 2010-2012 nobuhiro iwamatsu                  superh processor -
# - author (c) 2011-2012 pino     toscano                      hurd platform -
# ----------------------------------------------------------------------------

# ----------------------------------------------------------------------------
# - set default variables                                                    -
# ----------------------------------------------------------------------------

# the operating system name
pltnam="unknown"
# the operating system version
pltvrs="unknown"
pltmaj="0"
pltmin="0"
# the processor name
cpunam="unknown"
# the processor type
cputyp="generic"
# show the platform name only
shwplt="no"
# print the platform version only
shwvrs="no"
# print the platform major number only
shwmaj="no"
# print the platform minor number only
shwmin="no"
# print the processor name only
shwcpu="no"
# print the processor type only
shwtyp="no"

# ----------------------------------------------------------------------------
# - local function always make life easier                                   -
# ----------------------------------------------------------------------------

# print a usage message
usage () {
    echo "usage: afnix-guess [options]"
    echo "       -h           print this help message"
    echo "       -n           print the platform name"
    echo "       -v           print the platform version"
    echo "       -M           print the platform major number"
    echo "       -m           print the platform minor number"
    echo "       -p           print the processor name"
    echo "       -t           print the processor type"
    exit 0
}

# print an error message
error () {
    echo "error: $1"
    exit 1
}

# get the platform name
get_pltnam ()
{
    name=`uname -s`
    case $name in
    Linux)        pltnam=linux;;
    SunOS)        pltnam=solaris;;
    FreeBSD)      pltnam=freebsd;;
    Darwin)       pltnam=darwin;;
    GNU/kFreeBSD) pltnam=gnukbsd;;
    GNU)          pltnam=gnu;;
    esac
}

# get the platform version
get_pltvrs ()
{
    # compute major, minor and patch number
    version=`uname -r | sed 's/\./ /g'`
    plmajor=`echo $version | awk '{print $1}'`
    plminor=`echo $version | awk '{print $2}'`
    plpatch=`echo $version | awk '{print $3}'`

    # extract platform normalized version
    pltmaj=$(($plmajor+0))
    pltmin=$(($plminor+0))
    pltvrs=${pltmaj}.${pltmin}
}

# get the processor info
get_cpunam ()
{
    name=`uname -m`
    case $name in
    i386)    cpunam=ia32;;
    i486)    cpunam=ia32;;
    i586)    cpunam=ia32;
	     cputyp=586;;
    i686*)   cpunam=ia32;
	     cputyp=686;;
    alpha)   cpunam=alpha;;
    sun4*)   cpunam=sparc;;
    sparc*)  cpunam=sparc;;
    arm*)    cpunam=arm;;
    ppc)     cpunam=ppc;;
    m68k)    cpunam=m68k;;
    mips)    cpunam=mips;;
    mipsel)  cpunam=mipsel;;    
    parisc*) cpunam=pa64;;
    ia64)    cpunam=ia64;;
    s390*)   cpunam=s390;;
    x86_64)  cpunam=x64;;
    amd64)   cpunam=x64;;
    Power*)  cpunam=ppc;
    esac
}

# ----------------------------------------------------------------------------
# - parse options - this is where we really start                            -
# ----------------------------------------------------------------------------

preopt=
for nxtopt
do
    # assign the previous option argument
    if test -n "$preopt"; then
	eval "$preopt=\$nxtopt"
	preopt=
	continue
    fi

    # extract options
    case "$nxtopt" in
    -*=*) argopt=`echo "$nxtopt" | sed 's/[-_a-zA-Z0-9]*=//'`;;
       *) argopt=;;
    esac

    # process options now
    case "$nxtopt" in
    -h | --help)      usage;;
    -n | --platname)  shwplt="yes";;
    -v | --platvers)  shwvrs="yes";;
    -M | --platvmaj)  shwmaj="yes";;
    -m | --platvmin)  shwmin="yes";;
    -p | --procname)  shwcpu="yes";;
    -t | --proctype)  shwtyp="yes";;
    *)                error "afnix-guess: illegal option $nxtopt";;
    esac
done

# get system info
get_pltnam
get_pltvrs
get_cpunam

# build result variable
if test "$pltnam" = "unknown"; then
    echo "afnix-guess: cannot determine the platform name"
    exit 1
fi

if test "$pltvrs" = "unknown"; then
    echo "afnix-guess: cannot determine the platform version"
    exit 1
fi

if test "$cpunam" = "unknown"; then
    echo "afnix-guess: cannot determine the processor name"
    exit 1
fi

# print selectively
if test "$shwplt" = "yes"; then
echo ${pltnam}
exit 0
fi
if test "$shwvrs" = "yes"; then
echo ${pltvrs}
exit 0
fi
if test "$shwmaj" = "yes"; then
echo ${pltmaj}
exit 0
fi
if test "$shwmin" = "yes"; then
echo ${pltmin}
exit 0
fi
if test "$shwcpu" = "yes"; then
echo ${cpunam}
exit 0
fi
if test "$shwtyp" = "yes"; then
echo ${cputyp}
exit 0
fi

# print the result
echo ${pltnam}-${pltvrs}-${cpunam}-${cputyp}
exit 0
