#!/bin/bash

## deploy-files will deploy data files to $DOMAIN data from given FILE/DIRECTORY/URL
##
##

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] SOURCE DBNAME"

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

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

if [ -z "$DOMAIN" ]; then
    err "You must provide a destination domain as second argument."
    exit 1
fi

. "$CHARM_PATH"/lib/common

set -e

DOCKER_SITE_PATH=/var/www/$DOMAIN
if [ "$RELATION_BASE_SERVICE" ]; then  ## In a relation, we should use it then
    DST=$DATASTORE/$RELATION_BASE_SERVICE$DOCKER_SITE_PATH
else
    DST=${SERVICE_DATASTORE}$DOCKER_SITE_PATH
fi

echo rm -rf "$DST"

mkdir -p "$DST"

deploy_files "$SRC" "$DST"

info "Deployed files from '$SRC' to '$DST'."