SPATH=`echo $PATH | tr ' ' '\177' | tr ':' ' '`

pathcheck ()
{
    for i in $SPATH
    do
        PATHCHECK_FOUND="`echo $i | tr '\177' ' '`/$1"
        test -f "$PATHCHECK_FOUND" && return 0
    done
    PATHCHECK_FOUND=
    return 1
}

convert_windows_path ()
{
    case `uname` in
        CYGWIN_NT*)
            if [ ! -d "$1" ]
            then
                echo "ERROR: Path $1 does not exist"
                return 1
            fi
            #Make it a dos path to remove all spaces
            DOS_CONV_PATH=`cygpath -d "$1"`
            if [ $? != 0 ]
            then
                echo "ERROR: Could not convert to dos path $1"
                return 1
            fi
            #Now turn into a unix path for useage
            UNIX_CONV_PATH=`cygpath -u $DOS_CONV_PATH 2>/dev/null`
            if [ $? != 0 ]
            then
                echo "ERROR: Could not convert to unix path $1"
                return 1
            fi
            ;;
        *)
            UNIX_CONV_PATH="$1"
            DOS_CONV_PATH="$1"
            ;;
    esac
    return 0
}

get_vs_commontools()
{
    # use the Visual Studio defined in VS_HOME if this is not set look for other installed Visual Studio
    # versions and take the highest version
    if [ -n "$VS_HOME" ]; then echo "$(cygpath -w $VS_HOME)/VC/"; return; fi
    if [ -n "$VS140COMNTOOLS" ]; then echo "%VS140COMNTOOLS%../../VC/"; return; fi
    if [ -n "$VS120COMNTOOLS" ]; then echo "%VS120COMNTOOLS%../../VC/"; return; fi
    if [ -n "$VS110COMNTOOLS" ]; then echo "%VS110COMNTOOLS%../../VC/"; return; fi
    if [ -n "$VS100COMNTOOLS" ]; then echo "%VS100COMNTOOLS%../../VC/"; return; fi
    if [ -n "$VS90COMNTOOLS" ]; then echo "%VS90COMNTOOLS%../../VC/"; return; fi
    if [ -n "$VS80COMNTOOLS" ]; then echo "%VS80COMNTOOLS%../../VC/"; return; fi
}

function setVSEnv()
{
    # This function sets the env. variables required by Visual Studio, in a Bash shell, using a Windows cmd prompt and the vcvarsall.bat script
    [[ -z "$1" ]] && { echo "'VS_HOME' parameter is required!"; return 1; }

    ARCH="x86"
    case ${SPLICE_TARGET} in
    x86_64.*)
        ARCH="x64"
        ;;
    esac

    local cmd
    # Repository can be located on a different drive than the system drive, in
    # order to make the command below work in that scenario, first switch to
    # the drive on which Program Files is located.
    local drv=$(echo "${VS_HOME}" | perl -ne 'print "$1$2" if (m#^(?:([a-z]):|/cygdrive/([a-z])/)#i)')

    #needed to check if we have a VS > 2015 as the vsvarsall.bat has been renamed to VsDevCmd.bat
    vsvarscheck="${VS_HOME}/VC/vcvarsall.bat"
    if [ -f "$vsvarscheck" ]; then
        local cmd=$(printf "cmd /V /C %s: && cd %s && vcvarsall.bat %s 1>NUL && echo LIB=!LIB! && echo INCLUDE=!INCLUDE! && echo PATH=!PATH! && echo FrameworkSDKDir=!FrameworkSDKDir! && echo WindowsSdkDir=!WindowsSdkDir!" "$drv" "$1" "$ARCH")
    else
        vshome_norm=$(cygpath -w $VS_HOME)
        vs_gt_2015_path=${vshome_norm}/Common7/Tools/
        local cmd=$(printf "cmd /V /C %s: && cd %s && VsDevCmd.bat -arch=%s 1>NUL && echo LIB=!LIB! && echo INCLUDE=!INCLUDE! && echo PATH=!PATH! && echo FrameworkSDKDir=!FrameworkSDKDir! && echo WindowsSdkDir=!WindowsSdkDir!" "$drv" "$vs_gt_2015_path" "$ARCH")
    fi

    # PATH is converted by cygwin automatically, others are not. Unset them to prevent corruption in case this function is executed more than once
    [[ -n "$LIB" ]] && { echo "Warning: LIB already set!"; unset LIB; }
    [[ -n "$INCLUDE" ]] && { echo "Warning: INCLUDE already set!"; unset INCLUDE; }
    [[ -n "$FrameworkSDKDir" ]] && { echo "Warning: FrameworkSDKDir already set!"; unset FrameworkSDKDir; }

    local env

    while IFS= read -r line; do
        local var_name=${line%=*}
        local value_win=${line#*=}
        value_win="${value_win#"${value_win%%[![:space:]]*}"}"   # remove leading whitespace characters
        value_win="${value_win%"${value_win##*[![:space:]]}"}"   # remove trailing whitespace characters

        # For each ';'-separated windows path in $value_win, convert to posix format and append to ':'-separated $value_cyg
        if [[ $var_name == "PATH" ]]; then
            oldifs=$IFS
            IFS=";"
            for seg in $value_win; do
                [[ "${#seg}" -gt 1 ]] && {
                    seg="$(cygpath -u $seg)"
                    #[[ -d "$seg" ]] || { echo "Directory '$seg' (in $var_name) doesn't exist! (Skipped)"; continue; }
                    value_cyg+="${seg}:"
                }
            done

            IFS=$oldifs
            env+=$var_name=${value_cyg%?}$'\n' # strip trailing ':'
        elif [[ $var_name == "WindowsSdkDir" && $value_win != "!WindowsSdkDir!" ]]; then
            env+=FrameworkSDKDir=$(cygpath -u "$value_win")$'\n'
        elif [[ $var_name == "FrameworkSDKDir" && $value_win != "!FrameworkSDKDir!" ]]; then
            env+=$var_name=$(cygpath -u "$value_win")$'\n'
        else
            # the LIB and INCLUDE vars need to stay in windows format
            env+=$var_name=${value_win}$'\n'
        fi


    done < <($cmd)

    # Export each env. var in $env
    while IFS=$'\n' read -r var; do
        export "$var"
    done <<< "${env%?}" # strip trailing '\n'

    return 0
}

csharp_check ()
{
    echo -n "C#: "
    CSHARP_VER=`csc -help 2>&1 | tr -d '\r' | head -1 | sed 's@\(.*version \)\([0-9][0-9\.]*\)\(.*\)$@\2@'`
    if [ -z "$CSHARP_VER" ]
    then
        echo "ERROR - C# Compiler version cannot be determined"
        return 1
    fi
    echo "OK - using version $CSHARP_VER"
    return 0
}

msvs_check ()
{
#   VS_VER 19 Windows 10 / VS2015 / .NET framework V4.6 / SDK v10.0A
#   VS_VER 18 Windows 8.1 / VS2013 / .NET framework V4.5.1 / SDK 8.1A
#   VS_VER 17 Windows 8 / VS2012 / .NET framework V4.5 / SDK 8.0A
#   VS_VER 16 Windows 7 / VS2010 / .NET framework V4.0.30319 / SDK 7.0A
#   VS_VER 15 Windows Vista / VS2008 / .NET framework V3.5 / SDK 6.0A
#   VS_VER 14 Windows XP SP2 / VS2005 / .NET framework V3.0 / SDK ?

    echo -n "VS: "
    VSHOME=$(get_vs_commontools)
    setVSEnv "$VSHOME"
    VS_OK=$?

    if [ $VS_OK == 1 ]
    then
        echo "FAILED"
        return 1
    else
        COMPILER_VER=`cl -help 2>&1 | tr -d '\r' | head -1 | sed 's@\(.*Version \)\([0-9][0-9\.]*\)\(.*\)$@\2@'`
        if [ -z "$COMPILER_VER" ]
        then
            echo "ERROR - Compiler version cannot be determined"
            return 1
        fi
        set_var OSPL_COMPILER_VER "$COMPILER_VER"
        VS_VER=`echo $COMPILER_VER | cut -f 1 -d .`
        set_var VS_VER "$VS_VER"

        # needed for installer names
        if [ "$VS_VER" == "14" ]; then
            VS_YEAR=2005
            DOTNET="3.0"
        elif [ "$VS_VER" == "15" ]; then
            VS_YEAR=2008
            DOTNET="3.5"
        elif [ "$VS_VER" == "16" ]; then
            VS_YEAR=2010
            DOTNET="4.0"
        elif [ "$VS_VER" == "17" ]; then
            VS_YEAR=2012
            DOTNET="4.5"
        elif [ "$VS_VER" == "18" ]; then
            VS_YEAR=2013
            DOTNET="4.5.1"
        elif [[ $OSPL_COMPILER_VER == "19.0"* ]]; then
            VS_YEAR=2015
            DOTNET="4.6"
        elif [[ $OSPL_COMPILER_VER == "19.1"* ]]; then
            VS_YEAR=2017
            DOTNET="4.6.1"
        elif [[ $OSPL_COMPILER_VER == "19.2"* ]]; then
            VS_YEAR=2019
            DOTNET="4.7.2"
        else
            VS_YEAR=Unknown
            DOTNET="Unknown"
        fi

        set_var VS_YEAR "$VS_YEAR"
        set_var DOTNET "$DOTNET"
        # from VS 2015 update 3 and above C11 is enabled so enable it for the build too.
        if [ $VS_VER -gt 18 ]
        then
            set_var OSPL_USE_CXX11 "yes"
        fi


        # only generate an AssemblyAttributes for vs2010 and newer lower is not supported
        if [ $VS_VER -gt 15 ]
        then
            sed -e "s;%DOTNET%;$DOTNET;g" $OSPL_HOME/src/api/dcps/sacs/AssemblyAttributesTemplate.cs > $OSPL_HOME/src/api/dcps/sacs/code/DDS/AssemblyAttributes.cs
        fi

        DOTNETPROGFILES=$PROGRAMFILES

        case ${SPLICE_HOST} in
        x86_64.*)
            DOTNETPROGFILES="C:\\Program Files (x86)"
            ;;
        esac

        #fallback for when the host is 32 bit but the actual platform is 64 bit
        if [[ "$PROCESSOR_ARCHITECTURE" == "AMD64" ]]; then
             DOTNETPROGFILES="C:\\Program Files (x86)"
        fi

        if [[ "$PROCESSOR_ARCHITEW6432" == "AMD64" ]]; then
             DOTNETPROGFILES="C:\\Program Files (x86)"
        fi
        set_var DOTNETPROGFILES "$DOTNETPROGFILES"

        echo "OK - using compiler version: $COMPILER_VER for VS: $VS_YEAR and .Net $DOTNET and Program Files $DOTNETPROGFILES"
    fi



    # Right now we only absolutely need devenv for
    # ISO C++
    if [ "$INCLUDE_API_DCPS_ISOCPP" = "yes" -o "$INCLUDE_API_DCPS_ISOCPP2" = "yes" ]
    then
        echo -n "Visual Studio builder: "
        which devenv.com > /dev/null 2>&1
        if [ $? != 0 ]
        then
            which VCExpress.exe > /dev/null 2>&1
            if [ $? != 0 ]
            then
               which WDExpress.exe > /dev/null 2>&1
               if [ $? != 0 ]
               then
                    printf "ERROR: devenv.com or VCExpress.exe not available on the path when Visual Studio compilation configured.\n"
                    printf "PATH is $PATH\n"
                    printf "VS_HOME is $VS_HOME\n"
                    return 1
                else
                    set_var OSPL_DEVENV WDExpress.exe
                fi
            else
                set_var OSPL_DEVENV VCExpress.exe
            fi
        else
            set_var OSPL_DEVENV devenv.com
        fi
        echo "OK - using $OSPL_DEVENV"
    fi
    return 0
}

# test if gcc is at least v3.2
gcc_check ()
{
    echo -n "GCC: "
    pathcheck gcc
    if [ $? != 0 ]; then
        echo "ERROR - Not found"
        return 1
    fi

    GCC_FULLVERSION=`gcc -dumpversion`
    GCC_MAJOR=`echo $GCC_FULLVERSION | cut -f 1 -d .`
    GCC_MINOR=`echo $GCC_FULLVERSION | cut -f 2 -d .`
    export GCC_MAJOR GCC_MINOR

    # GCC < 3.2 not supported
    if echo $GCC_FULLVERSION | gawk '{ exit $1 < 3.2 ? 0 : 1 }'; then
        echo "ERROR - Only GCC >= 3.2 is supported (found: $GCC_FULLVERSION)"
        return 1
    fi

    echo "OK - Using version $GCC_FULLVERSION"

    # GCC >= 4.2 supports -Werror=switch
    GCC_WERROR_IS_SWITCH_SUPPORT=`echo $GCC_FULLVERSION | gawk '{ print $1 >= 4.2 ? 1 : 0 }'`
    set_var GCC_WERROR_IS_SWITCH_SUPPORT "$GCC_WERROR_IS_SWITCH_SUPPORT"

    # GCC >= 4.3 supports C++11
    GCC_SUPPORTS_CPLUSPLUS11=`echo $GCC_FULLVERSION | gawk '{ print $1 >= 4.3 ? 1 : 0 }'`
    set_var GCC_SUPPORTS_CPLUSPLUS11 "$GCC_SUPPORTS_CPLUSPLUS11"

    # GCC >= 4.4 supports -Wconversion (too many false positives in 4.3)
    GCC_WCONVERSION_IS_SUPPORTED=`echo $GCC_FULLVERSION | gawk '{ print $1 >= 4.4 ? 1 : 0 }'`
    set_var GCC_WCONVERSION_IS_SUPPORTED "$GCC_WCONVERSION_IS_SUPPORTED"

    # GCC >= 4.7 supports -flto (except 5.4, due to bug in linker: OSPL-10875)
    # Also at GCC >= 4.7 we disable maybe-uninitialized errors for some 3rd party code
    if echo $GCC_FULLVERSION | gawk '{ exit $1 >= 4.7 ? 0 : 1 }'; then
        case "${GCC_FULLVERSION}" in
          5.4*)
            ;;
          *)
            echo "  NOTE - Enable link-time optimizations (for release build)"
            set_var GCC_SUPPORTS_LTO 1
            if ! pathcheck gcc-ar; then
                echo "  WARNING - Unable to find gcc-ar (could result in link errors)"
            fi
            ;;
        esac
        set_var GCC_HAS_NO_MAYBE_UNINITIALIZED yes
    fi

    return 0
}

# test that we are on glibc and get its version string
glibc_check ()
{
    GLIBC_VERSION=`getconf GNU_LIBC_VERSION | cut -f 2 -d ' '`
    if test -z $GLIBC_VERSION
    then
        echo "ERROR - no or unidentifed glibc"
        return 1
    else
        echo "GLIBC: version $GLIBC_VERSION"
        return 0
    fi
}

# test if we are truing to use valgrind it is in the path
valgrind_check ()
{
    if [ "$VALGRIND" = "yes" ]
    then
        echo -n "Valgrind: "
        pathcheck valgrind
        if [ $? != 0 ]
        then
            echo "ERROR - Not found"
            return 1
        fi
        echo "OK - using version" `valgrind --version`
        if [ "$FORCE_DEBUG_SYMBOLS" != "yes" ]
        then
            echo "   - Warning debug symbols may not be compiled in"
            echo "   - set OVERRIDE_FORCE_DEBUG_SYMBOLS to yes to added"
            echo "   - compiler debug symboles"
        fi
        #source in the valgrind commands that are used
        #setup where the default logs will go
        #will be overridden when ran in the build system
        export VG_LOG_DIR=.
        . "$OSPL_HOME/bin/valgrind_cmds"
    fi
    return 0
}

# test if sun studio is at version 12
sun_cc_studio12_check ()
{
    echo -n "cc: "
    pathcheck cc
    if [ $? != 0 ]
    then
        echo "ERROR - Not found"
        return 1
    fi
    echo "OK - using version" `cc -V 2>&1 |  grep '^cc:'`
    return 0
}

check_and_set_native_dir ()
{
    if [ ! -d "${!1}" ]
    then
       echo "ERROR: Value of \$${1} : ${!1} does not exist "
       return 1
    fi
    case `uname` in
        CYGWIN_NT*)
            WINDOWS_PATH=`cygpath -w ${!1} 2>/dev/null`
            if [ $? == 0 ]
            then
                echo "OK. \$${1} was ${!1}, set to $WINDOWS_PATH"
                set_var ${1} ${WINDOWS_PATH}
            else
                echo "ERROR: Can't convert path ${!1} for \$${1} "
                return 1
            fi
            ;;
        *)
            if [ -d ${!1} ]
            then
                echo "OK. Set \$${1} to ${!1} "
                set_var ${1} ${!1}
            fi
            ;;
    esac
    return 0
}

qt_check ()
{
    echo -n "Qt: "
    if [ "$OSPL_QT_IS_ON" = "no" ]
    then
        echo "Disabled. \$OSPL_QT_IS_ON was: no"
        return 0
    fi
    if [ -n "$QTDIR" ]
    then
        if [ -f $QTDIR/bin/uic ]
        then
            QT_VERSION=`$QTDIR/bin/uic -version 2>&1 | sed 's/\([^0-9][^0-9]*\)\([0-9]\)\([^0-9].*\)/\2/'`
            echo "at_check (1) and QT_VERSION is $QT_VERSION"

            if [ "$QT_VERSION" != "4" ]
            then
                echo "Error \$QTDIR points to $QTDIR. This is not version 4. Please unset or set OSPL_QT_IS_ON=no"
                return 1
            fi
        fi
        check_and_set_native_dir QTDIR
        if [ $? = 0 ]
        then
            set_var OSPL_QT_IS_ON yes
            case `uname` in
            CYGWIN_NT*)
                convert_windows_path "$QTDIR"
                PATH="$UNIX_CONV_PATH/bin:$PATH"
                set_var PATH "$PATH"
                QTLIBDIR="$UNIX_CONV_PATH/lib"
                set_var QTLIBDIR "$QTLIBDIR"
                ;;
            *)
                PATH="$QTDIR/bin:$PATH"
                set_var PATH "$PATH"
                set_var QTLIBDIR $QTDIR/lib
                ;;
            esac
        else
            return 1
        fi
    else
        if [ -z $QTLIBDIR ]
        then
            linkpath=`ld --verbose | grep SEARCH_DIR | tr ' '  '\n' | sed -e 's/SEARCH_DIR("\=\?\(.*\)");/\1/g'`
            for i in $linkpath
            do
                if [ -f $i/libQtCore.so ]
                then
                    set_var QTLIBDIR $i
                fi
            done
        fi
        if [ -z $QTLIBDIR ]
        then
            echo "Cannot find Qt libraries. Standalone demo package will not be built. Please specifiy QTLIBDIR."
        fi
        case `uname` in
        CYGWIN_NT*)
            # Protect against 'cygwin' Qt. It can include uic-qt4.
            ;;
        *)
            pathcheck uic-qt4
            if [ $? = 0 ]
            then
                echo "On. Using QT tools from the path (with -qt4 suffix)."
                set_var QT4_TOOL_SUFFIX \-qt4
                set_var OSPL_QT_IS_ON yes
                return 0
            fi
            ;;
        esac
        pathcheck uic
        if [ $? = 0 ]
        then
            QT_VERSION=`uic -version 2>&1 | sed 's/\([^0-9][^0-9]*\)\([0-9]\)\([^0-9].*\)/\2/'`
            echo "at_check (2) and QT_VERSION is $QT_VERSION"

            if [ "$QT_VERSION" != "4" ]
            then
                echo "Off. Qt tools on the path are not version 4."
                return 0
            fi
            echo "On. Using QT tools from the path."
            set_var OSPL_QT_IS_ON yes
        else
            echo "Off. Ok, but you're missing out on bouncing shapes..."
        fi
    fi
    return 0
}

perl_check ()
{
    echo -n "Perl: "
    pathcheck perl
    if [ $? != 0 ]
    then
        echo "ERROR - Not found"
        return 1
    fi
    PERL_VER=`perl -V:version`
    echo "OK - using perl $PERL_VER"
    return 0
}

boost_check ()
{
    if [ -n "$BOOST_ROOT" ]
    then
        echo -n "Boost: "
        check_and_set_native_dir BOOST_ROOT
        if [ $? != 0 ]
        then
            return 1
        else
            convert_windows_path "$BOOST_ROOT"
            set_var BOOST_ROOT_UNIX $UNIX_CONV_PATH
        fi
    fi
}

# There are currently 56 individual config files in git that call make_check.
# There are (AFAIK) none that don't
global_checks ()
{
   ERRS=0
   perl_check || ERRS=1
   qt_check || ERRS=1
   boost_check || ERRS=1
   return $ERRS
}

# require GNU make 3.80 or 3.81
make_check ()
{
    echo -n "MAKE: "
    pathcheck make
    if [ $? != 0 ]
    then
        echo "ERROR - Not found"
        return 1
    fi

    MAKE_MAJOR=`make -v 2>/dev/null | head -1 | cut -f 1 -d . | cut -f 3 -d ' '`
    if [ $MAKE_MAJOR -lt 3 ]
    then
        echo "ERROR - Only make 3.80 and above is supported"
        return 1
    fi

    MAKE_MINOR=`make -v 2>/dev/null | head -1 | cut -f 2 -d .`
    if [ $MAKE_MAJOR -eq 3 -a $MAKE_MINOR -lt 80 ]
    then
        echo "ERROR - Only make 3.80 and above is supported"
        return 1
    fi

    echo "OK - using `make -v 2>/dev/null | head -1`"
    global_checks
    return $?
}

gsoap_check ()
{
    # disabling the cmsoap subsystem is not sufficient
    return 0

    echo -n "GSOAP: "
    # Check GSOAPHOME directory first
    if [ -n "$GSOAPHOME" ]
    then
        if [ ! -d "$GSOAPHOME" ]
        then
            echo "Error - $GSOAPHOME is invalid"
            return 1
        fi
        if [ ! -f "$GSOAPHOME/bin/soapcpp2" ]
        then
            echo "Error - Invalid Setup"
            return 1
        fi
        SOAPCPP2=soapcpp2
        PATH="$GSOAPHOME/bin:$PATH"
    else
        # If not check the path for soapcpp2
        pathcheck soapcpp2
        if [ $? != 0 ]
        then
            if [ -n "$OSPL_OUTER_HOME" ]
            then
                echo "Error - Not found"
                return 1
            else
                echo "Warning - Not found, cmsoap will not be built"
                unset_var INCLUDE_SERVICES_CMSOAP
                return 0
            fi
        fi
        SOAPCPP2_EXE="$PATHCHECK_FOUND"
        GSOAPHOME1=`dirname "$SOAPCPP2_EXE"`
        GSOAPHOME=`dirname "$GSOAPHOME1"`

        if [ -z "$OSPL_OUTER_HOME" ]
        then
            GSOAP_SRC_FILE=$GSOAPHOME/include/stdsoap2.c
            if [ ! -f $GSOAP_SRC_FILE ]
            then
                echo "Warning - Not found, cmsoap will not be built"
                echo "   file $GSOAP_SRC_FILE not found"
                unset_var INCLUDE_SERVICES_CMSOAP
                return 0
            fi
        fi
    fi
    LD_LIBRARY_PATH="$GSOAPHOME/lib:$LD_LIBRARY_PATH"

    # In newer versions of gSOAP the -v option verboses output and does not exit.
    # In older versions the -v option is used to print version info.
    # Try to get the version info via -V and -help first before trying the potentialy
    # blocking -v call.
    GSOAP_VERSION=`${SOAPCPP2} -V 2>/dev/null`
    if [ -z $GSOAP_VERSION ] ; then
        GSOAP_VERSION=`${SOAPCPP2} -help 2>&1 | sed -e '/^$/d' | head -n1 | grep -o '\([0-9]\.\)\+[0-9 a-z]*'`
        if [ -z $GSOAP_VERSION ] ; then
            GSOAP_VERSION=`${SOAPCPP2} -v 2>&1 | sed -e '/^$/d' | head -n1 | grep -o '\([0-9]\.\)\+[0-9 a-z]*'`
        fi
    fi

    #if [ "$GSOAP_VERSION" != "2.7.8c" ]
    #then
    #    echo "Error - Only gsoap version 2.7.8c is supported"
    #    return 1
    #fi

    set_var PATH "$PATH"
    set_var LD_LIBRARY_PATH "$LD_LIBRARY_PATH"
    export LD_LIBRARY_PATH
    set_var GSOAPHOME "$GSOAPHOME"

    ESCAPED_GSOAPHOME=`echo "$GSOAPHOME" | sed 's/ /\\\\ /g'`
    export ESCAPED_GSOAPHOME
    set_var ESCAPED_GSOAPHOME "$ESCAPED_GSOAPHOME"

    echo "OK - using GSOAP version $GSOAP_VERSION"
    echo "  setting GSOAPHOME to $GSOAPHOME"
    return 0
}

# required Bison
bison_check ()
{
    echo -n "BISON: "
    pathcheck bison
    if [ $? != 0 ]
    then
        echo "ERROR - Not found"
        return 1
    fi
    BISON_VERSION=`bison -V 2>/dev/null | head -1 | sed 's/[^0-9.]*\([0-9.]*\).*/\1/'`
    BISON_MAJOR=`echo $BISON_VERSION | cut -f 1 -d '.'`
    BISON_MINOR=`echo $BISON_VERSION | cut -f 2 -d '.'`
    if [ $BISON_MAJOR -lt 3 ] && ! [ $BISON_MAJOR -eq 2 -a $BISON_MINOR -ge 7 ]
    then
        echo "ERROR - Only bison 2.7 and above is supported"
        return 1
    fi

    echo "OK - using $BISON_VERSION"
    return 0
}

# required Flex
flex_check ()
{
    echo -n "FLEX: "
    pathcheck flex
    if [ $? != 0 ]
    then
        echo "ERROR - Not found"
        return 1
    fi

    FLEX_VERSION=`flex -V | cut -f 2 -d ' '`
    # some flex versions say "flex version x" rather than "flex x"
    if [ "$FLEX_VERSION" = "version" ]
    then
        FLEX_VERSION=`flex -V | cut -f 3 -d ' '`
    fi
    FLEX_MAJOR=`echo  $FLEX_VERSION | cut -f 1 -d '.'`
    FLEX_MIDDLE=`echo $FLEX_VERSION | cut -f 2 -d '.'`
    FLEX_MINOR=`echo  $FLEX_VERSION | cut -f 3 -d '.'`
    if [ $FLEX_MAJOR -lt 2 ]
    then
        echo "ERROR - Only flex 2.5.6 and above is supported"
        return 1
    fi
    if [ $FLEX_MAJOR -eq 2 ]
    then
        if [ $FLEX_MIDDLE -lt 5 ]
        then
            echo "ERROR - Only flex 2.5.6 and above is supported"
            return 1
        fi
        if [ $FLEX_MIDDLE -eq 5 ]
        then
            if [ $FLEX_MINOR -lt 6 ]
            then
                echo "ERROR - Only flex 2.5.6 and above is supported"
                return 1
            fi
        fi
    fi

    echo "OK - using $FLEX_VERSION"
    return 0
}

# require ar and ld
binutils_native_check ()
{
    echo -n "BINUTILS: "

    pathcheck ar
    if [ $? != 0 ]
    then
        echo "ERROR - ar Not found"
        return 1
    fi

    pathcheck ld
    if [ $? != 0 ]
    then
        echo "ERROR - ld Not found"
        return 1
    fi

    echo "OK"
    return 0
}

javac_check_darwin ()
{
    pathcheck $JAVAC_COMMAND
    if [ $? != 0 ]
    then
        no_javac
        return $?
    fi
    JAVA_HOME=`/usr/libexec/java_home`
    set_var JAVA_HOME "$JAVA_HOME"

    JAVAC_EXE=javac
    "$JAVAC_EXE" -version 2> javac.ver
    JAVAC_REAL_VERSION=`cat javac.ver | grep "javac" | head -1 | awk '{ print $2 }'`
    JAVAC_VERSION=`echo $JAVAC_REAL_VERSION | awk -F'.' '{ print $1"."$2 }' | sed -e 's;\.;0;g'`
    rm -f javac.ver
    if [ -z "$JAVAC_VERSION" -o "$JAVAC_VERSION" -lt "$REQUIRED_VERSION" ] ; then
        echo "ERROR - Only Oracle JAVA 1.6 and above is supported ($JAVAC_VERSION < $REQUIRED_VERSION)"
        return 1
    elif [ "$JAVAC_VERSION" -gt "$REQUIRED_VERSION" ] ; then
        echo "Warning - using compatibility mode for Java 1.6"
    fi
    echo "OK - using JAVAC version $JAVAC_REAL_VERSION"
    echo "  JAVA_HOME is $JAVA_HOME"
}

# require javac and check JAVA_HOME
javac_check ()
{
    echo -n "JAVAC: "
    REQUIRED_VERSION=1.6
    REQUIRED_VERSION=`echo $REQUIRED_VERSION | sed -e 's;\.;0;g'`

    if [ -z "$JAVAC_COMMAND" ] ; then
        JAVAC_COMMAND=javac
    fi

    # Darwin hack
    if [ -z "$JAVA_HOME" ] ; then
        case $host in
            Darwin*)
            javac_check_darwin
            return
            ;;
        esac
    fi

    # Check JAVA_HOME directory first
    if [ -n "$JAVA_HOME" ]
    then
        convert_windows_path "$JAVA_HOME"
        if [ $? != 0 ]
        then
            return 1
        fi
        JAVAC_EXE="$UNIX_CONV_PATH/bin/$JAVAC_COMMAND"
        if [ ! -f "$JAVAC_EXE" ]
        then
            no_javac
            return $?
        fi
        PATH="$UNIX_CONV_PATH/bin:$PATH"
        set_var PATH "$PATH"
    else
        # If not check the path for java
        pathcheck $JAVAC_COMMAND
        if [ $? != 0 ]
        then
            no_javac
            return $?
        fi
        T_JAVAC_EXE="$PATHCHECK_FOUND"
        if [ "$?" != "0" ]
        then
            no_javac
            return $?
        fi
        pathcheck readlink
        if [ $? != 0 ]
        then
            no_javac
            return $?
        fi
        JAVAC_EXE=`readlink -f "$T_JAVAC_EXE"`
    fi

    "$JAVAC_EXE" -version 2> javac.ver
    JAVAC_REAL_VERSION=`cat javac.ver | grep "javac" | head -1 | awk '{ print $2 }'`
    JAVAC_VERSION=`echo $JAVAC_REAL_VERSION | awk -F'.' '{ print $1"."$2 }' | sed -e 's;\.;0;g'`
    rm -f javac.ver
    if [ -n "$JAVAC_VERSION" ]
    then
        if [ "$JAVAC_VERSION" -ge "$REQUIRED_VERSION" ]
        then
            JAVA_HOME=`echo "$JAVAC_EXE" | sed 's@/bin/javac$@@' `
            set_var JAVA_HOME "$JAVA_HOME"
        else
            echo "ERROR - Only Oracle JAVA 1.6 and above is supported"
            return 1
        fi
    else
        echo "ERROR - Only Oracle JAVA 1.6 and above is supported"
        return 1
    fi

    LD_LIBRARY_PATH="$JAVA_HOME/lib:$LD_LIBRARY_PATH"

    T_LD_PATH=yes
    TYPE=`uname -m`
    case `uname` in
        Linux)
            if [ "$TYPE" = "x86_64" ]
            then
                JAVA_PATH_EXT=amd64
                LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$JAVA_HOME/jre/lib/$JAVA_PATH_EXT/server"
            else
                JAVA_PATH_EXT=i386
            fi
            ;;
	Darwin)
	    LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$JAVA_HOME/jre/lib/server"
	    ;;
        SunOS)
            JAVA_PATH_EXT=sparc
            ;;
        AIX)
            JAVA_PATH_EXT=ppc
            ;;
        CYGWIN_NT*)
            JAVA_PATH_EXT=i386
            case ${SPLICE_HOST} in
                x86_64.*)
                    JAVA_PATH_EXT=amd64
                    ;;
            esac
            PATH="$UNIX_CONV_PATH/lib:$PATH"
            PATH="$PATH:$UNIX_CONV_PATH/jre/lib/$JAVA_PATH_EXT"
            PATH="$PATH:$UNIX_CONV_PATH/jre/lib/$JAVA_PATH_EXT/client"
            PATH="$PATH:$UNIX_CONV_PATH/jre/lib/$JAVA_PATH_EXT/native_threads"
            set_var PATH "$PATH"
            T_LD_PATH=no
            ;;
        *)
            echo "ERROR - Not yet supported Java setup"
            return 1
            ;;
    esac

    if [ "$T_LD_PATH" != "no" ]
    then
        LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$JAVA_HOME/jre/lib/$JAVA_PATH_EXT"
        LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$JAVA_HOME/jre/lib/$JAVA_PATH_EXT/client"
        LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$JAVA_HOME/jre/lib/$JAVA_PATH_EXT/native_threads"
        set_var LD_LIBRARY_PATH "$LD_LIBRARY_PATH"
        export LD_LIBRARY_PATH
    fi

    echo "OK - using JAVAC version $JAVAC_REAL_VERSION"
    echo "  JAVA_HOME is $JAVA_HOME"
    return 0
}

no_javac()
{
   if [ "$STRICT_CHECKS" = "yes" ]
   then
       echo "ERROR - Not found"
       return 1
   else
       echo "Warning - Java compiler environment not set, building of all Java related features is disabled."
       unset_var INCLUDE_JAVA
       return 0;
   fi
}

key_value_store_check()
{
    echo -n "Key-value store implementations - SQLITE: "
    if [ -n "$SQLITE_HOME" -a -f "$SQLITE_HOME/sqlite3.c" ]
    then
        echo -n "OK, "
        set_var INCLUDE_SQLITE "yes"
    elif [ "$STRICT_CHECKS" = "yes" ]
    then
        echo "ERROR - sqlite source not found."
        return 1
    else
        echo -n "Warning - Not found, "
        unset_var INCLUDE_SQLITE
    fi
    echo -n "LEVELDB: "
    case `uname` in
        CYGWIN_NT-*)
            echo "Disabled pending Windows port"
            ;;
        SunOS)
            echo "Disabled for Solaris : OSPL-4283"
            ;;
        *)
            if [ -n "$LEVELDB_HOME" -a -f "$LEVELDB_HOME/Makefile" ]
            then
                echo "OK"
                set_var INCLUDE_LEVELDB "yes"
            elif [ "$STRICT_CHECKS" = "yes" ]
            then
                echo "ERROR - library not found"
                return 1
            else
                echo "Warning - Not found"
                unset_var INCLUDE_LEVELDB
            fi
            ;;
    esac
    return 0
}

gmcs_check()
{
   if [ "$STRICT_CHECKS" = "yes" ]
   then
       if [ -n "$MONO_HOME" ]
       then
           gmcs_check_fn "ERROR"
           RES=$?
       else
           echo "GMCS: MONO_HOME not set, disabling SACS api build."
           . "$OSPL_HOME/setup/environment/exclude_sacs"
           RES=0
       fi
   else
       gmcs_check_fn "Warning"
       if [ $? != 0 ]
       then
           echo "   gmcs C# compiler not found, disabling SACS api build."
           . "$OSPL_HOME/setup/environment/exclude_sacs"
       fi
       RES=0
   fi
   return $RES
}

# require gmcs
gmcs_check_fn ()
{
    GMCS_REQUIRED_VERSION=2.4.0.0

    echo -n "GMCS: "
    pathcheck gmcs
    if [ $? != 0 ]
    then
        echo "$1 - No gmcs compiler found"
        return 1
    fi

    GMCS_VERSION_OUTPUT=`gmcs --version 2>&1 | grep "Mono"`
    if [ $? != 0 ]
    then
        echo "$1 - Cannot setup GMCS"
        gmcs --version
        return 1
    else
        GMCS_REAL_VERSION="`echo $GMCS_VERSION_OUTPUT | sed -n '/version/ s/.*version \([0-9]\+\.[0-9]\+\.[0-9]\+\.[0-9]\+\).*/\1/p'`"
        GMCS_REQUIRED_VER=`echo $GMCS_REQUIRED_VERSION | sed -e 's;\.;0;g'`
        GMCS_VERSION=`echo $GMCS_REAL_VERSION | sed -e 's;\.;0;g'`

        if [ -n "$GMCS_VERSION" ]
        then
            if ! [ "$GMCS_VERSION" -ge "$GMCS_REQUIRED_VER" ]
            then
                echo "ERROR - Only GMCS $GMCS_REQUIRED_VERSION and above is supported"
                return 1
            fi
        else
            echo "ERROR - Only GMCS $GMCS_REQUIRED_VERSION and above is supported"
            return 1
        fi

    fi

    echo "OK - using GMCS version $GMCS_REAL_VERSION"
    echo "  found in $PATHCHECK_FOUND"
    return 0
}

tao_check()
{
   if [ "$STRICT_CHECKS" = "yes" ]
   then
       tao_check_fn "ERROR"
       RES=$?
   else
       tao_check_fn "Warning"
       if [ $? != 0 ]
       then
           echo "   TAO environment not set, disabling TAO related features."
           . "$OSPL_HOME/setup/environment/exclude_tao"
       fi
       RES=0
   fi
   return $RES
}

# require TAO and check TAO_ROOT
tao_check_fn ()
{
    echo -n "TAO: "
    if [ -z "$TAO_ROOT" ]
    then
        pathcheck tao_idl
        if [ "$?" != "0" ]
        then
            echo "$1 - No TAO found"
            return 1
        fi
        TAOIDL_EXE="$PATHCHECK_FOUND"
        TAO_ROOT1=`dirname "$TAOIDL_EXE"`
        TAO_ROOT=`dirname "$TAO_ROOT1"`
        set_var TAO_ROOT "$TAO_ROOT"
    else
        if [ ! -d "$TAO_ROOT" ]
        then
            echo "$1 - $TAO_ROOT is not set to a directory"
            return 1
        fi
        set_var TAO_ROOT "$TAO_ROOT"
    fi

    if [ -z "$ACE_ROOT" ]
    then
       ACE_ROOT=$TAO_ROOT
       set_var ACE_ROOT "$ACE_ROOT"
    fi

    LD_LIBRARY_PATH="$ACE_ROOT/lib:$TAO_ROOT/lib:$LD_LIBRARY_PATH"
    set_var LD_LIBRARY_PATH "$LD_LIBRARY_PATH"
    export LD_LIBRARY_PATH

    PATH="$TAO_ROOT/bin:$ACE_ROOT/lib:$ACE_ROOT/bin:$PATH"
    set_var PATH "$PATH"

    TAO_VERSION=`tao_idl -V 2>&1 | grep "PrismTech Version"`
    if [ $? != 0 ]
    then
        #ACE TAO
        TAO_VERSION=`tao_idl -V 2>&1 | grep "TAO_IDL_BE, version "`
        if [ $? = 0 ]
        then
            T_VAR="`echo $TAO_VERSION | awk '{ print $2\" \"$3 }'`"
            SPLICE_ORB=DDS_ACE_TAO_5_6_6
        else
            echo "$1 - Cannot setup TAO"
            tao_idl -V
            return 1
        fi
    else
        T_VAR="`echo $TAO_VERSION | grep \"PrismTech Version No: TAO 1.4.1\"`"
        if [ $? = 0 ]
        then
            SPLICE_ORB=DDS_OpenFusion_1_4_1
        else
            T_VAR="`echo $TAO_VERSION | grep \"PrismTech Version No: TAO 1.5.1\"`"
            if [ $? = 0 ]
            then
                SPLICE_ORB=DDS_OpenFusion_1_5_1
            else
                T_VAR="`echo $TAO_VERSION | grep \"PrismTech Version No: TAO 1.6.1\"`"
                if [ $? = 0 ]
                then
                    SPLICE_ORB=DDS_OpenFusion_1_6_1
                else
                    T_VAR="`echo $TAO_VERSION | grep \"PrismTech Version No: TAO 2.\"`"
                    if [ $? = 0 ]
                    then
                        SPLICE_ORB=DDS_OpenFusion_2
                    else
                        echo "$1 - Unsupported version of PrismTech TAO"
                        tao_idl -V
                        return 1
                    fi
                fi
            fi
        fi
    fi

    set_var SPLICE_ORB $SPLICE_ORB
    echo "OK - SPLICE_ORB is TAO $TAO_VERSION"
    echo "  setting TAO_ROOT to $TAO_ROOT"
    TAO_ROOT_NORMALIZED=`"$OSPL_HOME/bin/ospl_normalizePath" "$TAO_ROOT"`
    set_var TAO_ROOT_NORMALIZED $TAO_ROOT_NORMALIZED
    return 0
}

# require gawk (no version checks)
gawk_check ()
{
    echo -n "GAWK: "
    pathcheck gawk
    if [ $? != 0 ]
    then
        echo "ERROR - Not found"
        return 1
    fi
    GAWK_VERSION=`gawk --version | head -1`
    echo "OK - using $GAWK_VERSION"
    return 0
}

jacorb_check()
{
   if [ "$STRICT_CHECKS" = "yes" ]
   then
       jacorb_check_fn "ERROR"
       RES=$?
   else
       jacorb_check_fn "Warning"
       if [ $? != 0 ]
       then
           echo "   JACORB environment not set, disabling JACORB related features."
           . "$OSPL_HOME/setup/environment/exclude_jacorb"
       fi
       RES=0
   fi
   return $RES
}

# jacorb_check - should have JACORB_HOME
jacorb_check_fn ()
{
    echo -n "JACORB: "

    if [ -z "$JACORB_HOME" ]
    then
        echo "$1 - JACORB_HOME not set"
        return 1
    fi

    if test ! -d "$JACORB_HOME"
    then
        echo "$1 - JACORB_HOME is not a directory"
        return 1
    fi

    if [ ! -f "$JACORB_HOME/lib/endorsed/jacorb.jar" ]
    then
        echo "$1 - $JACORB_HOME/lib/endorsed/jacorb.jar is not a file"
        return 1
    fi

    if [ ! -f "$JACORB_HOME/lib/endorsed/logkit.jar" ]
    then
        echo "$1 - $JACORB_HOME/lib/endorsed/logkit.jar is not a file"
        return 1
    fi

    if [ ! -f "$JACORB_HOME/lib/endorsed/avalon-framework.jar" ]
    then
        echo "$1 - $JACORB_HOME/lib/endorsed/avalon-framework.jar is not a file"
        return 1
    fi

    if [ ! -f "$JACORB_HOME/lib/idl.jar" ]
    then
        echo "$1 - $JACORB_HOME/lib/idl.jar is not a file"
        return 1
    fi

    echo "OK"
    echo "  JACORB_HOME is $JACORB_HOME"
    JACORB_HOME_NORMALIZED=`"$OSPL_HOME/bin/ospl_normalizePath" "$JACORB_HOME"`
    set_var JACORB_HOME_NORMALIZED "$JACORB_HOME_NORMALIZED"
    return 0
}

doxygen_check ()
{
    echo -n "DOXYGEN: "
    command -v doxygen >/dev/null 2>&1
    DOXYGEN_INSTALLED=$?
    if [ $DOXYGEN_INSTALLED -ne 0 ]
    then
        command -v git >/dev/null 2>&1
        GIT_INSTALLED=$?
        if [ $GIT_INSTALLED -ne 0 ]
        then
            if [ "$IS_SCOREBOARD_BUILD" = "yes" ]
            then
                echo "ERROR: No doxygen or git installed, no way to get generated docs."
                return 1
            else
                echo "Warning - No doxygen or git installed"
                echo "  Generated documentation will not be available"
                set_var OSPL_DOCS none
                return 0
            fi
        fi
        git ls-remote git://repository2.prismtech.com/ospl_docs &> /dev/null
        REPO_CHECK=$?
        if [ $REPO_CHECK -ne 0 ]
        then
            if [ "$IS_SCOREBOARD_BUILD" = "yes" ]
            then
                echo "ERROR - ospl_docs repo cannot be reached and doxygen not installed"
                return 1
            else
                echo "Warning - ospl_docs repo cannot be reached and doxygen not installed"
                echo "  Generated documentation will not be available"
                set_var OSPL_DOCS none
                return 0
            fi
        fi
        echo "OK - no doxygen installed, will attempt to retreive docs from git repo"
        return 0
    fi

    echo OK
    return 0
}

c99_check()
{
    echo -n "C99: "
    RESULT=""

    # Only check if we possibly need to switch the C99 API off
    if [ "$INCLUDE_API_DCPS_C99" = "yes" ]
    then
        # Is a VS compiler detected?
        if [ -n "$VS_VER" ]
        then
            # At least VS2013 is needed to support C99 on Windows.
            if [ "$VS_VER" -lt 18 ]
            then
                set_var INCLUDE_API_DCPS_C99 no
                set_var INCLUDE_DBT_API_DCPS_C99 no
                RESULT="WARNING - Visual Studio $VS_YEAR ($VS_VER) does not support C99."
            fi
        fi
    fi

    if [ -z "$RESULT" ]
    then
        if [ "$INCLUDE_API_DCPS_C99" = "yes" ]
        then
            RESULT="OK - supported"
        else
            RESULT="C99 API not included"
        fi
    fi

    echo "$RESULT"
    if [ "$INCLUDE_API_DCPS_C99" != "yes" ]
    then
        echo "  C99 API will not be available"
    fi
    return 0
}

no_protoc()
{
   if [ "$STRICT_CHECKS" = "yes" ]
   then
       # this needs to be to an error before protobuf functionality is released
       # echo "ERROR - Not found"
       # return 1
       echo "Warning - Protobuf compiler environment not set, building of all protobuf related features is disabled."
       return 0;
   else
       echo "Warning - Protobuf compiler environment not set, building of all protobuf related features is disabled."
       return 0;
   fi

}

launch4j_check()
{
    # Check LAUNCH4J_HOME directory first
    if [ -n "$LAUNCH4J_HOME" ]
    then
        pathcheck launch4jc

       # If launch4j is not part of the path, add launch4j home dir to path
        if [ $? != 0 ]
        then
            PATH="$LAUNCH4J_HOME:$PATH"
            set_var PATH "$PATH"
            SPATH=`echo $PATH | tr ' ' '\177' | tr ':' ' '`
        else
            echo "launch4jc already in path"
        fi
    else
        echo "LAUNCH4J_HOME not set"
    fi

    # check the path for launch4jc now
    pathcheck launch4jc

    if [ $? != 0 ]
    then
        echo "launch4jc executable not in path"
        return 1
    fi
    T_L4J_EXE="$PATHCHECK_FOUND"

    if [ "$?" != "0" ]
    then
        echo "launch4jc executable not in path (2)"
        return 1
    fi
    pathcheck readlink
    if [ $? != 0 ]
    then
        echo "launch4jc executable not executable"
        return 1
    fi

    return 0
}

protoc_check()
{
    echo -n "GOOGLE PROTOCOL BUFFERS: "
    REQUIRED_VERSION=20600
    PROTOBUF_COMMAND=protoc

    # Check PROTOBUF_HOME directory first
    if [ -n "$PROTOBUF_HOME" ]
    then
        convert_windows_path "$PROTOBUF_HOME"
        if [ $? != 0 ]
        then
            return 1
        fi
    else
        echo "PROTOBUF_HOME has not been set"
        no_protoc
        return $?
    fi

    # PROTOBUF_HOME is set, check if it points to a location that includes sources
    if [ ! -f "$PROTOBUF_HOME/src/google/protobuf/compiler/plugin.proto" ]
    then
        echo "PROTOBUF_HOME points to $PROTOBUF_HOME that does not include required sources"
        no_protoc
        return $?
    fi

    # check the path for protoc now
    pathcheck $PROTOBUF_COMMAND
    if [ $? != 0 ]
    then
        no_protoc
        return $?
    fi
    T_PROTOC_EXE="$PATHCHECK_FOUND"
    if [ "$?" != "0" ]
    then
        no_protoc
        return $?
    fi
    pathcheck readlink
    if [ $? != 0 ]
    then
        no_protoc
        return $?
    fi
    PROTOC_EXE=$T_PROTOC_EXE

    "$PROTOC_EXE" --version 2 > protoc.ver
    PROTOC_REAL_VERSION=`cat protoc.ver | grep "libprotoc" | head -1 | awk '{ print $2 }'`
    PROTOC_REAL_VERSION="${PROTOC_REAL_VERSION//[$'\t\r\n']}"
    PROTOC_VERSION=`echo $PROTOC_REAL_VERSION | awk -F'.' '{ print $1"."$2"."$3}' | sed -e 's;\.;0;g'`
    rm -f protoc.ver

    if [ -n "$PROTOC_VERSION" ]
    then
        if [ "$PROTOC_VERSION" -ge "$REQUIRED_VERSION" ]
        then
            PROTOBUF_HOME=`echo "$PROTOBUF_HOME"`
        else
            echo "ERROR - Only Google Protocol Buffers 2.6.0 and above is supported"
            return 1
        fi
    else
        echo "ERROR - Only Google Protocol Buffers 2.6.0 and above is supported"
        return 1
    fi
    case `uname` in
        CYGWIN_NT-*)
            launch4j_check

            if [ $? != 0 ]
            then
                no_protoc
                return $?
            fi
            ;;
        *)
            ;;
    esac

    echo "OK - using PROTOC version $PROTOC_REAL_VERSION"
    echo "  PROTOBUF_HOME is $PROTOBUF_HOME"
    set_var INCLUDE_TOOLS_PROTOBUF "yes"
    set_var INCLUDE_DBT_TOOLS_PROTOBUF "yes"
    return 0

}

python3_check()
{
    echo -n "Python3: "

    # the component depends on C99 api, so check that first
    if [ "$INCLUDE_API_DCPS_C99" != "yes" ]
    then
        echo "Warning: C99 API not included in setup. Cannot build Python DCPS API."
        set_var INCLUDE_API_DCPS_PYTHON no
        return 0
    fi

    result=1
    # Check explicitly defined location for python installs
    if [ -n "$PYTHON3_HOME" ]
    then
        for pyexec in $(echo "$PYTHON3_HOME" | tr ":" "\n")
        do
            python3_check_fn "$pyexec" && \
                    result=0; python3_tmps="$python3_tmps:$pyexec"; set_undefined_var PYTHON3_EXEC $pyexec
        done
        set_var PYTHON3_EXECS $python3_tmps
        python3_tmps=
    # Else check if python3 is installed normally in host env
    else
        pyexec=$(which python3 2> /dev/null)
        python3_check_fn "$pyexec" && \
                result=0; set_var PYTHON3_EXECS $pyexec; set_var PYTHON3_EXEC=$pyexec
    fi

    if [ $result -eq 0 ]
    then
        set_var INCLUDE_API_DCPS_PYTHON yes
        set_var INCLUDE_DBT_API_DCPS_PYTHON yes
    else
        set_var INCLUDE_API_DCPS_PYTHON no
    fi

}

python3_check_fn()
{
    pyexec="$1"
    if [ -x "$pyexec" ]
    then
        (echo "import platform";
        echo "import sys";
        echo "if platform.system().find('Windows') == 0:";
        echo "  ver=[3,5,0]";
        echo "elif platform.system().find('CYGWIN') == 0:";
        echo "  print('Warning: Cygwin python3 detected, this is invalid for DCPS Python api')";
        echo "  sys.exit(1)";
        echo "else:";
        echo "  ver=[3,4,0]";
        echo "if [int(num) for num in platform.python_version().split('.')] <= ver:";
        echo "  print('Warning: Your version of python is below {0}.{1}.{2}'.format(ver[0],ver[1],ver[2]))";
        echo "  sys.exit(1)") | $pyexec;
        if [ $? -eq 0 ]
        then
            #Already a good version of python available
            pyver=`$pyexec -c "import platform; print(platform.python_version())"`
            echo "OK - Using `which $pyexec` v$pyver"
            modules_ok=0
            declare -a modules=("Cython" "wheel")
            for m in "${modules[@]}"
            do
                mod_version=$(python3_module_check $pyexec $m)
                if [ $? -eq 0 ]
                then
                    echo "    $m OK - using $m v$mod_version"
                else
                    echo "    Warning: $m module not found. Cannot build Python DCPS API"
                    set_var INCLUDE_API_DCPS_PYTHON no
                    modules_ok=1
                fi
            done
            return $modules_ok
        fi
    else
        echo "Warning: Python3 not available. Cannot build Python DCPS API."
        set_var INCLUDE_API_DCPS_PYTHON no
    fi
    return 1
}

maven_check_inner()
{
    echo -n "MAVEN: "

    # first check whether there is any maven in the path
    MAVEN_FOUND=`which mvn 2> /dev/null`

    if [ -z "$MAVEN_FOUND" ]
    then
        # No, it is not yet in the path

        if [ -n "$M2_HOME" ]
        then
            if [ ! -x "$M2_HOME/bin/mvn" ]
            then
                echo "Error - $M2_HOME is invalid"
                return 1
            fi
            MAVEN_FOUND="$M2_HOME/bin/mvn"
            PATH="$M2_HOME/bin:$PATH"
            set_var PATH "$PATH"
        fi
    fi

    if [ -z "$MAVEN_FOUND" ]
    then
        # Maven was not found
        echo "maven is not installed (properly):"
        echo "   mvn not found in path"
        echo "   M2_HOME variable not set"
        no_javac
    else
        # maven was found, now check the version number
        MAVEN_CHECK=`mvn --version | grep "Apache Maven 3."`
        if [ -z "$MAVEN_CHECK" ]
        then
            # found unsupported version
            MAVEN_CHECK=`mvn --version | grep "Apache Maven"`
            echo "   Found maven version $MAVEN_CHECK"
            echo "   WARNING - Maven version 3 would be better"
            no_javac
         else
            echo "OK - Using $MAVEN_CHECK"
         fi
    fi

    unset MAVEN_FOUND MAVEN_CHECK
    return 0
}

python3_module_check()
{
    interp=$1
    module=$2
    (echo "modversion=''";
    echo "try:";
    echo "  import $module";
    echo "  try:";
    echo "      modversion=str($module.__version__)";
    echo "  except AttributeError:";
    echo "      modversion=str('.'.join(map(str,$module.VERSION)))";
    echo "except ImportError:";
    echo "  print('Warning: Invalid python module')";
    echo "  exit(1)";
    echo "if modversion == '':";
    echo "  exit(-1)";
    echo "print(modversion)") | $interp
    return $?
}

nodejs_check()
{
    chkmsg="OK"

    # first check whether the NODEJS install directory exists
    if [ ! -d "$NODEJS_HOME" ]
    then
        chkmsg="nodejs install dir not found"
    fi

    # the component depends on C99 api, so check that too
    if [ "$INCLUDE_API_DCPS_C99" != "yes" ]
    then
        chkmsg="C99 API not included in setup"
    fi

	# DBT's depend on python, so check for existing native python
	PYTHON_FOUND=`which python 2> /dev/null`
	if [ ! -x "$PYTHON_FOUND" ]
	then
		chkmsg="Python not found"
	fi

    if [ "$chkmsg" != "OK" ]
    then
        chkmsg="$chkmsg - skipping build of NodeJS DCPS API and unit tests."
    else
        chkmsg="$chkmsg - NODEJS DCPS API will be built. NODEJS unit tests enabled."
        set_var INCLUDE_API_DCPS_NODEJS "yes"
        set_var INCLUDE_DBT_API_DCPS_NODEJS "yes"
        convert_windows_path "$NODEJS_HOME"
		case `uname` in
        CYGWIN_NT-*)
        	PATH="$UNIX_CONV_PATH:$PATH"
        	set_var PATH "$PATH"
	        ;;
	    esac
        case `uname` in
        Linux)
        	PATH="$UNIX_CONV_PATH/bin:$PATH"
        	set_var PATH "$PATH"
	        ;;
	    esac
    fi
    echo "NODEJS: $chkmsg"
    return 0
}

cleanup_checks ()
{
    unset msvs_check
    unset gcc_check
    unset make_check
    unset bison_check
    unset flex_check
    unset javac_check
    unset no_javac
    unset tao_check
    unset tao_check_fn
    unset gawk_check
    unset csharp_check
    unset jacorb_check
    unset valgrind_check
    unset gsoap_check
    unset doxygen_check
    unset protoc_check
    unset no_protoc
    unset c99_check
    unset python3_check
    unset python3_module_check
    unset launch4j_check
    unset maven_check_inner
    unset PATHCHECK_FOUND
    unset GCC_FULLVERSION BISON_VERSION FLEX_VERSION JAVAC_REAL_VERSION
    unset GMCS_VERSION GMCS_REAL_VERSION GMCS_VERSION_OUTPUT GMCS_REQUIRED_VERSION GMCS_REQUIRED_VER
    unset MAKE_MAJOR MAKE_MINOR BISON_MAJOR FLEX_MAJOR FLEX_MINOR
    unset REQUIRED_VERSION JAVAC_EXE JAVAC_VERSION MAIN_DRIVE
    unset T_JAVAC_EXE TAOIDL_EXE T_VAR SPATH TAO_ROOT1
    unset GAWK_VERSION JAVA_PATH_EXT CL_MAJOR COMPILER_VER CL_EXE
    unset VS_HOME_UNIX VS_HOME_DOS DOS_CONV_PATH UNIX_CONV_PATH T_LD_PATH
    unset CSC_EXE VERSIONS NET_VERSION TYPE PATH_ADDON SDK_PATH_ADDON
    unset PROTOC_REAL_VERSION PROTOC_VERSION PROTOC_EXE T_PROTOC_EXE T_L4J_EXE
}
