#!/bin/bash
#version_1.0
echo "Checking MC..."
if command -v mc &> /dev/null; then
    echo "MC installed."
else
    echo "Installing MC..."

    wget https://dl.min.io/client/mc/release/linux-amd64/mc

    chmod +x mc

    sudo mv mc /usr/local/bin/
    echo "MC installed."
fi

if [ "$#" -ne 1 ]; then
    echo "Usage: $0 {dump|restore}"
    exit 1
fi

case "$1" in
    dump)
    #minio
    access=""
    while [[ -z "$access" ]]; do
        read -p "Enter minio access key: " access
        if [[ -z "$access" ]]; then
            echo "Error: value cannot be empty. Please try again."
        fi
    done

    secret=""
    while true; do
        read -s -p "Enter minio secret key: " secret
        echo
        if [[ -z "$secret" ]]; then
            echo "Error: value cannot be empty. Please try again."
        else
            break
        fi
    done

    ip=""
    while [[ -z "$ip" ]]; do
        read -p "Enter minio ip or domain(Example: elph-chat-server.loc): " ip
        if [[ -z "$ip" ]]; then
            echo "Error: value cannot be empty. Please try again."
        fi
    done


    #mongo
    mongo_container=$(docker ps --format '{{.Names}}'| grep 'mongo-1$')

    #start backup
    date=$(date '+%d.%m.%Y.%H:%M')

    default_backup_dir="/tmp/elph-chat-backups/$date/"
    backup_dir=""
    read -p "Enter backup directory or press enter to use default (Default: $default_backup_dir): "
    if [[ -z "$backup_dir" ]]; then
        backup_dir="$default_backup_dir"
    fi
    mkdir -p "$backup_dir"mongo
    mkdir -p "$backup_dir"minio
    

    echo "Starting minio backup..."

    mc alias set myminio http://$ip:9000 $access $secret

    mc cp --recursive myminio/elph-chat "$backup_dir"/minio
    mc cp --recursive myminio/temp-files "$backup_dir"/minio

    echo "✅ Minio backup ended. Check files in $backup_dir"

    echo "Starting mongo backup..."

    docker exec "$mongo_container" mongodump --archive > "$backup_dir"mongo/mongodump.gz

        ;;
    restore)
    #minio
    access=""
    while [[ -z "$access" ]]; do
        read -p "Enter minio access key: " access
        if [[ -z "$access" ]]; then
            echo "Error: value cannot be empty. Please try again."
        fi
    done

    secret=""
    while true; do
        read -s -p "Enter minio secret key: " secret
        echo
        if [[ -z "$secret" ]]; then
            echo "Error: value cannot be empty. Please try again."
        else
            break
        fi
    done
    
    ip=""
    while [[ -z "$ip" ]]; do
        read -p "Enter minio ip or domain(Example: elph-chat-server.loc): " ip
        if [[ -z "$ip" ]]; then
            echo "Error: value cannot be empty. Please try again."
        fi
    done


    #mongo
    mongo_container=$(docker ps --format '{{.Names}}'| grep 'mongo-1$')


    _complete_dir() {
        local dir
        dir="${COMP_WORDS[COMP_CWORD]}"
        COMPREPLY=($(compgen -d -- "$dir"))
    }

    complete -F _complete_dir elph-chat-backup.sh

    read -e -p "Enter backup directory: " dir_path

    if [ -d "$dir_path" ]; then
        if [ -d "$dir_path/minio" ]; then
        echo "Starting minio restore..."
        mc alias set myminio http://$ip:9000 $access $secret
        mc cp --recursive "$dir_path/minio/elph-chat/rooms" myminio/elph-chat/
        mc cp --recursive "$dir_path/minio/elph-chat/users" myminio/elph-chat/
        mc cp --recursive  "$dir_path/minio/temp-files" myminio/temp-files
        echo "✅ Minio restore ended ended"
        else
            echo "Backup directory not contain /minio"
        fi

        if [ -d "$dir_path/mongo" ]; then
        echo "Starting mongo restore..."
        docker exec -i "$mongo_container" sh -c 'mongorestore --archive --db elph --drop' < "$dir_path"/mongo/mongodump.gz
        else
            echo "Backup directory not contain /mongo"
        fi
    else
        echo "Error. Enter backup directory."
    fi

        ;;
    *)
        echo "Unknown command: $1"
        echo "Usage: $0 {dump|restore}"
        exit 1
        ;;
esac

