#!/usr/bin/env bash

getMultiVersion() {
    MAJOR_VERSION=$(grep "VERSION_MAJOR" "Multiprotocol/Multiprotocol.h" | awk -v N=3 '{gsub(/\r/,""); print $N}')
    MINOR_VERSION=$(grep "VERSION_MINOR" "Multiprotocol/Multiprotocol.h" | awk -v N=3 '{gsub(/\r/,""); print $N}')
    REVISION_VERSION=$(grep "VERSION_REVISION" "Multiprotocol//Multiprotocol.h" | awk -v N=3 '{gsub(/\r/,""); print $N}')
    PATCH_VERSION=$(grep "VERSION_PATCH" "Multiprotocol//Multiprotocol.h" | awk -v N=3 '{gsub(/\r/,""); print $N}')
    MULTI_VERSION=$MAJOR_VERSION.$MINOR_VERSION.$REVISION_VERSION.$PATCH_VERSION
}

getAllRFModules() {
    if [[ "$BOARD" =~ ":avr:multixmega32d4" ]]; then
        ALL_RFMODULES=$(echo CYRF6936_INSTALLED);
    elif [[ "$BOARD" =~ ":avr:multiatmega328p:" ]]; then
        ALL_RFMODULES=$(echo A7105_INSTALLED CYRF6936_INSTALLED CC2500_INSTALLED NRF24L01_INSTALLED);
    elif [[ "$BOARD" =~ ":STM32F1:" ]]; then
        ALL_RFMODULES=$(echo A7105_INSTALLED CYRF6936_INSTALLED CC2500_INSTALLED NRF24L01_INSTALLED SX1276_INSTALLED);
    fi
}

getAllProtocols() {
    A7105_PROTOCOLS=$(sed -n 's/[\/\/]*[[:blank:]]*#define[[:blank:]]*\([[:alnum:]_]*_A7105_INO\)\(.*\)/\1/p' Multiprotocol/_Config.h)
    CC2500_PROTOCOLS=$(sed -n 's/[\/\/]*[[:blank:]]*#define[[:blank:]]*\([[:alnum:]_]*_CC2500_INO\)\(.*\)/\1/p' Multiprotocol/_Config.h)
    CYRF6936_PROTOCOLS=$(sed -n 's/[\/\/]*[[:blank:]]*#define[[:blank:]]*\([[:alnum:]_]*_CYRF6936_INO\)\(.*\)/\1/p' Multiprotocol/_Config.h)
    NRF24L01_PROTOCOLS=$(sed -n 's/[\/\/]*[[:blank:]]*#define[[:blank:]]*\([[:alnum:]_]*_NRF24L01_INO\)\(.*\)/\1/p' Multiprotocol/_Config.h)
    CCNRF_PROTOCOLS=$(sed -n 's/[\/\/]*[[:blank:]]*#define[[:blank:]]*\([[:alnum:]_]*_CCNRF_INO\)\(.*\)/\1/p' Multiprotocol/_Config.h)
    SX1276_PROTOCOLS=$(sed -n 's/[\/\/]*[[:blank:]]*#define[[:blank:]]*\([[:alnum:]_]*_SX1276_INO\)\(.*\)/\1/p' Multiprotocol/_Config.h)

    if [[ "$BOARD" =~ ":avr:multixmega32d4" ]]; then
        ALL_PROTOCOLS=$(echo $CYRF6936_PROTOCOLS);
    elif [[ "$BOARD" =~ ":avr:multiatmega328p:" ]]; then
        ALL_PROTOCOLS=$(echo $A7105_PROTOCOLS $CC2500_PROTOCOLS $CYRF6936_PROTOCOLS $NRF24L01_PROTOCOLS $CCNRF_PROTOCOLS);
    elif [[ "$BOARD" =~ ":STM32F1:" ]]; then
        ALL_PROTOCOLS=$(echo $A7105_PROTOCOLS $CC2500_PROTOCOLS $CYRF6936_PROTOCOLS $NRF24L01_PROTOCOLS $CCNRF_PROTOCOLS $SX1276_PROTOCOLS);
    fi
}

buildMulti() {
    echo ::group::_Config.h
    git diff Multiprotocol/_Config.h
    echo ::endgroup::
    BUILDCMD="arduino-cli compile -b $BOARD ${GITHUB_WORKSPACE}/Multiprotocol/Multiprotocol.ino --build-path ${GITHUB_WORKSPACE}/build/";
    echo $BUILDCMD;
    $BUILDCMD
    return $?
}

buildProtocol() {
    exitcode=0;
    opt_disable $ALL_PROTOCOLS;
    opt_enable $1;
    buildMulti;
    if [ $? -ne 0 ]; then exitcode=1; fi;
    return $exitcode;
}

buildEachProtocol() {
    exitcodesum=0;
    for PROTOCOL in $ALL_PROTOCOLS ; do 
        printf "\e[33;1mBuilding $PROTOCOL\e[0m\n"; 
        buildProtocol $PROTOCOL; 
        if [ $? -ne 0 ]; then exitcodesum=$((exitcodesum + 1)); fi;
    done;
    return $exitcodesum;
}

buildRFModule() {
    exitcode=0;
    opt_disable $ALL_RFMODULES;
    opt_enable $1;
    buildMulti;
    if [ $? -ne 0 ]; then exitcode=1; fi;
    return $exitcode;
}

buildEachRFModule() {
    exitcodesum=0;
    for RFMODULE in $ALL_RFMODULES; do
        printf "\e[33;1mBuilding $RFMODULE\e[0m\n";
        buildRFModule $RFMODULE;
        if [ $? -ne 0 ]; then exitcodesum=$((exitcodesum + 1)); fi;
    done;
    return $exitcodesum;
}

buildReleaseFiles(){
    if [[ "$RELEASE" == "scripts" ]]; then
        build_release_scripts;
    elif [[ "$RELEASE" == "orangerx" ]]; then
        build_release_orx;
    elif [[ "$RELEASE" == "atmega328p" ]]; then
        build_release_avr_noboot;
    elif [[ "$RELEASE" == "atmega328p-optiboot" ]]; then
        build_release_avr_optiboot;
    elif [[ "$RELEASE" == "stm32f103-128k-4in1" ]]; then
        build_release_stm32f1_4in1_no_debug;
    elif [[ "$RELEASE" == "stm32f103-128k-usb-debug" ]]; then
        build_release_stm32f1_4in1_native_debug;
    elif [[ "$RELEASE" == "stm32f103-128k-serial-debug" ]]; then
        build_release_stm32f1_4in1_serial_debug;
    elif [[ "$RELEASE" == "stm32f103-cc2500-64k" ]]; then
        build_release_stm32f1_cc2500_64k;
    elif [[ "$RELEASE" == "stm32f103-cc2500-128k" ]]; then
        build_release_stm32f1_cc2500_128k;
    elif [[ "$RELEASE" == "stm32f103-128k-5in1" ]]; then
        build_release_stm32f1_5in1;
    elif [[ "$RELEASE" == "tlite-5in1" ]]; then
        build_release_stm32f1_tlite;
    elif [[ "$RELEASE" == "t18-5in1" ]]; then
        build_release_stm32f1_t18int;
    else
      printf "No release files for this board.";
    fi
}
