#!/bin/bash

## Load action gets a first argument a DIRECTORY holding the necessary files.
##
##

if [ -z "$SERVICE_DATASTORE" ]; then
    echo "This script is meant to be run through 'compose' to work properly." >&2
    exit 1
fi


usage="$exname [-h|--help] SRC_FILE DBNAME"

dbname=
source=
while [ "$1" ]; do
    case "$1" in
        "--help"|"-h")
            print_usage
            exit 0
            ;;
        --*|-*)
            err "Unexpected optional argument '$1'"
            print_usage
            exit 1
            ;;
        *)
            [ -z "$source" ] && { source=$1 ; shift ; continue ; }
            [ -z "$dbname" ] && { dbname=$1 ; shift ; continue ; }
            err "Unexpected positional argument '$1'"
            print_usage
            exit 1
        ;;
    esac
    shift
done

if [ -z "$source" ]; then
    err "You must provide a source filename name as first argument."
    print_usage
    exit 1
fi

if [ -z "$dbname" ]; then
    err "You must provide a database name as second argument."
    print_usage
    exit 1
fi

if ! [ -e "$source" ]; then
    err "File '$source' not found. Please provide an existing file as first argument."
    print_usage
    exit 1
fi

realpath=$(realpath "$source") || exit 1
dirname="$(dirname "$realpath")" || exit 1
basename=$(basename "$realpath") || exit 1
host_path="$(get_host_path "$dirname")" || {
    die "Failed to find host path for local directory: $dirname"
}


set -e

## Ensure odoo is launched
service_def=$(get_compose_service_def "$SERVICE_NAME")

## XXXvlab: should be moved to lib
CONFIG=$SERVICE_CONFIGSTORE/etc/odoo-server.conf
ADMIN_PASSWORD=$(echo "$service_def" | shyaml -q get-value options.admin-password) || {
    if [ -e "$CONFIG" ]; then
        ADMIN_PASSWORD=$(grep ^admin_passwd "$CONFIG" | sed -r 's/^admin_passwd\s+=\s+(.+)$/\1/g')
    fi
    if [ -z "$ADMIN_PASSWORD" ]; then
        err "Could not find 'admin-password' in $SERVICE_NAME service definition nor in config file."
        exit 1
    fi
}


containers="$(get_running_containers_for_service "$SERVICE_NAME")"

if [ -z "$containers" ]; then
    err "No containers running for service $DARKYELLOW$SERVICE_NAME$NORMAL."
    die "Please ensure that $DARKYELLOW$SERVICE_NAME$NORMAL is running before using '$exname'."
fi

## XXXvlab: taking first container is probably not a good idea
container="$(echo "$containers" | head -n 1)"

## XXXvlab: taking first ip is probably not a good idea
read-0 container_network container_ip < <(get_container_network_ip "$container")

DEFAULT_CURL_IMAGE=${DEFAULT_CURL_IMAGE:-docker.0k.io/curl}

debug docker run --rm --network "$container_network" \
       "-v" "$host_path:/tmp/work" \
       "$DEFAULT_CURL_IMAGE" \
       -sS \
       -X POST \
       -F "master_pwd=<hidden>" \
       -F "backup_file=@/tmp/work/${source}" \
       -F "name=${dbname}" \
    http://${container_ip}:8069/web/database/restore
docker run --rm --network "$container_network" \
       "-v" "$host_path:/tmp/work" \
       "$DEFAULT_CURL_IMAGE" \
       -sS \
       -X POST \
       -F "master_pwd=${ADMIN_PASSWORD}" \
       -F "backup_file=@/tmp/work/${basename}" \
       -F "name=${dbname}" \
    http://${container_ip}:8069/web/database/restore >/dev/null || {
    die "Querying odoo through curl was unsuccessfull."
}

info "Restored '$source' odoo database and filestore to '$dbname'."