#!/usr/bin/env bash

# Build a Mazer collection tarball with Ansible artifacts
# Usage: run "make collection" in the root of the repository

# Copyright (C) 2019 Maciej Delmanowski <drybjed@gmail.com>
# Copyright (C) 2019 DebOps https://debops.org/


set -o nounset -o pipefail -o errexit

# Check if we are in the correct directory
if ! [ -f lib/mazer/make-collection ] ; then
    printf "%s\\n" "Error: this script needs to be executed in the root of the DebOps repository"
    exit 1
fi

# Check if mazer is installed
if ! type mazer > /dev/null 2>&1 ; then
    printf "%s\\n" "Error: mazer not found"
    exit 1
fi

# Check if the repository is in a consistent state
if ! git diff --quiet && git diff --cached --quiet ; then
    printf "%s\\n" "Error: there are uncommitted changes in the repository"
    git status --short
    exit 1
fi

workdir="$(mktemp -d /tmp/debops.XXXXXXXXXX)"

file_list=(
    README.md
    LICENSE
    INSTALL.rst
    CHANGELOG.rst
)

mkdir -p "${workdir}/roles" \
         "${workdir}/plugins/callback" \
         "${workdir}/plugins/connection" \
         "${workdir}/plugins/filter" \
         "${workdir}/plugins/lookup" \
         "${workdir}/plugins/modules"

# Drop the 'debops.' prefix from role directories so that Ansible Galaxy
# accepts them as roles in a collection
pushd ansible/roles > /dev/null
for role in debops.* ; do
    cp -r "${role}" "${workdir}/roles/${role#debops.}"
done
popd > /dev/null

# Remove the 'role_name' parameter which is not used in Galaxy Collections
find "${workdir}"/roles/*/meta/main.yml -type f -exec sed -i -e '/role_name/d' {} +

# Copy various Ansible plugins to the directories where collection expects them
cp ansible/plugins/callback_plugins/*.py "${workdir}/plugins/callback"
cp ansible/plugins/connection_plugins/*.py "${workdir}/plugins/connection"
cp ansible/roles/debops.ansible_plugins/filter_plugins/*.py "${workdir}/plugins/filter"
cp ansible/roles/debops.ansible_plugins/lookup_plugins/*.py "${workdir}/plugins/lookup"
cp ansible/roles/debops.ansible_plugins/library/*.py "${workdir}/plugins/modules"

for filename in "${file_list[@]}" ; do
    cp "${filename}" "${workdir}/"
done

# Generate the 'galaxy.yml' file
cat <<EOF > "${workdir}/galaxy.yml"
---

# Configuration file used by Mazer to build a collection of Ansible artifacts.

namespace: "debops"
name: "debops"
version: "$(git describe | tr -d 'v')"
description: "Your Debian-based data center in a box"

authors:
  - "Maciej Delmanowski <drybjed@gmail.com>"
  - "DebOps Developers <debops-users@lists.debops.org>"

repository: "https://github.com/debops/debops"
documentation: "https://docs.debops.org/en/master/ansible/role-index.html"
homepage: "https://debops.org/"
issues: "https://github.com/debops/debops/issues"

dependencies: {}

readme: "README.md"
license:
  - "GPL-3.0-only"
license_file: "LICENSE"

tags:
  - "debian"
  - "ubuntu"
  - "sysadmin"
  - "cluster"
  - "datacenter"
  - "collection"
EOF

pushd "${workdir}" > /dev/null
mazer build
popd > /dev/null
