#!/bin/bash

usage() {
    prefix="docker run --rm -it -v \$PWD:/data docker-compose-viz-mermaid"
    echo "Usage $0 [options]"
    echo -e "\t-h display this help and exit"
    echo -e "\t-v '<options>' passes some options to docker-compose-viz-mermaid"
    echo -e "\t-m pipes docker-compose-viz-mermaid outputs to mermaid"
    echo -e "\t-M '<options>' passes some options to mermaid"
    echo "Exemples"
    echo -e "\t$prefix -M -otest.png -M -w1000"
    echo -e "\t$prefix -v -p -m"
    echo -e "\t$prefix -v -p -v -fMARKDOWN"
}
cmd="java -jar /opt/docker-compose-viz-mermaid.jar"
mmdc="/home/mermaidcli/node_modules/.bin/mmdc -p /puppeteer-config.json"
pipe=false

while getopts ":hmv:M:" opt; do
    case $opt in
        h)
            usage
            exit
            ;;
        v)
            opts="$opts $OPTARG"
            ;;
        m)
            pipe=true
            ;;
        M)
            pipe=true
            mmdc_opts="$mmdc_opts $OPTARG"
            ;;
        \?)
            echo "Invalid option : -$OPTARG" >&2
            usage
            exit 1
            ;;
    esac
done
shift $((OPTIND-1))

if $pipe; then
    $cmd $opts | $mmdc $mmdc_opts
else
    $cmd $opts
fi
