os: linux
dist: bionic
language: c

env:
  jobs:
    - BOARD="multi4in1:avr:multiatmega328p:bootloader=none"
    - BOARD="multi4in1:avr:multiatmega328p:bootloader=optiboot"
    - BOARD="multi4in1:avr:multixmega32d4"
    - BOARD="multi4in1:STM32F1:multi5in1t18int"
    - BOARD="multi4in1:STM32F1:multistm32f103cb:debug_option=none"
    - BOARD="multi4in1:STM32F1:multistm32f103cb:debug_option=native"
    - BOARD="multi4in1:STM32F1:multistm32f103cb:debug_option=ftdi"
    - BOARD="multi4in1:STM32F1:multistm32f103c8:debug_option=none"

notifications:
  email: false

before_install:
  # Fetch the tag information for the current branch
  - git fetch origin --tags

  # Publish the buildroot script folder
  - chmod +x ${TRAVIS_BUILD_DIR}/buildroot/bin/*
  - export PATH=${TRAVIS_BUILD_DIR}/buildroot/bin/:${PATH}

  # Helper functions for the builds
  - buildMulti() { start_fold config_diff; travis_time_start; git diff Multiprotocol/_Config.h; end_fold config_diff; exitcode=0; BUILDCMD="arduino-cli compile -b $BOARD Multiprotocol/Multiprotocol.ino --build-path ${TRAVIS_BUILD_DIR}/build/"; echo $BUILDCMD; $BUILDCMD; if [ $? -ne 0 ]; then exitcode=1; fi; echo; return $exitcode; }
  - 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"; 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"; buildRFModule $RFMODULE; if [ $? -ne 0 ]; then exitcodesum=$((exitcodesum + 1)); fi; done; return $exitcodesum; }
  - buildDefault() { exitcode=0; printf "\n\e[33;1mBuilding default configuration\e[0m\n"; buildMulti; if [ $? -ne 0 ]; then exitcode=1; fi; return $exitcode; }
  - buildSerialOnly() { exitcode=0; printf "\n\e[33;1mBuilding serial mode only\e[0m\n"; opt_disable ENABLE_PPM; opt_enable ENABLE_SERIAL; buildMulti; if [ $? -ne 0 ]; then exitcode=1; fi; return $exitcode; }
  - buildPPMOnly() { exitcode=0; printf "\n\e[33;1mBuilding PPM mode only\e[0m\n"; opt_enable ENABLE_PPM; opt_disable ENABLE_SERIAL; buildMulti; if [ $? -ne 0 ]; then exitcode=1; fi; return $exitcode; }
  - export -f buildMulti

  # Function to build the release files - dependent on board type.
  # Release build scripts are located in buildroot/bin.
  - if [[ "$BOARD" == "multi4in1:avr:multixmega32d4" ]]; then
      buildReleaseFiles(){
        build_release_orx;
      };
    elif [[ "$BOARD" == "multi4in1:avr:multiatmega328p:bootloader=none" ]]; then
      buildReleaseFiles(){
        build_release_avr_noboot;
      };
    elif [[ "$BOARD" == "multi4in1:avr:multiatmega328p:bootloader=optiboot" ]]; then
      buildReleaseFiles(){
        build_release_avr_optiboot;
      };
    elif [[ "$BOARD" == "multi4in1:STM32F1:multistm32f103cb:debug_option=none" ]]; then
      buildReleaseFiles(){
        build_release_stm32f1_no_debug;
      };
    elif [[ "$BOARD" == "multi4in1:STM32F1:multistm32f103cb:debug_option=native" ]]; then
      buildReleaseFiles(){
        build_release_stm32f1_native_debug;
      };
    elif [[ "$BOARD" == "multi4in1:STM32F1:multistm32f103cb:debug_option=ftdi" ]]; then
      buildReleaseFiles(){
        build_release_stm32f1_serial_debug;
      };
    elif [[ "$BOARD" == "multi4in1:STM32F1:multi5in1t18int" ]]; then
      buildReleaseFiles(){
        build_release_stm32f1_t18int;
      };
    else
      buildReleaseFiles() { printf "No release files for this board."; };
    fi

install:
  # Install Arduino CLI
  - mkdir ~/arduino-cli
  - curl -fsSL https://raw.githubusercontent.com/arduino/arduino-cli/master/install.sh | BINDIR=~/arduino-cli sh -s 0.13.0;
  - export PATH=$PATH:$HOME/arduino-cli

  # Update the board url and package index
  - arduino-cli core update-index --additional-urls https://raw.githubusercontent.com/pascallanger/DIY-Multiprotocol-TX-Module-Boards/master/package_multi_4in1_board_index.json

  # Install the STM32 board if needed
  - if [[ "$BOARD" =~ "multi4in1:STM32F1:" ]]; then
      arduino-cli core install multi4in1:STM32F1 --additional-urls https://raw.githubusercontent.com/pascallanger/DIY-Multiprotocol-TX-Module-Boards/master/package_multi_4in1_board_index.json;
    fi

  # Install the AVR board if needed
  - if [[ "$BOARD" =~ "multi4in1:avr:" ]]; then
      arduino-cli core install arduino:avr;
      arduino-cli core install multi4in1:avr --additional-urls https://raw.githubusercontent.com/pascallanger/DIY-Multiprotocol-TX-Module-Boards/master/package_multi_4in1_board_index.json;
    fi

before_script:
  # Export all the variables
  - set -a

  # Change current working directory to the build dir
  - cd ${TRAVIS_BUILD_DIR}

  # Create somwhere to put the exported binaries
  - mkdir ./binaries

  # Log the initial Multi config 
  - cat Multiprotocol/_Config.h

  # Back up the configuration
  - cp Multiprotocol/_Config.h ./_Config.h.bak

  # Get the firmware version number from the source
  - 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

  # Derive the Multi protocols from the Multi source
  - 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)
  - SX1276_PROTOCOLS=$(sed -n 's/[\/\/]*[[:blank:]]*#define[[:blank:]]*\([[:alnum:]_]*_SX1276_INO\)\(.*\)/\1/p' Multiprotocol/_Config.h)

  # Get the full set of protocols for each board
  - 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);
    elif [[ "$BOARD" =~ "multi4in1:STM32F1:" ]]; then
      ALL_PROTOCOLS=$(echo $A7105_PROTOCOLS $CC2500_PROTOCOLS $CYRF6936_PROTOCOLS $NRF24L01_PROTOCOLS $SX1276_PROTOCOLS);
    fi
  - echo $ALL_PROTOCOLS

  # Declare all the installed modules
  - 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
  - echo $ALL_RFMODULES

  # Disable CHECK_FOR_BOOTLOADER when not needed
  - if [[ "$BOARD" == "multi4in1:avr:multiatmega328p:bootloader=none" ]]; then
      opt_disable CHECK_FOR_BOOTLOADER;
    fi

  # Trim the enabled protocols down for the STM32F103CB board with debugging
  - if [[ "$BOARD" == "multi4in1:STM32F1:multistm32f103cb:debug_option=ftdi" ]] || [[ "$BOARD" == "multi4in1:STM32F1:multistm32f103cb:debug_option=native" ]]; then
      opt_disable $ALL_PROTOCOLS;
      opt_enable FRSKYX_CC2500_INO AFHDS2A_A7105_INO MJXQ_NRF24L01_INO DSM_CYRF6936_INO;
    fi

  # Trim the enabled protocols down for the STM32F103C8 board
  - if [[ "$BOARD" == "multi4in1:STM32F1:multistm32f103c8:debug_option=none" ]]; then
      opt_disable $ALL_PROTOCOLS;
      opt_enable FRSKYX_CC2500_INO AFHDS2A_A7105_INO MJXQ_NRF24L01_INO DSM_CYRF6936_INO;
    fi

  # Trim the enabled protocols down for the Atmega328p board
  - if [[ "$BOARD" =~ "multi4in1:avr:multiatmega328p:" ]]; then
      opt_disable $ALL_PROTOCOLS;
      opt_enable FRSKYX_CC2500_INO AFHDS2A_A7105_INO MJXQ_NRF24L01_INO DSM_CYRF6936_INO;
    fi

  # Useful Travis functions
  - export -f travis_fold
  - export -f travis_nanoseconds
  - export -f travis_time_start
  - export -f travis_time_finish
  - start_fold() { echo -e "travis_fold:start:$1"; }
  - end_fold() { echo -e "\ntravis_fold:end:$1\r"; }

script:
  # Build with default configuration - all protocols are enabled for STM32; a subset of protocols for Atmega or STM32 debugging
  - buildDefault

  # Serial only
  - buildSerialOnly

  # PPM only
  - buildPPMOnly

  # Re-enable PPM and serial
  - opt_enable ENABLE_SERIAL
  - opt_enable ENABLE_PPM

  # Build for each RF module individually
  - buildEachRFModule

  # Restore the default configuration
  - cp ./_Config.h.bak Multiprotocol/_Config.h

  # Build each protocol individually
  - buildEachProtocol

  # Restore the default configuration
  - cp ./_Config.h.bak Multiprotocol/_Config.h

  # Builds the files for a release - always built, but only copied to Github if the test is tagged as a release
  - buildReleaseFiles

deploy:
  provider: releases
  token:
    secure: KGXaoqvd8rbZ3AZtL9Rrn1JYiocGsPaihRUyR8gM8vTfvH9WYAE1+h6SzROQOuJSwr89MvTo3SBOTlM/0PDBnEGLec9Irt7cwO0xf9xM2vPuUG8DYcUzmJJzME9dkn/7qHof1JGgRpp1duUAN1triE9NxhKxL1hbs+tUUbDPAejxwoFNfnta/T4PfD6xmkZNJbneIfYFuFgyLpwwFhuUy9JP7s1AFOiT+fCHxPaZrPn5GsXqAi95Cb7Q3w1iVSt3BmrGxL2j3CeNpWzFY1RrMdc8ay+ppOhSPEIl2vyM7VeLRRBL3EVeFWkiS4ywevqw70wOivTczluv3OeuIJAe5o2UU+w5+59c7+i44Nih23PDAZBhAG5JkLUYUN0XUJpXJ5ZlZsb8IS8sI1txlZa5tNVoXO9+soGEY4rKSpZaPptuENm792CzzAjcaUI9pOFJ/0CBoSCbu5MpM/plkJCMd8fY27EE8cNYvolMuRATNlXs7h9mURGR69pmcR1jFShH+A7Kyp1S1sH19sGCEU16rt2aAtf2FadFg/gKACC2y9rB3wBb4Qnapu2AwNRlTYNuU1+G+kb2FXRwMl04q+38S+cIBHH9NHfdftp9MRPf8Ekatojs92be/Ux21S+hcA7sx/DV22Dl45V6l4mXzR7U4x1nQcdn1SGuy5I4lL6IYCk=
  skip_cleanup: true
  file_glob: true
  file: binaries/*
  on:
    tags: true
