#!/usr/bin/env bash
set -ex

PATH="$PATH:$HOME/.local/bin"

if [[ $(whoami) == "root" ]]; then
    SUDO=""
else
    SUDO="sudo"
fi

if [[ $(command -v ansible) ]]; then 
    printf "ansible already installed\n"
elif [[ $(uname) == "Linux" ]]; then
    if [[ $(command -v pacman) ]]; then
        $SUDO pacman -Sy --noconfirm ansible
    elif [[ $(command -v zypper) ]]; then
        $SUDO zypper install -y ansible
        $SUDO ln -sf /usr/bin/python3 /usr/bin/python
    elif [[ $(command -v apt) ]]; then
        $SUDO apt install -y ansible python3-apt python-is-python3
    elif [[ $(command -v dnf) ]]; then
        $SUDO dnf install ansible -y
    elif [[ $(command -v emerge) ]]; then
        $SUDO emerge ansible
    fi
elif [[ $(uname) == "Darwin"  ]]; then
    if [[ ! $(command -v brew) ]]; then
        # Yeah, raw brew install
        /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
    fi
    if [[ $(arch) == 'arm64' ]]; then
        /opt/homebrew/bin/brew install ansible
    else
        /usr/local/bin/brew install ansible
    fi
fi

ANSIBLE_PYTHON_INTERPRETER="$(command -v python)"
ansible-galaxy collection install kewlfft.aur
ansible-galaxy collection install community.general
ansible-galaxy collection install ansible.posix
ansible-playbook --ask-become-pass -i inventory main.yml