#!/bin/sh

# Copyright (C) 2019-2020 Andrey Cherepanov <cas@altlinux.org>
# This file is part of deploy <https://altlinux.org/Deploy>.
#
# deploy is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# deploy is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with deploy. If not, see <http://www.gnu.org/licenses/>.

modules_dir="/usr/share/deploy"
VERBOSE="$VERBOSE"
RPM="rpm"

usage()
{
	parameters_msg="Show available parameters for specified role"
	echo "Usage: deploy [-d] <role> [<parameters>]  Deploy specified role"
	echo "       deploy -r <role>  Remove specified role"
	echo "       deploy --parameters <role>  $parameters_msg"
	echo "       deploy --help     This help"
	echo "       deploy --version  This help"
	echo "If <role> is omitted program shows all available roles."
	exit 0
}

version()
{
	$RPM -q deploy --qf '%{version}\n'
	exit 0
}

list_roles()
{
	if [ ! -d "$modules_dir" ]; then
		echo "There is no roles directory ($modules_dir). Check installation."
		exit 1
	fi
	cd "$modules_dir"
	/bin/ls -1 *.yml 2>/dev/null | sed 's|\.yml$||'
	exit 0
}

check_role()
{
	role="$1"
	if [ ! -s "$modules_dir/$role.yml" ]; then
		echo "Unable to find role $role" >&2
		exit 1
	fi

}

list_parameters()
{
	role="$1"
	check_role "$role"
	defaults_file="$modules_dir/$role/defaults/main.yml"
	[ -s "$defaults_file" ] || exit 0
	sed -nE 's/^(\w+):\s*(.*)/\1=\2/p' "$defaults_file"
	exit 0
}

# Run specified module
deploy_role()
{
	target="$1"
	shift

	cd "$modules_dir"
	check_role "$target"
	echo "Deploying $target..."
	if [ -n "$*" ]; then
		# Pass varialbles
		ansible-playbook $VERBOSE "$target.yml" --extra-vars "$*"
	else
		ansible-playbook $VERBOSE "$target.yml"
	fi
	ret="$?"
	if [ "$ret" -eq 0 ]; then
		echo "Deploy complete successful."
		exit 0
	else
		echo "Error deploy $target"
		exit $ret
	fi
}

# Remove specified role
remove_role()
{
	target="$1"
	shift

	cd "$modules_dir"
	check_role "$target-remove"
	echo "Removing $target..."
	if [ -n "$*" ]; then
		# Pass varialbles
		ansible-playbook $VERBOSE "$target-remove.yml" --extra-vars "$*"
	else
		ansible-playbook $VERBOSE "$target-remove.yml"
	fi
	ret="$?"
	if [ "$ret" -eq 0 ]; then
		echo "Remove complete successful."
		exit 0
	else
		echo "Error remove $target"
		exit $ret
	fi
}

command="$1"

# Process default parameters
test "$command" = "-h" -o "$command" = "--help" && usage
test "$command" = "--version" && version
if [ "$command" = "--parameters" ]; then
	role="${2-}"
	[ -n "$role" ] || usage
	list_parameters "$role"
fi
test -z "$command" && list_roles
if [ "$command" = "-d" ]; then
	VERBOSE="-v"
	shift
	command="$1"
fi

# Check for superuser privileges
if [ "$EUID" -ne 0 ]; then
	echo "Insufficient privileges. Please run as root."
	exit 1
fi

if [ "$command" = "-r" ]; then
    shift
    remove_role "$@"
else
    deploy_role "$@"
fi

# vi: noexpandtab
