#!/bin/bash

cd "$(dirname "${BASH_SOURCE[0]}")" || exit

sleep 5

## ==========================
# Load Env File
## ==========================
env_file="${1:-.env}"

if [ -f $env_file ]; then
    source $env_file
else
    echo ".env file not found! Please provide a .env file path or add said file to the working direcotry to continue."
    exit 1
fi

## ==========================
# Setup first root function
## ==========================
query_db_with_main_root() {
    local query="$1"
    local result=$(mariadb -u root -p$MARIADB_ROOT_PASSWORD -e "$query")

    if [[ $? -ne 0 ]]; then
        echo "Error executing the query."
        return 2
    fi

    local row_count=$(echo "$result" | wc -l)
    if ((row_count > 1)); then
        return 0
    else
        return 1
    fi
}

## ==========================
# Mariadb Query Function
## ==========================
query_db() {
    local query="$1"
    local result=$(mariadb -u root -h $DSQL_DB_HOST -p$DSQL_DB_PASSWORD -e "$query")

    if [[ $? -ne 0 ]]; then
        echo "Error executing the query."
        return 2
    fi

    local row_count=$(echo "$result" | wc -l)
    if ((row_count > 1)); then
        return 0
    else
        return 1
    fi
}

# SQL=$(sed "s/\${\([^}]*\)}/\${\1}/g" "test.sql" | envsubst)

# query_db "$SQL"

## ==========================
# Check for required ENV Variables
## ==========================
if [ -z "${DSQL_DB_TARGET_IP_ADDRESS}" ]; then
    echo "DSQL_DB_TARGET_IP_ADDRESS env is not set"
    exit 1
fi

if [ -z "${DSQL_DB_PASSWORD}" ]; then
    echo "DSQL_DB_PASSWORD env is not set"
    exit 1
fi

if [ -z "${DSQL_DB_READ_ONLY_PASSWORD}" ]; then
    echo "DSQL_DB_READ_ONLY_PASSWORD env is not set"
    exit 1
fi

if [ -z "${DSQL_DB_FULL_ACCESS_PASSWORD}" ]; then
    echo "DSQL_DB_FULL_ACCESS_PASSWORD env is not set"
    exit 1
fi

## ==========================
# Create Datasquirel Root User
## ==========================
check_root_user_exists=$(query_db_with_main_root "select user,host from mysql.user where user = 'root' and host = '$DSQL_DB_TARGET_IP_ADDRESS';")

if [[ $? == 0 ]]; then
    echo "Root User Exists"
else
    echo "No Root User. Creating ..."
    query_db_with_main_root "CREATE USER IF NOT EXISTS 'root'@'$DSQL_DB_TARGET_IP_ADDRESS' IDENTIFIED BY '$DSQL_DB_PASSWORD';"
    query_db_with_main_root "GRANT ALL PRIVILEGES ON *.* TO 'root'@'$DSQL_DB_TARGET_IP_ADDRESS' WITH GRANT OPTION;"
    query_db_with_main_root "FLUSH PRIVILEGES;"
fi

if [ $? -ne 0 ]; then
    echo "Error: Couldn't create root user."
    exit 1
fi

## ==========================
# Create Datasquirel Database
## ==========================
check_database_exists=$(query_db_with_main_root "show databases like 'datasquirel';")

if [[ $? == 2 ]]; then
    echo "DSQL Root user setup failed. Please check root user for host $DSQL_DB_TARGET_IP_ADDRESS"
    exit 1
fi

if [[ $? == 0 ]]; then
    echo "Dsql Database exists"
else
    echo "Dsql Database does not exist. Creating ..."
    query_db_with_main_root "create database datasquirel;"
fi

## ==========================
# Create Datasquirel Read Only User
## ==========================
check_read_only_user_exists=$(query_db_with_main_root "select user,host from mysql.user where user = '$DSQL_DB_READ_ONLY_USERNAME' and host = '$DSQL_DB_TARGET_IP_ADDRESS';")

if [[ $? == 0 ]]; then
    echo "$DSQL_DB_READ_ONLY_USERNAME User Exists"
else
    echo "No $DSQL_DB_READ_ONLY_USERNAME User. Creating ..."
    query_db_with_main_root "CREATE USER IF NOT EXISTS '$DSQL_DB_READ_ONLY_USERNAME'@'$DSQL_DB_TARGET_IP_ADDRESS' IDENTIFIED BY '$DSQL_DB_READ_ONLY_PASSWORD' REQUIRE SSL;"
fi

## ==========================
# Create Datasquirel Full Access User
## ==========================
DSQL_DB_FULL_ACCESS_USERNAME="datasquirel_full_access"
check_full_access_user_exists=$(query_db_with_main_root "select user,host from mysql.user where user = '$DSQL_DB_FULL_ACCESS_USERNAME' and host = '$DSQL_DB_TARGET_IP_ADDRESS';")

if [[ $? == 0 ]]; then
    echo "$DSQL_DB_FULL_ACCESS_USERNAME User Exists"
else
    echo "No $DSQL_DB_FULL_ACCESS_USERNAME User. Creating ..."
    query_db_with_main_root "CREATE USER IF NOT EXISTS '$DSQL_DB_FULL_ACCESS_USERNAME'@'$DSQL_DB_TARGET_IP_ADDRESS' IDENTIFIED BY '$DSQL_DB_FULL_ACCESS_PASSWORD' REQUIRE SSL;"
fi

query_db_with_main_root "FLUSH PRIVILEGES;"

exit 0