#!/bin/bash

set -eu

# TODO(miku): following two checks are not the right way.

# first, check if tasks exists
OUTPUT=$(taskoutput "$@")

# then extract file name
OUTPUT=$(taskoutput "$@"|tail -1)

if [[ ! -f "$OUTPUT" ]]; then
    read -p "Output does not exists. Should we try to run this task now [y/N]? " -r
    if [[ $REPLY =~ ^[Yy]$ ]]
    then
        taskdo "$@"
    else
        exit 1
    fi
fi

if [[ "$OUTPUT" == *mrc ]]; then
    yaz-marcdump $OUTPUT
else
    cat $OUTPUT
fi
