#!/usr/bin/env bash
#
# Helper script to setup an Ansible development environment
#
# Copyright (C) 2025 The Tor Project, Inc.
# SPDX-License-Identifier: AGPL-3.0-or-later
#

# Parameters
DIRNAME="`dirname $0`"
BASEPATH="$DIRNAME/.."
ROLE="${ROLE:-ancible}"
DEPENDENCIES_APT="podman podman-compose pipenv ansible make"

# Check for sudo
if [ "`whoami`" != "root" ]; then
  SUDO="sudo"
fi

# System upgrade
$SUDO apt-get update
$SUDO apt-get upgrade -y

# Install dependencies
$SUDO apt install -y $DEPENDENCIES_APT || exit 1

# Provision the virtualenv
$DIRNAME/ancible-provision-venv || exit 1

# Create a symlink so molecule and ansible-lint can find the role
for scenario in podman local; do
  if [ ! -e "$BASEPATH/molecule/$scenario/roles/$ROLE" ]; then
    mkdir -p $BASEPATH/molecule/$scenario/roles && \
      ln -sf ../../.. $BASEPATH/molecule/$scenario/roles/$ROLE
  fi
done
