---
version: '3'

vars:
  TEMPLATE_FILE: '{{if .CLI_ARGS}}{{.CLI_ARGS}}{{else}}template.json{{end}}'

tasks:
  descriptions:
    deps:
      - :install:software:jq
    log:
      error: Error populating `{{.TEMPLATE_FILE}}` with descriptions
      start: Populating `{{.TEMPLATE_FILE}}` with descriptions
      success: Populated `{{.TEMPLATE_FILE}}` with descriptions
    cmds:
      - |
        DESCRIPTION_TEMPLATE="$(jq -r '.description_template' '{{.VARIABLES_PATH}}')"
        VERSION_DESCRIPTION="$(jq -r '.version_description' '{{.VARIABLES_PATH}}')"
        TMP="$(mktemp)"
        jq --arg a "$DESCRIPTION_TEMPLATE" --arg b "$VERSION_DESCRIPTION" \
        '.variables.description = $a | .variables.version_description = $b' {{.TEMPLATE_FILE}} > "$TMP"
        mv "$TMP" {{.TEMPLATE_FILE}}
    sources:
      - '{{.TEMPLATE_FILE}}'
      - '{{.VARIABLES_PATH}}'

  readme:
    log:
      error: Encountered error while updating README.md for Packer template
      start: Updating README.md for Packer template
      success: Updated README.md for Packer template
    cmds:
      - task: readme:platform
        vars:
          TYPE: hyperv-iso
      - task: readme:platform
        vars:
          TYPE: parallels-iso
      - task: readme:platform
        vars:
          TYPE: qemu
      - task: readme:platform
        vars:
          TYPE: virtualbox-iso
      - task: readme:platform
        vars:
          TYPE: vmware-iso

  readme:platform:
    log:
      start: Determining whether to update README.md for `{{.TYPE}}`
    cmds:
      - task: readme:platform:{{OS}}
        vars:
          TYPE: '{{.TYPE}}'
    status:
      - |
        grep -q '"type": "{{.TYPE}}"' {{.TEMPLATE_FILE}}

  readme:platform:darwin:
    log:
      error: Error updating README.md with supported OS information for Packer template
      start: Updating README.md with supported OS information for Packer template
      success: Updated README.md with supported OS information for Packer template
    cmds:
      - sed -i .bak '/SUPPORTED_OS_{{.TYPE}}/d' README.md && rm README.md.bak

  readme:platform:linux:
    log:
      error: Error updating README.md with supported OS information for Packer template
      start: Updating README.md with supported OS information for Packer template
      success: Updated README.md with supported OS information for Packer template
    cmds:
      - sed -i '/SUPPORTED_OS_{{.TYPE}}/d' README.md

  variables:
    deps:
      - :install:software:jq
    vars:
      ISO_VERSION:
        sh: jq -r '.variables.iso_version' {{.TEMPLATE_FILE}}
      MAJOR_VERSION:
        sh: cut -d '.' -f 1 <<< {{.ISO_VERSION}}
      MINOR_VERSION:
        sh: cut -d '.' -f 2 <<< {{.ISO_VERSION}}
    log:
      error: Error updating `{{.VARIABLES_PATH}}` with variables from `{{.TEMPLATE_FILE}}`
      start: Updating `{{.VARIABLES_PATH}}` with variables from `{{.TEMPLATE_FILE}}`
      success: Updated `{{.VARIABLES_PATH}}` with variables from `{{.TEMPLATE_FILE}}`
    cmds:
      - |
        TEMPLATE_JSON="$(jq -r '.' {{.TEMPLATE_FILE}})"
        TMP="$(mktemp)"
        jq -S --arg templatejson "$TEMPLATE_JSON" '.template_json = ($templatejson | fromjson)' '{{.VARIABLES_PATH}}' > "$TMP"
        mv "$TMP" '{{.VARIABLES_PATH}}'
      - task: variables:{{OS}}
        vars:
          ISO_VERSION: '{{.ISO_VERSION}}'
          MAJOR_VERSION: '{{.MAJOR_VERSION}}'
          MINOR_VERSION: '{{.MINOR_VERSION}}'
    sources:
      - '{{.TEMPLATE_FILE}}'
      - '{{.VARIABLES_PATH}}'
    preconditions:
      - sh: test -f {{.TEMPLATE_FILE}}
        msg: 'A `{{.TEMPLATE_FILE}}` file is not present. This project uses values stored in `{{.TEMPLATE_FILE}}` to generate certain
          meta artifacts. Please add a `{{.TEMPLATE_FILE}}`. You can find an example of one in
          [this repository](https://gitlab.com/megabyte-labs/packer/ubuntu-desktop)'
      - sh: test -f '{{.VARIABLES_PATH}}'
        msg: 'The `{{.VARIABLES_PATH}}` file is missing!'

  variables:darwin:
    log:
      error: Error updating major version
      start: Updating major version
      success: Updated major version
    cmds:
      - sed -i .bak "s^MAJOR_VERSION^{{.MAJOR_VERSION}}^g" '{{.VARIABLES_PATH}}' && rm '{{.VARIABLES_PATH}}.bak'
      - sed -i .bak "s^MINOR_VERSION^{{.MINOR_VERSION}}^g" '{{.VARIABLES_PATH}}' && rm '{{.VARIABLES_PATH}}.bak'
      - sed -i .bak "s^ISO_VERSION^{{.ISO_VERSION}}^g" '{{.VARIABLES_PATH}}' && rm '{{.VARIABLES_PATH}}.bak'

  variables:linux:
    log:
      error: Error updating major version
      start: Updating major version
      success: Updated major version
    cmds:
      - sed -i "s^MAJOR_VERSION^{{.MAJOR_VERSION}}^g" '{{.VARIABLES_PATH}}'
      - sed -i "s^MINOR_VERSION^{{.MINOR_VERSION}}^g" '{{.VARIABLES_PATH}}'
      - sed -i "s^ISO_VERSION^{{.ISO_VERSION}}^g" '{{.VARIABLES_PATH}}'
