# Use the latest 2.1 version of CircleCI pipeline process engine.
# See: https://circleci.com/docs/configuration-reference
version: 2.1
parameters:
  run_build:
    type: boolean
    default: false
  environment:
    type: string
    default: "m5stack-atom"
  build_flags:
    type: string
    default: ""
  config:
    type: string
    default: "{}"
  suffix:
    type: string
    default: ""
orbs:
  python: circleci/python@1.4.0

# Define a job to be invoked later in a workflow.
# See: https://circleci.com/docs/configuration-reference/#jobs
jobs:
  pio-build:
    executor: python/default
    # Add steps to the job
    # See: https://circleci.com/docs/configuration-reference/#steps
    environment:
      PLATFORMIO_BUILD_FLAGS: << pipeline.parameters.build_flags >>
    steps:
      - checkout
      - run:
          name: "platformio install"
          command: "pip install --upgrade platformio"
      - run:
          name: "build"
          command: "pio run -e << pipeline.parameters.environment >>"
      - run:
          name: "save config"
          working_directory: ".pio/build/<< pipeline.parameters.environment >>"
          command: "echo '<< pipeline.parameters.config >>' > config.json"
      - run:
          name: "save build config"
          working_directory: ".pio/build/<< pipeline.parameters.environment >>"
          command: "echo 'GIT_SHA=\"<< pipeline.git.revision >>\" PLATFORMIO_BUILD_FLAGS=\"<< pipeline.parameters.build_flags >>\" pio run -e << pipeline.parameters.environment >> ' > buildconfig.txt"
      - run:
          name: "rename"
          working_directory: ".pio/build/<< pipeline.parameters.environment >>"
          command: "mv firmware.bin << pipeline.parameters.environment >><< pipeline.parameters.suffix >>-update.bin"
      - when:
          condition: 
            not: 
              equal: [ << pipeline.parameters.suffix >> ,""]
          steps:
            - run:
                name: "rename2"
                working_directory: ".pio/build/<< pipeline.parameters.environment >>"
                command: "mv << pipeline.parameters.environment >>-all.bin << pipeline.parameters.environment >><< pipeline.parameters.suffix >>-all.bin"
      - run:
          name: "compress"
          working_directory: ".pio/build/<< pipeline.parameters.environment >>"
          command: "zip << pipeline.parameters.environment >><< pipeline.parameters.suffix >>.zip << pipeline.parameters.environment >>*.bin config.json buildconfig.txt"
      - store_artifacts:
          path: .pio/build/<< pipeline.parameters.environment >>/<< pipeline.parameters.environment >><< pipeline.parameters.suffix >>.zip
          destination: << pipeline.parameters.environment >><< pipeline.parameters.suffix >>.zip
# Orchestrate jobs using workflows
# See: https://circleci.com/docs/configuration-reference/#workflows
workflows:
  build-workflow:
    when: << pipeline.parameters.run_build >>
    jobs:
      - pio-build:
          filters:
            tags:
             only: /.*/