#!/usr/bin/env bash
#
# Copyright (c) 2024 YunoHost Contributors
#
# This file is part of YunoHost (see https://yunohost.org)
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program 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 Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#

# Run SQL instructions in a database ($db_name by default)
#
# usage: ynh_mysql_db_shell [database] <<< "instructions"
# | arg: database=    - the database to connect to (by default, $db_name)
#
# examples:
#    ynh_mysql_db_shell $db_name <<< "UPDATE ...;"
#    ynh_mysql_db_shell < /path/to/file.sql
#
ynh_mysql_db_shell() {
    local database=${1:-$db_name}

    local default_character_set=()
    if [[ -n "$database" ]]; then
        default_character_set=(
            --default-character-set
            "$(mysql -B "$database" <<< 'show variables like "character_set_database";' | tail -n1 | cut -f2)"
        )
    fi

    mysql "${default_character_set[@]}" -B "$database"
}

# Create a database and grant optionnaly privilegies to a user
#
# [internal] ... handled by the core / "database resource"
#
# usage: ynh_mysql_create_db db [user [pwd]]
# | arg: db - the database name to create
# | arg: user - the user to grant privilegies
# | arg: pwd - the password to identify user by
#
ynh_mysql_create_db() {
    local db=$1

    local sql="CREATE DATABASE ${db};"

    # grant all privilegies to user
    if [[ $# -gt 1 ]]; then
        sql+=" GRANT ALL PRIVILEGES ON ${db}.* TO '${2}'@'localhost'"
        if [[ -n ${3:-} ]]; then
            sql+=" IDENTIFIED BY '${3}'"
        fi
        sql+=" WITH GRANT OPTION;"
    fi

    mysql -B <<< "$sql"
}

# Drop a database
#
# [internal] ... handled by the core / "database resource"
#
# If you intend to drop the database *and* the associated user,
# consider using ynh_mysql_remove_db instead.
#
# usage: ynh_mysql_drop_db db
# | arg: db - the database name to drop
#
ynh_mysql_drop_db() {
    mysql -B <<< "DROP DATABASE ${1};"
}

# Dump a database
#
# usage: ynh_mysql_dump_db database
# | arg: database   - the database name to dump (by default, $db_name)
# | ret: The mysqldump output
#
# example: ynh_mysql_dump_db "roundcube" > ./dump.sql
#
ynh_mysql_dump_db() {
    local database=${1:-$db_name}

    local default_character_set=()
    if [[ -n "$database" ]]; then
        default_character_set=(
            --default-character-set
            "$(mysql -B "$database" <<< 'show variables like "character_set_database";' | tail -n1 | cut -f2)"
        )
    fi

    mysqldump "${default_character_set[@]}" --single-transaction --skip-dump-date --routines "$database"
}

# Create a user
#
# [internal] ... handled by the core / "database resource"
#
# usage: ynh_mysql_create_user user pwd [host]
# | arg: user - the user name to create
# | arg: pwd - the password to identify user by
#
ynh_mysql_create_user() {
    mysql -B <<< "CREATE USER '${1}'@'localhost' IDENTIFIED BY '${2}';"
}

# Check if a mysql user exists
#
# [internal]
#
# usage: ynh_mysql_user_exists user
# | arg: user   - the user for which to check existence
# | ret: 0 if the user exists, 1 otherwise.
ynh_mysql_user_exists() {
    local user=$1
    [[ -n "$(mysql -B <<< "SELECT User from mysql.user WHERE User = '$user';")" ]]
}

# Check if a mysql database exists
#
# [internal]
#
# usage: ynh_mysql_database_exists database
# | arg: database    - the database for which to check existence
# | exit: Return 1 if the database doesn't exist, 0 otherwise
#
ynh_mysql_database_exists() {
    local database=$1
    mysqlshow | grep -q "^| $database "
}

# Drop a user
#
# [internal] ... handled by the core / "database resource"
#
# usage: ynh_mysql_drop_user user
# | arg: user - the user name to drop
#
ynh_mysql_drop_user() {
    mysql -B <<< "DROP USER '${1}'@'localhost';"
}
