version: 2

.job_steps: &job_steps
  steps:
    - checkout
    - run:
        name: "Build"
        no_output_timeout: 30m
        command: |
          BRANCH=$([ -z "$CIRCLE_TAG" ] && echo "$CIRCLE_BRANCH" || echo "master")
          bash .circleci/build-CI.sh "-DSTATIC=${STATIC}" "${BRANCH}" "${CIRCLE_TAG}" "${CIRCLE_JOB}"
    - run:
        name: "Binary checks"
        command: bash test/arch_test.sh
    - run:
        name: "Compute checksum"
        command: |
          mv pihole-FTL "${BIN_NAME}"
          sha1sum pihole-FTL-* > ${BIN_NAME}.sha1
    - run:
        name: "Upload binary to binary bucket"
        command: |
          [ -z "${CIRCLE_PR_USERNAME}" ] || exit 0
          DIR="${CIRCLE_TAG:-${CIRCLE_BRANCH}}"
          mkdir -p ~/.ssh/
          ssh-keyscan -H $SSH_HOST >> ~/.ssh/known_hosts
          sftp -b - $SSH_USER@$SSH_HOST <<< "-mkdir html/${DIR}
          put ${BIN_NAME}* html/${DIR}"
    - run:
        name: "Verify uploaded binary"
        command: |
          [ -z "${CIRCLE_PR_USERNAME}" ] || exit 0
          DIR="${CIRCLE_TAG:-${CIRCLE_BRANCH}}"
          mkdir download
          cd download
          wget "https://ftl.pi-hole.net/${DIR}/${BIN_NAME}"
          wget "https://ftl.pi-hole.net/${DIR}/${BIN_NAME}.sha1"
          sha1sum -c "${BIN_NAME}.sha1"
          cd ..
    - run:
        name: "Test"
        command: |
          mv "${BIN_NAME}" pihole-FTL
          test/run.sh

.docker_template: &docker_template
  docker:
    - image: pihole/ftl-build:v1.8-$CIRCLE_JOB
  <<: *job_steps

jobs:
  armv4t:
    <<: *docker_template
    environment:
      BIN_NAME: "pihole-FTL-armv4-linux-gnueabi"

  armv5te:
    <<: *docker_template
    environment:
      BIN_NAME: "pihole-FTL-armv5-linux-gnueabi"

  armv6hf:
    <<: *docker_template
    environment:
      BIN_NAME: "pihole-FTL-armv6-linux-gnueabihf"

  armv7hf:
    <<: *docker_template
    environment:
      BIN_NAME: "pihole-FTL-armv7-linux-gnueabihf"

  armv8a:
    <<: *docker_template
    environment:
      BIN_NAME: "pihole-FTL-armv8-linux-gnueabihf"

  aarch64:
    <<: *docker_template
    environment:
      BIN_NAME: "pihole-FTL-aarch64-linux-gnu"

  x86_64:
    <<: *docker_template
    environment:
      BIN_NAME: "pihole-FTL-linux-x86_64"

  x86_64-musl:
    <<: *docker_template
    environment:
      BIN_NAME: "pihole-FTL-musl-linux-x86_64"

  x86_32:
    <<: *docker_template
    environment:
      BIN_NAME: "pihole-FTL-linux-x86_32"

workflows:
  version: 2
  build:
    jobs:
      - armv4t:
          filters:
            tags:
              only: /^v.*/
      - armv5te:
          filters:
            tags:
              only: /^v.*/
      - armv6hf:
          filters:
            tags:
              only: /^v.*/
      - armv7hf:
          filters:
            tags:
              only: /^v.*/
      - armv8a:
          filters:
            tags:
              only: /^v.*/
      - aarch64:
          filters:
            tags:
              only: /^v.*/
      - x86_64:
          filters:
            tags:
              only: /^v.*/
      - x86_64-musl:
          filters:
            tags:
              only: /^v.*/
      - x86_32:
          filters:
            tags:
              only: /^v.*/

