#!/bin/sh
# ----------------------------------------------------------------------------
# - afnix-vcomp                                                              -
# - afnix compiler version detection                                         -
# ----------------------------------------------------------------------------
# - 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 program name
progname=$0
# the platform name
platname=
# the compiler to check
compiler=
# result version
cversion=

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

# print a usage message
usage () {
    echo "usage: afnix-vcomp [options]"
    echo "       -h                 print this help message"
    echo "       --compiler=name    set the comppiler name"
    exit 0
}

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

# get the platform and the compiler
getdef () {
    # get the platform name
    guess=`dirname $progname`/afnix-guess
    platname=`$guess -n`

    # compute compiler definition if not defined
    if test -z "$compiler" ; then
	case $platname in
	linux)   compiler=gcc ;;
	freebsd) compiler=gcc ;;
	solaris) compiler=gcc ;;
	darwin)  compiler=gcc ;;
	gnukbsd) compiler=gcc ;;
	esac
    fi
}

# get the compiler version
getccv () {
    # check for gcc
    if test "$compiler" = "gcc" ; then
        # get gcc version 
	vers=`gcc -dumpversion`
	case $vers in
	2.*) cversion=2 ;;
	3.*) cversion=3 ;;
	4.*) cversion=4 ;;
	esac
    fi
    # check for g++
    if test "$compiler" = "g++" ; then
        # get gcc version 
	vers=`g++ -dumpversion`
	case $vers in
	2.*) cversion=2 ;;
	3.*) cversion=3 ;;
	4.*) cversion=4 ;;
	esac
    fi
}

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

# get the default settings
getdef

# parse the options
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 ;;

    --compiler)              lastopt=compiler ;;
    --compiler=*)            compiler="$argsopt" ;;

    *)                       error "illegal option $nextopt" ;;
    esac
done

# get the compiler version
getccv

# check for final result
if test -z "$cversion" ; then
    error "cannot find version for compiler $compiler"
fi

# format result
result=$compiler-$cversion
echo $result
exit 0
