#!/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.      -
# ----------------------------------------------------------------------------
# - copyright (c) 1999-2007 amaury darsch                                    -
# ----------------------------------------------------------------------------

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

# the operating system name
plat_name=unknown
# the operating system version
plat_vers=unknown
plat_vmaj=0
plat_vmin=0
# the processor name
proc_name=unknown
# the processor type
proc_type=generic
# the result platform name
platform=unknown
# print the platform name only
print_plat="no"
# print the platform version only
print_vers="no"
# print the platform major number only
print_vmaj="no"
# print the platform minor number only
print_vmin="no"
# print the processor name only
print_proc="no"
# print the processor type only
print_type="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_plat_name ()
{
    name=`uname -s`
    case $name in
    Linux)        plat_name=linux;;
    SunOS)        plat_name=solaris;;
    FreeBSD)      plat_name=freebsd;;
    Darwin)       plat_name=darwin;;
    GNU/kFreeBSD) plat_name=gnukbsd;;
    esac
}

# get the platform version
get_plat_vers ()
{
    # 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
    plat_vmaj=$(($plmajor+0))
    plat_vmin=$(($plminor+0))
    plat_vers=${plat_vmaj}.${plat_vmin}
}

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

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

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

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

    # process options now
    case "$nextopt" in
    -h | --help)             usage ;;
    -n | --platname)         print_plat="yes" ;;
    -v | --platvers)         print_vers="yes" ;;
    -M | --platvmaj)         print_vmaj="yes" ;;
    -m | --platvmin)         print_vmin="yes" ;;
    -p | --procname)         print_proc="yes" ;;
    -t | --proctype)         print_type="yes" ;;
    *)                       error "afnix-guess: illegal option $nextopt" ;;
    esac
done

# get system info
get_plat_name
get_plat_vers
get_proc_name

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

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

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

# print selectively
if test "$print_plat" = "yes"; then
echo ${plat_name}
exit 0
fi
if test "$print_vers" = "yes"; then
echo ${plat_vers}
exit 0
fi
if test "$print_vmaj" = "yes"; then
echo ${plat_vmaj}
exit 0
fi
if test "$print_vmin" = "yes"; then
echo ${plat_vmin}
exit 0
fi
if test "$print_proc" = "yes"; then
echo ${proc_name}
exit 0
fi
if test "$print_type" = "yes"; then
echo ${proc_type}
exit 0
fi

echo ${plat_name}-${plat_vers}-${proc_name}-${proc_type}
exit 0