#!/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-2019 amaury   darsch                                     -
# - author (c) 2010-2013 nobuhiro iwamatsu                  superh processor -
# - author (c) 2011-2013 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"
# the processor size
cpubit="unknown"
# 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"
# print the processor size only
shwbit="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;;
    aarch64*)  cpunam=aarch64;;
    ppc)       cpunam=ppc;;
    ppc64le)   cpunam=ppc64el;;
    m68k)      cpunam=m68k;;
    mips)      cpunam=mips;;
    mipsel)    cpunam=mipsel;;
    mips64)    cpunam=mips64;;
    mips64el)  cpunam=mips64el;;
    parisc*)   cpunam=pa64;;
    ia64)      cpunam=ia64;;
    s390*)     cpunam=s390;;
    x86_64)    cpunam=x64;;
    amd64)     cpunam=x64;;
    Power*)    cpunam=ppc;;
    sh*)       cpunam=sh;;
    riscv32)   cpunam=riscv32;;
    riscv64)   cpunam=riscv64;;
    esac
}

# get the machine size
get_cpubit ()
{
    name=`uname -m`
    case $name in
    i386)    cpubit=32;;
    i486)    cpubit=32;;
    i586)    cpubit=32;;
    i686*)   cpubit=32;;
    alpha)   cpubit=64;;
    sun4*)   cpubit=32;;
    sparc*)  cpubit=64;;
    arm*)    cpubit=32;;
    ppc)     cpubit=32;;
    ppc64*)  cpubit=64;;
    m68k)    cpubit=32;;
    mips)    cpubit=32;;
    mipsel)  cpubit=32;;    
    mips64*) cpubit=64;;    
    parisc*) cpubit=64;;
    ia64)    cpubit=64;;
    x86_64)  cpubit=64;;
    amd64)   cpubit=64;;
    Power*)  cpubit=32;;
    sh*)     cpubit=32;;
    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";;
    -s | --showbits)  shwbit="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
if test "$shwbit" = "yes"; then
get_cpubit
echo ${cpubit}
exit 0
fi

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