---
version: '3'

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

tasks:
  build:
    deps:
      - :install:software:packer
    desc: Build Packer images for all available platforms
    summary: |
      # Build Packer images for all available virtualization platforms

      This task begins by removing cached files that may interfere with the Packer build
      process. It then runs `packer build template.json` if no arguments are passed. This
      command will build Packer images for all the virtualization platforms specified in
      the template file. The template file may include instructions for the following
      virtualization platforms (and it is possible that it includes other ones that are
      not listed below):

      * Hyper-V
      * KVM
      * Parallels
      * VMWare
      * VirtualBox

      If you would like to build machine images with another template then you can pass the
      template's file name as a parameter (see example below).

      **Example building on all available platforms:**
      `task packer:build`

      **Example using a template file named `another_template.json`:**
      `task packer:build -- another_template.json`
    hide:
      sh: '[ "{{.REPOSITORY_TYPE}}" != "packer" ]'
    log:
      error: Encountered error while running `packer build`
      start: Running `packer build` for all supported platforms that are installed
      success: Successfully built the Packer images
    cmds:
      - task: prepare:template
      - rm -rf build
      - |
        IMAGES=","
        if type qemu-system-x86_64 &> /dev/null; then
          .config/log info 'QEMU is available'
          IMAGES="${IMAGES},qemu"
        fi
        if [ '{{OS}}' == 'darwin' ] && mdfind -name 'Parallels Desktop.app' &> /dev/null; then
          .config/log info 'Parallels is installed'
          IMAGES="${IMAGES},parallels-iso"
        fi
        if type vboxmanage &> /dev/null; then
          .config/log info 'VirtualBox is installed'
          task app:virtualbox:clear
          IMAGES="${IMAGES},virtualbox-iso"
        fi
        if [ '{{OS}}' == 'linux' ] && type vmware &> /dev/null; then
          .config/log info 'VMWare Workstation is installed'
          IMAGES="${IMAGES},vmware-iso"
        fi
        if [ '{{OS}}' == 'darwin' ] && type vmrun &> /dev/null; then
          .config/log info 'VMWare Fusion is installed'
          IMAGES="${IMAGES},vmware-iso"
        fi
        packer build -only=$(echo $IMAGES | sed 's/^.//') {{if .CLI_ARGS}}{{.CLI_ARGS}}{{else}}template.json{{end}}

  install:platforms:
    desc: Install all available virtualization providers
    hide:
      sh: '[ "{{.REPOSITORY_TYPE}}" != "packer" ]'
    cmds:
      - task: :install:software:kvm
      - task: :install:software:parallels
      - task: :install:software:virtualbox
      - task: :install:software:vmware

  latestos:
    deps:
      - :install:pipx:latestos
      - :install:software:jq
    vars:
      TAG:
        sh: jq -r '.variables.latestos_tag' {{.TEMPLATE_FILE}}
    hide:
      sh: '[ "{{.REPOSITORY_TYPE}}" != "packer" ]'
    log:
      error: Error running `{{.PYTHON_HANDLE}}latestos {{.TAG}}`
      start: Running `{{.PYTHON_HANDLE}}latestos {{.TAG}}`
      success: Successfully ran `{{.PYTHON_HANDLE}}latestos {{.TAG}}`
    cmds:
      - |
        PATH="$PATH:$HOME/.local/bin"
        {{.PYTHON_HANDLE}}latestos {{.TAG}}
    status:
      - '[[ "{{.TAG}}}" == "macos" ]] || [ "${container:=}" == "docker" ]'
    preconditions:
      - sh: test -f {{.TEMPLATE_FILE}}
        msg: The `{{.TEMPLATE_FILE}}` file is missing from the root of this project.

  prepare: 'true'

  prepare:template:
    deps:
      - :install:software:jq
    log:
      error: Error preparing Packer template
      start: Preparing Packer template
      success: Successfully prepared Packer template
    cmds:
      - |
        TMP="$(mktemp)"
        if [ '{{OS}}' == 'linux' ]; then
          jq '.variables.accelerator = "kvm"' {{.TEMPLATE_FILE}} > "$TMP"
        elif [ '{{OS}}' == 'darwin' ]; then
          jq '.variables.accelerator = "hvf"' {{.TEMPLATE_FILE}} > "$TMP"
        else
          .config/log warn 'Windows not yet tested'
        fi
        mv "$TMP" {{.TEMPLATE_FILE}}

  publish: 'true'

  verify: 'true'