---
version: '3'

vars:
  ENV_GOBIN:
    sh: mktemp
  ENV_GOROOT:
    sh: mktemp
  ENV_PATH:
    sh: mktemp

env:
  PREPARE_PROJECT: 'true'
  UPDATE_GIT_REPOS: 'true'

tasks:
  ci:
    run: once
    cmds:
      - task: setup
    status:
      - '[ -n "$CI" ]'

  commit:
    run: once
    cmds:
      - task: :git:commit:automated
      - task: :git:push:all
      - task: :ci:synchronize

  dev:
    cmds:
      - task: setup
    status:
      - '([ -z "$CI" ] && [ -f /.dockerenv ])'

  devcontainer:
    cmds:
      - task: :install:modules:local
      - task: :install:python:requirements:poetry
      - task: :git:remotes
    status:
      - '[ ! -f /.devcontainer ]'

  docker:
    cmds:
      - task: docker:taskfile
    status:
      - '[ "{{.DOCKER_BUILDING}}" != "true" ]'

  docker:taskfile:
    cmds:
      - task: :deps:run:parallel
    status:
      - '[ ! -f Taskfile.yml ]'

  init:
    run: once
    log:
      error: Error encountered while initializing the update process
      start: Initializing the update process
      success: Finished initializing the update process
    cmds:
      - task: :ci:before
      - task: :ci:submodules
      - task: :repair

  prepare:
    run: once
    cmds:
      - task: :prepare
    status:
      - '[ "$PREPARE_PROJECT" != "true" ] || [ -f /.devcontainer ]'

  prereqs:
    env:
      PATH:
        sh: |
          if type brew &> /dev/null; then
            echo "$PATH"
          else
            . "$HOME/.profile" &> /dev/null || true
            echo "$PATH"
          fi
    run: once
    log:
      error: Error encountered while installing pre-requisite software
      start: Ensuring pre-requisite software is installed and added to the PATH
      success: Pre-requisite software is installed
    cmds:
      - task: :install:software:jq
      - task: :install:software:yq
      - task: :install:software:node
      - task: :install:software:poetry
      - task: :install:software:go
      - task: :install:software:task

  register:env:
    cmds:
      - cmd: |
          . "$HOME/.profile" &> /dev/null || true
          echo "$PATH" > '{{.ENV_PATH}}'
          echo "$GOBIN" > '{{.ENV_GOBIN}}'
          echo "$GOROOT" > '{{.ENV_GOROOT}}'
        ignore_error: true
    status:
      - type jq &> /dev/null
      - type yq &> /dev/null
      - type node &> /dev/null
      - type go &> /dev/null
      - type poetry &> /dev/null
      - '[ -n "$GOBIN" ]'
      - '[ -n "$GOROOT" ]'

  setup:
    run: once
    cmds:
      - task: :install:software:brew
      - task: prereqs
      - task: register:env
        vars:
          ENV_GOBIN: '{{.ENV_GOBIN}}'
          ENV_GOROOT: '{{.ENV_GOROOT}}'
          ENV_PATH: '{{.ENV_PATH}}'
      - task: setup:start
        env:
          GOBIN:
            sh: cat '{{.ENV_GOBIN}}'
          GOROOT:
            sh: cat '{{.ENV_GOROOT}}'
          PATH:
            sh: cat '{{.ENV_PATH}}'

  setup:start:
    run: once
    cmds:
      - task: setup:start:ci
      - task: setup:start:dev

  setup:start:ci:
    run: once
    cmds:
      - task: init
      - task: prepare
      - task: update
      - task: :ci:commit
      - task: :ci:synchronize
    status:
      - '[ -z "$CI" ]'

  setup:start:dev:
    run: once
    cmds:
      - task: init
      - task: prepare
      - task: update
      - task: commit
      - task: devcontainer
    status:
      - '[ -n "$CI" ]'

  update:
    run: once
    cmds:
      - task: :upstream:{{.PROJECT_TYPE}}
      - task: update:git
    status:
      - '[ "$UPDATE_PROJECT" == "true" ] || [ -f /.devcontainer ]'

  update:git:
    run: once
    cmds:
      - task: :git:update
    status:
      - '[ -n "$CI" ] || [ "{{.DOCKER_BUILDING}}" == "true" ]'
      - '[ "$UPDATE_GIT_REPOS" == "false" ] || [ "{{.DOCKER_BUILDING}}" == "true" ]]'
