#!/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] DBNAME [MODULE ...]"

dbname=
modules=()
while [ "$1" ]; do
    case "$1" in
        "--help"|"-h")
            print_usage
            exit 0
        ;;
        *)
            [ -z "$dbname" ] && { dbname=$1 ; shift ; continue ; }
            modules+=("$1")
        ;;
    esac
    shift
done

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

if [ -z "${modules[*]}" ]; then
    err "You must provide at least one module as third argument."
    print_usage
    exit 1
fi

modules="$(echo "${modules[@]}" | tr " " ",")"

## This can work only if ~/.my.cnf is correctly created by init.

set -e

launch_docker_compose run "$CONTAINER_NAME" --init="$modules" -d "$dbname" --stop-after-init

info "Installed '$modules' module(s) into database '$dbname'."