#!/bin/bash

cd /root

MONGOSH_BIN="/usr/local/jetapps/usr/bin/mongosh"
GREEN='\033[0;32m'
RED='\033[0;31m'
NC='\033[0m'

[[ ! -f /etc/os-release ]] && echo "Can't find /etc/os-release file" && exit 1

echo "Installing jetmongod-remote package..."

source /etc/os-release

if [[ -x "$(command -v yum)" || -x "$(command -v dnf)" ]]; then
	yum clean all --disablerepo=* --enablerepo=jetapps >/dev/null
	yum -y -q install jetmongod-remote --disablerepo=* --enablerepo=jetapps >/dev/null
elif [[ -x "$(command -v apt-get)" ]]; then
	apt update >/dev/null
	apt install -y jetmongod-remote >/dev/null
else 
        echo "[ERROR] Failed fetching package manager." >&2
        exit 1
fi

if [[ ! -f "$MONGOSH_BIN" ]]; then
        echo "[ERROR] Failed install jetmongod-remote package." >&2
        exit 1
fi

echo "Select Environment:"
echo "1) Standard (Local/Remote)"
echo "2) MongoDB Atlas (SRV)"
echo "3) AWS DocumentDB / Custom TLS"
read -e -p "Choice [1-3]: " ENV_TYPE
read -e -p "Enter MongoDB database username: " DB_USER
read -e -s -p "Enter MongoDB database Password: " DB_PASS
echo ""
read -e -p "Enter MongoDB authentication database: " AUTH_DB
read -e -p "Enter MongoDB database name: " DB_NAME
read -e -p "Enter MongoDB server host: " DB_HOST

CUSTOM_FLAGS=""
if [ "$ENV_TYPE" == "3" ]; then
    read -e -p "Path to CA File (optional, leave blank if none): " CA_FILE
    if [ ! -z "$CA_FILE" ]; then CUSTOM_FLAGS="--tls --tlsCAFile $CA_FILE"; fi
fi

if [ "$ENV_TYPE" == "2" ]; then
	PROTOCOL="mongodb+srv://"
    URI="$PROTOCOL$DB_USER:$DB_PASS@$DB_HOST/?authSource=$AUTH_DB"
else
    read -e -p "Enter MongoDB server port: " DB_PORT
	PROTOCOL="mongodb://"
    URI="$PROTOCOL$DB_USER:$DB_PASS@$DB_HOST:$DB_PORT/?authSource=$AUTH_DB"
fi

echo -e "\nTesting Connection..."

CHECK_SCRIPT="
  const ping = db.runCommand({ping:1});
  const access = db.getSiblingDB('$DB_NAME').getCollectionNames();
  if(ping.ok === 1) { print('PING_OK'); }
  if(Array.isArray(access)) { print('ACCESS_OK'); }
"

RESULT=$($MONGOSH_BIN "$URI" $CUSTOM_FLAGS --quiet --eval "$CHECK_SCRIPT" 2>&1)

if [[ $RESULT != *"PING_OK"* ]]; then
    echo -e "[ERROR] ${RED}Connection Failed${NC}" >&2
    echo "$RESULT" >&2
    exit 1
fi

if [[ $RESULT != *"ACCESS_OK"* ]]; then
    echo -e "[ERROR] ${RED}Permission Denied${NC}" >&2
    echo "The user is authenticated, but does not have 'find' permissions on database: '$DB_NAME'" >&2
    exit 1
fi

INSTALLATION_HOSTNAME=$($MONGOSH_BIN "$URI" $CUSTOM_FLAGS --quiet --eval "db.getSiblingDB('$DB_NAME').getCollection('config').findOne({_id:'general'})?.hostname || ''" 2>&1)

if [[ "$INSTALLATION_HOSTNAME" && "$INSTALLATION_HOSTNAME" != `hostname` ]]; then
	echo -e "[ERROR] ${RED}Database Namespace Conflict${NC}" >&2
	echo "The specified database is already linked to another server instance: '$INSTALLATION_HOSTNAME'." >&2
	echo "Cross-installation database sharing is not supported." >&2
	echo "Solution: Please provide a fresh database or clear the existing one before proceeding." >&2
	exit 1
fi

cat <<EOF | sudo tee /usr/local/jetapps/etc/.mongod.auth >/dev/null
HOST=$DB_HOST
PORT=$DB_PORT
USER=$DB_USER
PASS=$DB_PASS
DB=$DB_NAME
AUTHDB=$AUTH_DB
PROTOCOL=$PROTOCOL
CA=$CA_FILE
URI=$URI
EOF

echo -e "${GREEN}Database configuration successful!${NC}"
echo "You can now proceed with the application installation; it will use the authenticated database details provided."
exit 0

