#!/usr/bin/env bash

# Small wrapper script around the c++ executable that dumps the data and some
# potential post-processing that is easier with python

set -euo pipefail

THIS_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd -P)"
if ! ls "${THIS_DIR}"/podio-dump-tool > /dev/null 2>&1; then
    echo "Could not find podio-dump-tool executable (has it been installed?)" >&2
    exit 1
fi

ALL_ARGS=("$@")
DUMP_MODEL=0

while [[ "$#" -gt 0 ]]; do
    case $1 in
        --dump-edm*)
            DUMP_MODEL=1
            break
            ;;
        *)
            shift
            ;;
    esac
done

if [ ${DUMP_MODEL} = 1 ]; then
    if ! ls "${THIS_DIR}"/json-to-yaml > /dev/null 2>&1; then
        echo "Could not find the json-to-yaml executable (has it been instaled?)" >&2
        exit 1
    fi
    "${THIS_DIR}"/podio-dump-tool "${ALL_ARGS[@]}" | "${THIS_DIR}"/json-to-yaml
else
    "${THIS_DIR}"/podio-dump-tool "${ALL_ARGS[@]}"
fi
