#!/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" =~ "multi4in1:avr:multixmega32d4" ]]; then
        ALL_RFMODULES=$(echo CYRF6936_INSTALLED);
    elif [[ "$BOARD" =~ "multi4in1:avr:multiatmega328p:" ]]; then
        ALL_RFMODULES=$(echo A7105_INSTALLED CYRF6936_INSTALLED CC2500_INSTALLED NRF24L01_INSTALLED);
    elif [[ "$BOARD" =~ "multi4in1: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" =~ "multi4in1:avr:multixmega32d4" ]]; then
        ALL_PROTOCOLS=$(echo $CYRF6936_PROTOCOLS);
    elif [[ "$BOARD" =~ "multi4in1:avr:multiatmega328p:" ]]; then
        ALL_PROTOCOLS=$(echo $A7105_PROTOCOLS $CC2500_PROTOCOLS $CYRF6936_PROTOCOLS $NRF24L01_PROTOCOLS $CCNRF_PROTOCOLS);
    elif [[ "$BOARD" =~ "multi4in1: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 [[ "$BOARD" == "multi4in1:avr:multixmega32d4" ]]; then
        build_release_orx;
    elif [[ "$BOARD" == "multi4in1:avr:multiatmega328p:bootloader=none" ]]; then
        build_release_avr_noboot;
    elif [[ "$BOARD" == "multi4in1:avr:multiatmega328p:bootloader=optiboot" ]]; then
        build_release_avr_optiboot;
    elif [[ "$BOARD" == "multi4in1:STM32F1:multistm32f103cb:debug_option=none" ]]; then
        build_release_stm32f1_no_debug;
    elif [[ "$BOARD" == "multi4in1:STM32F1:multistm32f103cb:debug_option=native" ]]; then
        build_release_stm32f1_native_debug;
    elif [[ "$BOARD" == "multi4in1:STM32F1:multistm32f103cb:debug_option=ftdi" ]]; then
        build_release_stm32f1_serial_debug;
    elif [[ "$BOARD" == "multi4in1:STM32F1:multi5in1t18int" ]]; then
        build_release_stm32f1_t18int;
    elif [[ "$BOARD" == "multi4in1:STM32F1:multistm32f103c8:debug_option=none" ]]; then
        build_release_stm32f1_64k;
    else
      printf "No release files for this board.";
    fi
}
