---
version: '3'

vars:
  DEFAULT_ANSIBLE_LICENSE: license (MIT)
  DEFAULT_NAMESPACE: professormanhattan
  GITHUB_ROLE_PATH_PREFIX: https://github.com/ProfessorManhattan/ansible-
  GITLAB_ROLE_PATH_PREFIX: https://gitlab.com/megabyte-labs/ansible-roles/
  MIN_ANSIBLE_VERSION: 2.10

tasks:
  all:after:
    cmds:
      - |
        if [ "$(jq -r '.blueprint.overview' package.json)" == 'null' ]; then
          .config/log warn 'The `blueprint.overview` field is missing from `package.json`'
        fi
        if [ "$(jq -r '.blueprint.description' package.json)" == 'null' ]; then
          .config/log error 'The `blueprint.description` field is missing from `package.json`'
          EXIT_PROGRAM=true
        fi
        if [ "$(jq -r '.blueprint.name' package.json)" == 'null' ]; then
          .config/log error 'The `blueprint.name` field is missing from `package.json`'
          EXIT_PROGRAM=true
        fi
        if [ "$EXIT_PROGRAM" == 'true' ]; then
          exit 1
        fi

  all:before:
    cmds:
      - |
        if [ ! -f package.json ]; then
          .config/log error 'The `package.json` file must exist. See `https://gitlab.com/megabyte-labs/ansible-roles/androidstudio` for an example of one.'
          exit 1
        fi
      - |
        GROUP="$(jq -r '.blueprint.group' package.json)"
        SUBGROUP="$(jq -r '.blueprint.subgroup' package.json)"
        TASK_GROUP="{{.REPOSITORY_TYPE}}"
        TASK_SUBGROUP="{{.REPOSITORY_SUBTYPE}}"
        if ([ "$GROUP" != 'null' ] && [ "$TASK_GROUP" != 'null' ] && [ "$GROUP" != "$TASK_GROUP" ]) || \
        ([ "$GROUP" == 'null' ] && [ "$TASK_GROUP" != 'null' ]); then
          .config/log info 'Setting `blueprint.group` in `package.json` equal to `vars.REPOSITORY_TYPE` from `Taskfile.yml`'
          TMP="$(mktemp)" && jq --arg group "$TASK_GROUP" '.blueprint.group = $group' package.json > "$TMP" && mv "$TMP" package.json
        elif [ "$GROUP" != 'null' ] && [ "$TASK_GROUP" == 'null' ]; then
          .config/log info 'Setting `vars.REPOSITORY_TYPE` equal to value in `blueprint.group` in `package.json`'
          yq e -i '.vars.REPOSITORY_TYPE = env(GROUP)' Taskfile.yml
        elif [ "$GROUP" == 'null' ] && [ "$TASK_GROUP" == 'null' ]; then
          .config/log error 'Either `blueprint.group` in `package.json` or `vars.REPOSITORY_TYPE` in `Taskfile.yml` must be defined'
          exit 1
        fi
        if ([ "$SUBGROUP" != 'null' ] && [ "$TASK_SUBGROUP" != 'null' ] && [ "$SUBGROUP" != "$TASK_SUBGROUP" ]) || \
        ([ "$SUBGROUP" == 'null' ] && [ "$TASK_SUBGROUP" != 'null' ]); then
          .config/log info 'Setting `blueprint.subgroup` in `package.json` equal to `vars.REPOSITORY_SUBTYPE` from `Taskfile.yml`'
          TMP="$(mktemp)" && jq --arg group "$TASK_SUBGROUP" '.blueprint.subgroup = $group' package.json > "$TMP" && mv "$TMP" package.json
        elif [ "$SUBGROUP" != 'null' ] && [ "$TASK_SUBGROUP" == 'null' ]; then
          .config/log info 'Setting `vars.REPOSITORY_SUBTYPE` equal to value in `blueprint.subgroup` in `package.json`'
          yq e -i '.vars.REPOSITORY_SUBTYPE = env(SUBGROUP)' Taskfile.yml
        elif [ "$SUBGROUP" == 'null' ] && [ "$TASK_SUBGROUP" == 'null' ]; then
          .config/log error 'Either `blueprint.subgroup` in `package.json` or `vars.REPOSITORY_SUBTYPE` in `Taskfile.yml` must be defined'
          exit 1
        fi

  angular: 'true'

  ansible:
    cmds:
      - task: all:before
      - task: ansible:{{.REPOSITORY_SUBTYPE}}
      - task: all:after

  ansible:playbook: 'true'

  ansible:role:
    cmds:
      - |
        if [ ! -f meta/main.yml ]; then
          .config/log error 'The `meta/main.yml` file must exist. See `https://gitlab.com/megabyte-labs/ansible-roles/androidstudio` for an example of one.'
          exit 1
        fi
      - |
        if [ "$(yq e '.galaxy_info.author' meta/main.yml)" == 'null' ]; then
          .config/log info 'Setting `author` to `{{.GALAXY_AUTHOR}}` in `meta/main.yml`'
          yq e -i '.galaxy_info.author = {{.GALAXY_AUTHOR}}' meta/main.yml
        fi
      - |
        if [ "$(yq e '.galaxy_info.company' meta/main.yml)" == 'null' ]; then
          .config/log info 'Setting `company` to `{{.GALAXY_COMPANY}}` in `meta/main.yml`'
          yq e -i '.galaxy_info.company = {{.GALAXY_COMPANY}}' meta/main.yml
        fi
      - |
        if [ "$(yq e '.galaxy_info.min_ansible_version' meta/main.yml)" == 'null' ]; then
          .config/log info 'Setting `min_ansible_version` to `{{.MIN_ANSIBLE_VERSION}}` in `meta/main.yml`'
          yq e -i '.galaxy_info.min_ansible_version = {{.MIN_ANSIBLE_VERSION}}' meta/main.yml
        fi
      - |
        if [ "$(yq e '.galaxy_info.license' meta/main.yml)" == 'null' ]; then
          .config/log info 'Setting `license` to `{{.DEFAULT_ANSIBLE_LICENSE}}` in `meta/main.yml`'
          yq e -i '.galaxy_info.license = {{.DEFAULT_ANSIBLE_LICENSE}}' meta/main.yml
        fi
      - |
        ROLE_NAME="$(yq e '.galaxy_info.role_name' meta/main.yml)"
        if [ "$ROLE_NAME" == 'null' ]; then
          .config/log warn 'The `meta/main.yml` file is missing the `.galaxy_info.role_name` property. Adding it as the folder name - please edit if necessary.'
          ROLE_NAME="$(basename $PWD)" yq e -i '.galaxy_info.role_name = env(ROLE_NAME)' meta/main.yml
        fi
        SLUG="$(jq -r '.blueprint.slug' package.json)"
        if [ "$SLUG" == 'null' ]; then
          .config/log info 'Adding `slug` to package.json'
          TMP="$(mktemp)" && jq --arg slug "$ROLE_NAME" '.blueprint.slug = $slug' package.json > "$TMP" && mv "$TMP" package.json
        fi
        GITLAB_REPO="$(jq -r '.blueprint.repository.gitlab' package.json)"
        if [ "$GITLAB_REPO" == 'null' ]; then
          GITLAB_REPO="{{.GITLAB_ROLE_PATH_PREFIX}}$ROLE_NAME"
          .config/log info 'Adding GitLab repository to `package.json`'
          TMP="$(mktemp)" && jq --arg repo "$GITLAB_REPO" '.blueprint.repository.gitlab = $repo' package.json > "$TMP" && mv "$TMP" package.json
        fi
        GITHUB_REPO="$(jq -r '.blueprint.repository.github' package.json)"
        if [ "$GITHUB_REPO" == 'null' ]; then
          GITHUB_REPO="{{.GITHUB_ROLE_PATH_PREFIX}}$ROLE_NAME"
          .config/log info 'Adding GitHub repository to `package.json`'
          TMP="$(mktemp)" && jq --arg repo "$GITHUB_REPO" '.blueprint.repository.github = $repo' package.json > "$TMP" && mv "$TMP" package.json
        fi
        if [ "$(yq e '.galaxy_info.issue_tracker_url' meta/main.yml)" == 'null' ]; then
          ISSUE_TRACKER="$GITLAB_REPO/-/issues"
          .config/log info 'Adding `issue_tracker_url` to `meta/main.yml`'
          yq e -i '.galaxy_info.issue_tracker_url = env(ISSUE_TRACKER)' meta/main.yml
        fi
      - |
        if [ "$(yq e '.galaxy_info.description' meta/main.yml)" == 'null' ]; then
          BP_DESC="$(jq -r '.blueprint.description' package.json)"
          if [ "$BP_DESC" == 'null' ]; then
            .config/log error 'The `description` in `meta/main.yml` is missing. It must be present.'
            .config/log info 'For an example `meta/main.yml` file see `https://gitlab.com/megabyte-labs/ansible-roles/androidstudio`.'
            exit 1
          else
            yq e -i '.galaxy_info.description = env(BP_DESC)' meta/main.yml
            .config/log info '`meta/main.yml` description populated using value from `package.json` `blueprint.description`'
          fi
        fi

  docker: 'true'

  go: 'true'

  npm: 'true'

  packer: 'true'

  python: 'true'
