#!/usr/bin/env bash
# PROJECT:     XTchain
# LICENSE:     See the COPYING.md in the top level directory
# FILE:        scripts/xtclib
# DESCRIPTION: XTchain library
# DEVELOPERS:  Rafal Kupiec <belliash@codingworkshop.eu.org>


# Sets the target architecture
charch()
{
    if [ "x${1}" == "x" ]; then
        echo "Syntax: charch [architecture]"
        return
    fi
    case ${1} in
        "aarch64"|"arm64")
            export TARGET="aarch64"
            ;;
        "arm"|"armv7")
            export TARGET="armv7"
            ;;
        "i386"|"i486"|"i586"|"i686"|"x86")
            export TARGET="i686"
            ;;
        "amd64"|"x64"|"x86_64")
            export TARGET="amd64"
            ;;
        *)
            export TARGET="UNKNOWN"
    esac
    echo "Target Architecture: ${TARGET}"
}
export -f charch

# Sets the build type
chbuild()
{
    if [ "x${1}" == "x" ]; then
        echo "Syntax: chbuild [DEBUG|RELEASE]"
        return
    fi
    case ${1} in
        [Rr][Ee][Ll][Ee][Aa][Ss][Ee])
            export BUILD_TYPE="RELEASE"
            ;;
        *)
            export BUILD_TYPE="DEBUG"
    esac
    echo "Target build type: ${BUILD_TYPE}"
}
export -f chbuild

# Prints help
help()
{
	version
	echo "XTChain defines an internal list of commands:"
	echo " * charch [arch]   - sets the target CPU architecture [aarch64/armv7/i686/amd64]"
	echo " * chbuild [type]  - sets build type [debug/release]"
	echo " * help            - prints this message"
	echo " * version         - prints XTChain and its components version"
	echo " * xbuild          - builds an application with a Ninja build system"
}
export -f help

# Displays version banner
version()
{
    echo "###############################################################################"
    echo "#                        XT Toolchain v${XTCVER} for Linux                        #"
    echo "#               by Rafal Kupiec <belliash@codingworkshop.eu.org>              #"
    echo "###############################################################################"
    echo
    echo
    echo "LLVM Compiler Version: $(${XTCDIR}/bin/clang --version | grep 'clang version' | cut -d' ' -f3)"
    echo "LLVM Windres Utility Version: $(${XTCDIR}/bin/i686-w64-mingw32-windres -V | cut -d' ' -f6)"
    echo "Mingw IDL Compiler Version: $(${XTCDIR}/bin/i686-w64-mingw32-widl -V | grep 'version' | cut -d' ' -f5)"
    echo "Wine Message Compiler Version: $(${XTCDIR}/bin/wmc -V | grep 'version' | cut -d' ' -f5)"
    echo "Wine Resource Compiler Version: $(${XTCDIR}/bin/wrc --version | grep 'version' | cut -d' ' -f5)"
    echo
    charch ${TARGET:-amd64}
    chbuild ${BUILD_TYPE:-DEBUG}
    echo
    echo
}
export -f version

# Builds application (wrapper to Ninja)
xbuild()
{
	if [ ! -f build.arch ]; then
		ninja "$@"
	else
		ARCH=$(cat build.arch)
		if [ x"${ARCH}" != x"${TARGET}" ]; then
			echo "Build is configured for '${ARCH}' while current target set to '${TARGET}'!"
			echo "Cannot continue until conflict is resolved ..."
			return 1
		fi
		ninja "$@"
	fi
}
export -f xbuild
