YML_CONFIG_PATH=/conf.yml
LOCAL_YML_CONFIG_PATH="$SERVICE_CONFIGSTORE$YML_CONFIG_PATH"
HOST_YML_CONFIG_PATH="$HOST_CONFIGSTORE/$SERVICE_NAME$YML_CONFIG_PATH"


CONFIG_PATH=/setup.ts
LOCAL_CONFIG_PATH="$SERVICE_CONFIGSTORE$CONFIG_PATH"
HOST_CONFIG_PATH="$HOST_CONFIGSTORE/$SERVICE_NAME$CONFIG_PATH"

DEST_PATH=/var/www/cyclos-ui
LOCAL_DEST_PATH="$SERVICE_DATASTORE$DEST_PATH"
HOST_DEST_PATH="$HOST_DATASTORE/$SERVICE_NAME$DEST_PATH"


cyclos_ui.build_builder_image() {
    local last_line
    if out=$(cd "$CHARM_PATH/misc/builder" && docker build . 2>&1); then
        last_line="${out##*$'\n'}"
        if [[ "${last_line}" =~ ^"Successfully built "[a-f0-9]+$ ]]; then
            echo "${last_line##* }"
            return 0
        else
            err "Couldn't find image id:"
        fi
    else
        err "Failed to build the builder image:"
    fi
    e "$out" | prefix " ${DARKGRAY}|${NORMAL} " >&2
    return 1
}


cyclos_ui.make_base_config_file() {
    local cfg_file_content builder_image_id
    # builder_image_id=$(cyclos_ui.build_builder_image) || return 1
    # cfg_file_content=$(docker run --rm "$builder_image_id" cat src/app/setup.ts) || {
    #     err "Couldn't access 'src/app/setup.ts' in given image."
    #     return 1
    # }
    service_def=$(get_compose_service_def "$SERVICE_NAME") || return 1
    cfg_file_content=$(echo "$service_def" | shyaml -y get-value "options" 2>/dev/null) || true
    mkdir -p "${LOCAL_YML_CONFIG_PATH%/*}" &&
    e "$cfg_file_content" > "$LOCAL_YML_CONFIG_PATH"
}


cyclos_ui.generate_website() {
    local builder_image_id hash_file="$LOCAL_DEST_PATH/.hash"
    hash=$(hash_get < "$LOCAL_YML_CONFIG_PATH")

    if [ -f "${hash_file}" ]; then
        if [[ "$(cat "${hash_file}")" == "$hash" ]]; then
            return 0
        else
            [ -d "$LOCAL_DEST_PATH" ] && echo rm -rf "${LOCAL_DEST_PATH}"
        fi
    fi
    builder_image_id=$(cyclos_ui.build_builder_image) || return 1
    cfg_file_content=$(docker run --rm "$builder_image_id" cat src/app/setup.ts) || {
        err "Couldn't access 'src/app/setup.ts' in given image."
        return 1
    }
    ## remove last '}'
    cfg_file_content="${cfg_file_content%\}*}"
    while read-0 k v; do
        json=$(e "$v" | yaml2json) || {
            err "Failed to correct yaml to json"
            return 1
        }
        cfg_file_content+="  Configuration.$(e "$k" | shyaml get-value) = $json;"$'\n'""
    done < <(cat "$LOCAL_YML_CONFIG_PATH" | shyaml -y key-values-0)
    cfg_file_content+="}"
    mkdir -p "${LOCAL_CONFIG_PATH%/*}" &&
        e "$cfg_file_content" > "$LOCAL_CONFIG_PATH"
    docker run --rm \
           -v "$HOST_CONFIG_PATH:/opt/apps/cyclos-ui/src/app/setup.ts:ro" \
           -v "$HOST_DEST_PATH:/opt/apps/cyclos-ui/dist:rw" \
           "${builder_image_id}" \
           npm run build  >&2 || {
        err "Failed to build new sources."
        return 1
    }
    e "$hash" > "$hash_file"
}


cyclos_ui.conf_add() {
    local yaml="$1"
    prev=$([ -f "$LOCAL_YML_CONFIG_PATH" ] && cat "$LOCAL_YML_CONFIG_PATH")
    if ! out=$(merge_yaml_str "$prev" "$yaml"); then
        err "Couldn't merge new configuration."
        exit 1
    fi
    mkdir -p "${LOCAL_CONFIG_PATH%/*}" &&
    e "$out" > "$LOCAL_YML_CONFIG_PATH"
}