#!/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>
#              Aiken Harris <harraiken91@gmail.com>


# Prints XTChain banner
banner()
{
    local XTC_BANNER="XT Toolchain v${XTCVER} for Linux"

    printf "###############################################################################\n\n"
    printf "\033[33m%*s\033[00m\n\n" $(( (${#XTC_BANNER} + 80) / 2)) "${XTC_BANNER}"
    printf "###############################################################################\n\n"
}
export -f banner

# 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()
{
    banner
    echo "XTChain defines an internal list of commands:"
    echo " * banner          - prints XTChain banner"
    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()
{
    local XTCHAIN_EXTTOOLS=false

    if [ ! -f "${XTCDIR}/bin/clang" ] || [ "$(which clang)" != "${XTCDIR}/bin/clang" ] || [ $(echo ${XTCVER} | grep "min") ]; then
        XTCHAIN_EXTTOOLS=true
        for TOOL in {clang,clang++,cmake,lld-link,ninja}; do
            which ${TOOL} &> /dev/null
            if [ $? -ne 0 ]; then
                echo "ERROR: You are using minimal version of XTChain and '${TOOL}' has been not found in your system!"
                echo "ERROR: Please install all required tools."
                exit 1
            fi
        done
    fi

    banner
    echo -en "\nLLVM/Clang Compiler: $(clang --version | grep 'clang version' | cut -d' ' -f3) ($(which clang))"
    echo -en "\nLLVM/LLD Linker: $(lld-link --version | cut -d' ' -f2) ($(which lld-link))"
    echo -en "\nWine IDL Compiler: $(widl -V | grep 'version' | cut -d' ' -f5) ($(which widl))"
    echo -en "\nWine Message Compiler: $(wmc -V | grep 'version' | cut -d' ' -f5) ($(which wmc))"
    echo -en "\nWine Resource Compiler: $(wrc --version | grep 'version' | cut -d' ' -f5) ($(which wrc))"
    echo -en "\nXT SPEC Compiler: $(xtcspecc --help | grep Version | cut -d' ' -f5) ($(which xtcspecc))"
    echo -en "\nCMake Build System: $(cmake --version | grep 'cmake version' | cut -d' ' -f3) ($(which cmake))"
    echo -en "\nNinja Build System: $(ninja --version) ($(which ninja))"
    echo -en "\n\n"

    charch ${TARGET:-amd64}
    chbuild ${BUILD_TYPE:-DEBUG}

    echo -en "\n\nFor a list of all supported commands, type 'help'"
    echo -en "\n-------------------------------------------------\n\n\n"
}
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
