#!/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.      -
# ----------------------------------------------------------------------------
# - author (c) 1999-2017 amaury darsch                                       -
# - author (c) 2016-2016 martin michlmayr                              GCC 5 -
# - author (c) 2017-2017 nobuhiro iwamatsu                             GCC 7 -
# ----------------------------------------------------------------------------

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

# the program name
prgnam="$0"
# the platform name
pltnam=
# the ccname to check
ccname=
# the cc name to map
ccmake=
# result version
ccvers=

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

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

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

# get the platform and the ccname
getdef () {
    # get the platform name
    basdir=`dirname $prgnam`
    pltexe="$basdir/afnix-guess"
    pltnam=`$pltexe -n`
    # get the compiler name if not defined
    if test -z "$ccname" ; then
	qryexe="$basdir/afnix-query"
	ccname=`$qryexe --default=gcc compiler`
    fi
}

# get the compiler makefile
getccm () {
    # check for gcc
    if test "$ccname" = "gcc" ; then
	ccmake="gcc"
    fi
    # check for g++
    if test "$ccname" = "g++" ; then
	ccmake="gcc"
    fi
    # check for clang
    if test "$ccname" = "clang"; then
	ccmake="clg"
    fi
    # check for clang++
    if test "$ccname" = "clang++"; then
	ccmake="clg"
    fi
}

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

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

# get the default settings
getdef

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

    --compiler)    preopt=ccname;;
    --compiler=*)  ccname="$argopt";;

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

# get the cc make/version
getccm
getccv
# check for final result
if test -z "$ccmake" ; then
    error "cannot find compile makefile for $ccname"
fi
if test -z "$ccvers" ; then
    error "cannot find version for compiler $ccname"
fi

# format result
result=${ccmake}${ccvers}
echo $result
exit 0
