---
version: '3'

vars:
  COMMON_FILES_URL: https://gitlab.com/megabyte-labs/common/shared.git
  COMMON_FOLDER: .common
  SHARED_DOCS_URL: https://gitlab.com/megabyte-labs/documentation/shared.git
  SHARED_FOLDER: .shared

tasks:
  clean:
    deps:
      - clean:common
      - clean:shared

  clean:common:
    log:
      error: Error running `rm -rf {{.COMMON_FOLDER}}`
      start: Running `rm -rf {{.COMMON_FOLDER}}`
      success: Successfully ran `rm -rf {{.COMMON_FOLDER}}`
    cmds:
      - rm -rf {{.COMMON_FOLDER}}

  clean:shared:
    log:
      error: Error running `rm -rf {{.SHARED_FOLDER}}`
      start: Running `rm -rf {{.SHARED_FOLDER}}`
      success: Successfully ran `rm -rf {{.SHARED_FOLDER}}`
    cmds:
      - rm -rf {{.SHARED_FOLDER}}

  clone:
    deps:
      - clone:common
      - clone:shared

  clone:common:
    log:
      error: Error encountered in clone logic for {{.COMMON_FILES_URL}}
      start: Cloning {{.COMMON_FILES_URL}}
      success: Successfully cloned {{.COMMON_FILES_URL}}
    cmds:
      - rm -rf {{.COMMON_FOLDER}}
      - git clone --depth=1 {{.COMMON_FILES_URL}} {{.COMMON_FOLDER}}
      - rm -rf {{.COMMON_FOLDER}}/.git

  clone:shared:
    log:
      error: Error encountered in clone logic for {{.SHARED_DOCS_URL}}
      start: Cloning {{.SHARED_DOCS_URL}}
      success: Successfully cloned {{.SHARED_DOCS_URL}}
    cmds:
      - rm -rf {{.SHARED_FOLDER}}
      - git clone --depth=1 {{.SHARED_DOCS_URL}} {{.SHARED_FOLDER}}
      - rm -rf {{.SHARED_FOLDER}}/.git

  copy:
    deps:
      - :install:software:yq
    log:
      error: Error encountered in copy sequence
      start: Beginning copy sequence
      success: Completed copy sequence
    cmds:
      - mv .gitlab-ci.yml old.gitlab-ci.yml
      - task: copy:before
      - |
        yq eval-all -i 'select(fileIndex == 0) * select(fileIndex == 1)' old.gitlab-ci.yml .gitlab-ci.yml
        mv old.gitlab-ci.yml .gitlab-ci.yml
      - cp {{.SHARED_FOLDER}}/.gitlab-ci.yml .gitlab-ci.yml

  copy:before:
    deps:
      - copy:common
      - copy:shared

  copy:common:
    deps:
      - :install:software:coreutils
    log:
      error: Error copying common files
      start: Copying common files
      success: Copied common files
    cmds:
      - |
        {{if (eq OS "darwin")}}PATH="$(brew --prefix)/opt/coreutils/libexec/gnubin:$PATH"{{end}}
        cp -rT ./{{.COMMON_FOLDER}}/common/ .
      - task: :common:husky:permissions

  copy:shared:
    log:
      error: Error encountered while copying shared files
      start: Copying shared files
      success: Copied shared files
    cmds:
      - rm -rf common
      - mv {{.SHARED_FOLDER}}/common common
      - mv {{.SHARED_FOLDER}}/README.md README.md
      - mkdir -p docs
      - mv {{.SHARED_FOLDER}}/docs/CONTRIBUTING.md docs/CONTRIBUTING.md

  merge:
    deps:
      - :install:software:jq
    log:
      error: Error combining common.json files
      start: Combining common.json files
      success: Combined common.json files
    cmds:
      - jq -s -S '.[0] * .[1]' {{.SHARED_FOLDER}}/common.json common.json > variables.json

  template:
    cmds:
      - task: :upstream:variables
        vars:
          INPUT_FILE: variables.json
          OUTPUT_FILE: ./.variables.json
      - task: :upstream:template
