build:
  image: europeanxfel/karabo-ci:ubuntu-22-003
  script:
    # Set up Arduino environment
    - cat /etc/os-release
    - apt-get install -y wget python3-pip rename
    - wget -nv
      https://downloads.arduino.cc/arduino-cli/arduino-cli_latest_Linux_64bit.tar.gz
    - tar -xf arduino-cli_latest_Linux_64bit.tar.gz
    - mv arduino-cli /usr/bin/arduino-cli

    # Configure arduino-cli
    - echo "board_manager:" > arduino-cli.yaml
    - echo "  additional_urls:" >> arduino-cli.yaml
    - echo "    - https://dl.espressif.com/dl/package_esp32_index.json" >> arduino-cli.yaml
    - echo "library:" >> arduino-cli.yaml
    - echo -n "  enable_unsafe_install:" >> arduino-cli.yaml
    - echo " true" >> arduino-cli.yaml
    - cat -E arduino-cli.yaml

    # Set up esp32 support
    - arduino-cli core update-index --config-file arduino-cli.yaml
    - arduino-cli core install esp32:esp32

    # Add pyserial for esp32 tools to chooch
    - pip3 install pyserial

    # Install dependencies
    - arduino-cli lib install Ethernet
    - arduino-cli lib install MCP23S08@0.5.2
    - arduino-cli lib install --git-url https://github.com/patricklaf/SNMP.git --config-file arduino-cli.yaml  # Awaiting 1.4.1+ tag

    # Set version, tag if available else commit sha
    - ICBM_GIT_VERSION=$CI_COMMIT_SHORT_SHA
    - if [[ -v CI_COMMIT_TAG ]]; then ICBM_GIT_VERSION=$CI_COMMIT_TAG; fi;
    - echo "Firmware version is $ICBM_GIT_VERSION"

    # Compile artifact
    - arduino-cli compile --fqbn esp32:esp32:esp32wroverkit icbm.ino --output-dir icbm-$ICBM_GIT_VERSION --build-property build.extra_flags="-DICBM_GIT_VERSION=\"$ICBM_GIT_VERSION\" -DICBM_GIT_TIMESTAMP=\"$CI_COMMIT_TIMESTAMP\""
    - rename "s/ino/$ICBM_GIT_VERSION.ino/" icbm-$ICBM_GIT_VERSION/*
    - zip -r icbm-$ICBM_GIT_VERSION.zip icbm-$ICBM_GIT_VERSION
  artifacts:
    when: always
    paths:
      - icbm-*.zip
    expire_in: 1 week
  interruptible: true
