#!/bin/bash -

. /etc/os-release
MAJOR_VERSION=`sed -E 's/([0-9]+).*/\1/g' <<< "$VERSION_ID"`

DISTRO_FAMILY="el"
if [[ $ID_LIKE == *"suse"* ]]
then
	DISTRO_FAMILY="suse"
elif [[ $ID == "debian" || $ID_LIKE == *"debian"* ]]
then
  DISTRO_FAMILY="deb"
  MAJOR_VERSION=""
fi

repo=${SBR_REPO:-"http://repo.storware.eu/storware/current/${DISTRO_FAMILY}${MAJOR_VERSION}"}
node_name=${SBR_NODE_NAME:-"node1"}
install_server=${SBR_INSTALL_SERVER:-"y"}
install_node=${SBR_INSTALL_NODE:-"y"}
admin_user=${SBR_ADMIN_USER:-"admin"}
mariadb_add_repo=${SBR_MARIADB_ADD_REPO:-"true"}

export LC_CTYPE="C.UTF-8"
export LC_ALL="C.UTF-8"

read_pass()
{
	pass=$3
	while [[ -z "$pass" ]]
	do
		printf "\nPlease provide password for the $2 account: "
		IFS= read -rs pass
		echo

		if [[ -z "$pass" ]]
		then
			echo "Password cannot be empty!"
			continue
		fi

		printf "Repeat password: "
		IFS= read -rs pass2
		echo

		if [[ "$pass" != "$pass2" ]]
		then
			echo "Passwords don't match!"
			pass=""
		fi
	done
	qpass=$(printf "%q" "$pass")
	eval $1=$qpass
}

read_pass admin_pass "Server '$admin_user' user" "$SBR_ADMIN_PASS"

if [[ "$install_server" == "y" ]]
then
    read_pass db_pass "database root" "$SBR_DB_PASS"
fi

rm -Rf sbr
mkdir -p sbr
pushd sbr

if [[ $ID == "rhel" ]]
then
	if [[ "$MAJOR_VERSION" -eq 9 ]]
	then
		subscription-manager repos --enable ansible-automation-platform-2.4-for-rhel-9-x86_64-rpms
	elif [[ "$MAJOR_VERSION" -eq 8 ]]
	then
		subscription-manager repos --enable ansible-2.9-for-rhel-8-x86_64-rpms
	else
		subscription-manager repos --enable rhel-7-server-ansible-2.9-rpms
		subscription-manager repos --enable rhel-7-server-optional-rpms
	fi
elif [[ $ID == "centos" && $MAJOR_VERSION -eq 7 ]]
then
	yum -y install epel-release
elif [[ $ID == "sles" ]]
then
	SUSEConnect -p PackageHub/$VERSION_ID/x86_64
	SUSEConnect -p sle-module-desktop-applications/$VERSION_ID/x86_64
	SUSEConnect -p sle-module-development-tools/$VERSION_ID/x86_64
	SUSEConnect -p sle-module-public-cloud/$VERSION_ID/x86_64
fi

if [[ $ID_LIKE == *"fedora"* ]]
then
	if [[ "$MAJOR_VERSION" -eq 7 ]]
	then
		yum -y install ansible
	else
		dnf -y install ansible-core || yum -y install ansible
	fi
elif [[ $ID_LIKE == *"suse"* ]]
then
	zypper -n install ansible
elif [[ $ID == "debian" || $ID_LIKE == *"debian"* ]]
then
	apt -y install python3-venv && python3 -m venv venv && source venv/bin/activate && pip install --no-input ansible
fi

ANSIBLE_VERSION=`ansible --version | head -1 | sed -E 's/.*[^0-9.]([0-9.]+).*/\1/g'`
ANSIBLE_VERSION_MIN=`printf "2.10\n${ANSIBLE_VERSION}\n" | sort -V | head -1`

ansible-galaxy install --force xe0nic.ansible_vprotect_server
ansible-galaxy install --force xe0nic.ansible_vprotect_node

if [[ "$ANSIBLE_VERSION_MIN" == "2.10" ]]
then
    ansible-galaxy install -r  ~/.ansible/roles/xe0nic.ansible_vprotect_server/meta/collections.yml
    ansible-galaxy install -r  ~/.ansible/roles/xe0nic.ansible_vprotect_node/meta/collections.yml
fi

if [[ -n "$SBR_SERVER_FQDN" ]]
then
    server_fqdn_assignment="server_fqdn=$SBR_SERVER_FQDN"
fi

base64_prefix="[BASE64]"
q_admin_pass=$(printf '%s' "$admin_pass")
q_db_pass=$(printf '%s' "${base64_prefix}${db_pass}" | base64)

cat << EOF > hosts
[all:vars]
ansible_user=root
ansible_connection=local
vprotect_repo=$repo
admin_user=$admin_user
admin_pass=$q_admin_pass
db_pass=$q_db_pass
mariadb_add_repo=$mariadb_add_repo
$server_fqdn_assignment

[server]
localhost

[nodes]
localhost node_name=$node_name
EOF

cat << EOF > site.yml
---

EOF

if [[ "$install_server" == "y" ]]
then
    cat << EOF >> site.yml
- hosts: server
  roles:
    - xe0nic.ansible_vprotect_server

EOF
fi

if [[ "$install_node" == "y" ]]
then
    cat << EOF >> site.yml
- hosts: nodes
  roles:
    - xe0nic.ansible_vprotect_node

EOF
fi

ansible-playbook -i hosts site.yml
result=$?
popd
rm -Rf sbr

if [[ $result -eq 0 ]]
then
	echo "DONE! Try to log into your Storware Backup & Recovery Server console now."
fi

