# Generated by `starci compile --target circleci` from shell-scripts.ci.star. Edit that, not this.
#
# Each job is one step of the plan: the same image, command, tools and
# packages, given only the step's inputs. A job skips its work when it has
# already passed with the same inputs.
#
# What does not carry over:
#  - Passes are remembered in this project's CircleCI cache, which expires.
#    starci's own store does not.

version: 2.1

jobs:
  shellcheck:
    machine:
      image: ubuntu-2404:current
    resource_class: medium
    steps:
      - checkout
      - run:
          name: "Keep only this step's inputs"
          command: |
            git sparse-checkout set --no-cone '/scripts/*.sh'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,glob)scripts/*.sh' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-shellcheck-ba360bc949a2f0ba-{{ checksum "/tmp/starci-inputs" }}
      - run:
          name: "Stop here if it has"
          command: |
            if [ -e ~/starci-passed/at ]; then
              echo "Passed before with the same inputs, on $(cat ~/starci-passed/at). Nothing to do."
              circleci-agent step halt
            fi
      - run:
          name: "Tool: shellcheck"
          command: |
            mkdir -p "$HOME/starci-tools/shellcheck"
            curl -fsSL 'https://github.com/koalaman/shellcheck/releases/download/v0.11.0/shellcheck-v0.11.0.linux.x86_64.tar.xz' -o /tmp/shellcheck.download
            echo "8c3be12b05d5c177a04c29e3c78ce89ac86f1595681cab149b65b97c4e227198  /tmp/shellcheck.download" | sha256sum -c -
            tar -xf "/tmp/shellcheck.download" -C "$HOME/starci-tools/shellcheck"
      - run:
          name: "shellcheck"
          environment:
            STARCI_RUN: |-
              shellcheck scripts/*.sh
          command: |
            docker pull -q debian:12-slim
            image_path=$(docker image inspect debian:12-slim --format '{{range .Config.Env}}{{println .}}{{end}}' | sed -n 's/^PATH=//p')
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              -v "$HOME/starci-tools/shellcheck:/opt/starci/tools/shellcheck:ro" \
              -e "PATH=/opt/starci/tools/shellcheck/shellcheck-v0.11.0:$image_path" \
              debian:12-slim sh -c "$STARCI_RUN"
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-shellcheck-ba360bc949a2f0ba-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

  json-is-valid:
    machine:
      image: ubuntu-2404:current
    resource_class: medium
    steps:
      - checkout
      - run:
          name: "Keep only this step's inputs"
          command: |
            git sparse-checkout set --no-cone '/config/*.json'
      - run:
          name: "Identify the inputs"
          command: |
            git ls-files -s -- ':(top,glob)config/*.json' > /tmp/starci-inputs
      - restore_cache:
          name: Has this already passed with these inputs?
          keys:
            - starci-passed-json-is-valid-278517b8325745c6-{{ checksum "/tmp/starci-inputs" }}
      - run:
          name: "Stop here if it has"
          command: |
            if [ -e ~/starci-passed/at ]; then
              echo "Passed before with the same inputs, on $(cat ~/starci-passed/at). Nothing to do."
              circleci-agent step halt
            fi
      - run:
          name: "Tool: jq"
          command: |
            mkdir -p "$HOME/starci-tools/jq"
            curl -fsSL 'https://github.com/jqlang/jq/releases/download/jq-1.8.1/jq-linux-amd64' -o /tmp/jq.download
            echo "020468de7539ce70ef1bceaf7cde2e8c4f2ca6c3afb84642aabc5c97d9fc2a0d  /tmp/jq.download" | sha256sum -c -
            install -m 755 "/tmp/jq.download" "$HOME/starci-tools/jq/jq"
      - run:
          name: "json is valid"
          environment:
            STARCI_RUN: |-
              for f in config/*.json; do jq empty "$f" || exit 1; done
          command: |
            docker pull -q debian:12-slim
            image_path=$(docker image inspect debian:12-slim --format '{{range .Config.Env}}{{println .}}{{end}}' | sed -n 's/^PATH=//p')
            docker run --rm \
              -v "$PWD:/workspace" \
              -w /workspace \
              -v "$HOME/starci-tools/jq:/opt/starci/tools/jq:ro" \
              -e "PATH=/opt/starci/tools/jq:$image_path" \
              debian:12-slim sh -c "$STARCI_RUN"
      - run:
          name: "Remember this pass"
          command: |
            mkdir -p ~/starci-passed && date > ~/starci-passed/at
      - save_cache:
          key: starci-passed-json-is-valid-278517b8325745c6-{{ checksum "/tmp/starci-inputs" }}
          paths:
            - ~/starci-passed

workflows:
  ci:
    jobs:
      - shellcheck
      - json-is-valid
