#!/bin/bash

# This file loops through all the folders that are two levels deep
# in the ./roles folder that is in the base of this repository.

if [[ -d "$PWD/roles" ]]; then
	find "$PWD/roles" -mindepth 2 -maxdepth 2 -type d -not -path '*/\.*' | while read -r FOLDER; do
		echo "Changing directory to $FOLDER"
		cd "$FOLDER" || exit
    #cp ~/gas-station/roles/applications/androidstudio/Taskfile.yml .
    #rm -rf .config
    #cp -rf ~/testy .config
    #TMP="$(mktemp)"
    #DIRRR="$(basename $PWD)"
    #GITHUBY="https://github.com/ProfessorManhattan/ansible-$DIRRR"
    #GITLABY="https://gitlab.com/megabyte-labs/ansible-roles/$DIRRR"
		#jq --arg dirrr "$DIRRR" --arg githuby "$GITHUBY" --arg gitlaby "$GITLABY" '.type = "module" | .blueprint.group = "ansible" | .blueprint.subgroup = "role" | .blueprint.slug = $dirrr | .blueprint.repository.github = $githuby | .blueprint.repository.gitlab = $gitlaby' package.json > "$TMP"
		#mv "$TMP" package.json
		#yq e -i '.galaxy_info.namespace = "professormanhattan"' meta/main.yml
    #yq e -i '.galaxy_info.min_ansible_version = 2.10' meta/main.yml
    #yq e -i '.galaxy_info.license = "license (MIT)"' meta/main.yml
    #yq e -i '.galaxy_info.company = "Megabyte Labs"' meta/main.yml
    #yq e -i '.galaxy_info.author = "ProfessorManhattan"' meta/main.yml
    #rm -rf molecule
    #cp -rf /home/hawkwood/gas-station/nodejs/molecule/ .
    #rm -rf /home/hawkwood/gas-station/nodejs/molecule/.results/logs
    #rm -rf .config
    #cp -rf /home/hawkwood/gas-station/nodejs/.config/ .
    #rm -rf .gitlab
    #cp -rf /home/hawkwood/gas-station/nodejs/.gitlab/ .
    #cp /home/hawkwood/gas-station/nodejs/Taskfile.yml Taskfile.yml
    #rm -rf test
    #cp -rf /home/hawkwood/gas-station/nodejs/test/ .
    #ROLE_NAME="$(basename $PWD)" yq eval -i '.[0].roles[0].role = "professormanhattan." + env(ROLE_NAME)' molecule/converge.yml
    #cp /home/hawkwood/gas-station/roles/languages/nodejs/poetry.lock poetry.lock
    #cp /home/hawkwood/gas-station/roles/languages/nodejs/pyproject.toml pyproject.toml
    if [ "$FOLDER" != '/home/hawkwood/gas-station/roles/applications/androidstudio' ]; then
    rm -f .flake8
    rm -f .prettierignore
    rm -f .start.sh
    rm -f requirements.txt
    rm -f .ansible-lint
    rm -f .yamllint
    mkdir docs
    mv CODE_OF_CONDUCT.md docs || true
    mv CONTRIBUTING.md docs || true
    rm -rf .husky
    rm -rf .gitlab
    cp -rf ../../../roles/applications/androidstudio/.gitlab .gitlab
    rm -rf .gitlab/ci
    rm -rf .github
    cp -rf ../../../roles/applications/androidstudio/.github .github
    cp ../../applications/androidstudio/Taskfile.yml Taskfile.yml
    rm -rf .config
    cp -rf ../../../roles/applications/androidstudio/.config .config
    rm -rf .vscode
    fi
    #poetry config virtualenvs.create true
    #poetry install
    #task ansible:test:molecule:virtualbox:matrix || continue
  done
else
	echo "ERROR: This script should be run from the root of the repository where the roles directory resides."
fi
