---
version: '3'

tasks:
  labels:
    deps:
      - :install:software:jq
    vars:
      GROUP_URL:
        sh: jq -r '.repository.group.dockerfile' .variables.json
    log:
      start: Ensuring `Dockerfile` labels are up-to-date
      success: '`Dockerfile` labels are up-to-date'
    cmds:
      - task: labels:add
      - task: labels:update
    sources:
      - .variables.json
      - Dockerfile
      - package.json
    preconditions:
      - sh: test -f Dockerfile
        msg: A `Dockerfile` is not present in the root of this project!
      - sh: test -f .variables.json
        msg: The `.variables.json` file is not present in the root of this project!
      - sh: test -f package.json
        msg: The `package.json` file is not present in the root of this project!

  labels:add:
    vars:
      AUTHORS:
        sh: jq -r '.docker_label_authors' .variables.json
      DESCRIPTION:
        sh: jq -r '.description' package.json
      HELP_EMAIL:
        sh: jq -r '.email.help' .variables.json
      LICENSE:
        sh: jq -r '.license' .variables.json
      ORGANIZATION:
        sh: jq -r '.organization' .variables.json
      SOURCE:
        sh: jq -r '.blueprint.repository.github' package.json
      URL:
        sh: jq -r '.link.home' .variables.json
    log:
      error: Failed to add Dockerfile labels
      start: Ensuring default Dockerfile labels are present (e.g. `org.opencontainers.image` tags)
      success: Successfully injected `org.opencontainers.image` labels into Dockerfile
    cmds:
      - |
        echo 'ARG BUILD_DATE' >> Dockerfile
        echo 'ARG REVISION' >> Dockerfile
        echo 'ARG VERSION' >> Dockerfile
        echo '' >> Dockerfile
        echo 'LABEL maintainer="{{.ORGANIZATION}} <{{.HELP_EMAIL}}>"' >> Dockerfile
        echo 'LABEL org.opencontainers.image.authors="{{.AUTHORS}}"' >> Dockerfile
        echo 'LABEL org.opencontainers.image.created=$BUILD_DATE' >> Dockerfile
        echo 'LABEL org.opencontainers.image.description="{{.DESCRIPTION}}"' >> Dockerfile
        echo 'LABEL org.opencontainers.image.documentation="{{.SOURCE}}/blob/master/README.md"' >> Dockerfile
        echo 'LABEL org.opencontainers.image.licenses="{{.LICENSE}}"' >> Dockerfile
        echo 'LABEL org.opencontainers.image.revision=$REVISION' >> Dockerfile
        echo 'LABEL org.opencontainers.image.source="{{.SOURCE}}"' >> Dockerfile
        echo 'LABEL org.opencontainers.image.url="{{.URL}}"' >> Dockerfile
        echo 'LABEL org.opencontainers.image.vendor="{{.ORGANIZATION}}"' >> Dockerfile
        echo 'LABEL org.opencontainers.image.version=$VERSION' >> Dockerfile
        echo 'LABEL space.megabyte.type="{{.REPOSITORY_SUBTYPE}}"' >> Dockerfile
        echo '' >> Dockerfile
    status:
      - grep 'org.opencontainers.image.documentation' Dockerfile

  # yamllint disable rule:line-length
  labels:update:
    vars:
      AUTHORS:
        sh: jq -r '.docker_label_authors' .variables.json
      CMD_PREFIX: sed -i{{if (eq OS "darwin")}} .bak{{end}}
      GROUP_URL:
        sh: jq -r '.repository.group.dockerfile' .variables.json
      LICENSE:
        sh: jq -r '.license' .variables.json
      ORGANIZATION:
        sh: jq -r '.organization' .variables.json
      URL:
        sh: jq -r '.link.home' .variables.json
    env:
      DESCRIPTION:
        sh: jq -r '.description' package.json
      REPOSITORY_GITHUB:
        sh: jq -r '.blueprint.repository.github' package.json
    log:
      error: Encountered error while updating `Dockerfile` labels
    cmds:
      - |
        {{.CMD_PREFIX}} "s^.*org.opencontainers.image.authors.*^LABEL org.opencontainers.image.authors=\"{{.AUTHORS}}\"^g" Dockerfile
        {{.CMD_PREFIX}} "s^.*org.opencontainers.image.description.*^LABEL org.opencontainers.image.description=\"${DESCRIPTION}\"^g" Dockerfile
        {{.CMD_PREFIX}} "s^.*org.opencontainers.image.documentation.*^LABEL org.opencontainers.image.documentation=\"${REPOSITORY_GITHUB}/blob/master/README.md\"^g" Dockerfile
        {{.CMD_PREFIX}} "s^.*org.opencontainers.image.licenses.*^LABEL org.opencontainers.image.licenses=\"{{.LICENSE}}\"^g" Dockerfile
        {{.CMD_PREFIX}} "s^.*org.opencontainers.image.source.*^LABEL org.opencontainers.image.source=\"${REPOSITORY_GITHUB}.git\"^g" Dockerfile
        {{.CMD_PREFIX}} "s^.*org.opencontainers.image.url.*^LABEL org.opencontainers.image.url=\"{{.URL}}\"^g" Dockerfile
        {{.CMD_PREFIX}} "s^.*org.opencontainers.image.vendor.*^LABEL org.opencontainers.image.vendor=\"{{.ORGANIZATION}}\"^g" Dockerfile
      - rm -f Dockerfile.bak
  # yamllint enable rule:line-length

  sort:
    todo: Make this remove duplicates using a command like `uniq -u`
    log:
      error: Encountered error while sorting `*.txt` files in the `./local` folder`
      start: Sorting, removing empty lines, and removing duplicate lines from all `*.txt` files in the `./local` folder
      success: Successfully sorted `*.txt` files in the `./local` folder
    cmds:
      - find ./local -type f -name '*.txt' -exec sort {} -o {} \; -exec sed -i '/^$/d' {} \;
    status:
      - '! find ./local -type f -name "*.txt" | grep txt'

  version:
    deps:
      - :install:software:jq
    vars:
      VERSION_COMMAND:
        sh: jq -r '.blueprint.versionCommand' package.json
    log:
      error: Error acquiring software version
      start: Acquiring software version
      success: Finished software version acquisition sequence
    cmds:
      - docker build --tag {{.DOCKER_IMAGE}}:latest  --target "{{.DOCKER_BASE_TAG}}" .
      - |
        if [ -n "$VERSION_OVERRIDE" ]; then
          VERSION="$VERSION_OVERRIDE"
        else
          VERSION="$({{.VERSION_COMMAND}})" || EXIT_CODE=$?
          VERSION="$(echo "$VERSION" | perl -pe 'if(($v)=/([0-9]+([.][0-9]+)+)/){print"$v\n";exit}$_=""')"
        fi
        if [ -n "$EXIT_CODE" ]; then
          .config/log warn 'Failed to run version command (`{{.VERSION_COMMAND}}`) - will retry after images are finished building'
        else
          CURR_VERSION="$(jq -r '.version' package.json)"
          TMP="$(mktemp)"
          jq --arg version "$VERSION" '.version = $version' package.json > "$TMP"
          mv "$TMP" package.json
          if [ "$CURR_VERSION" != "$(jq -r '.version' package.json)" ]; then
            .config/log info 'Updating documentation with new software version'
            task common:update:update
          fi
        fi
    status:
      - '[ "{{.VERSION_COMMAND}}" == "null" ]'
