#!/bin/sh
TYPE="docx"
# Defaults to mmd3
SCRIPT="./MultiMarkdown"

if [ ! -e $SCRIPT ]
then
    # No mmd3 on the system, use the bundled version
    DIR="$( cd "$( dirname "$0" )" && pwd )"
    SCRIPT="perl $DIR/mmd2XHTML.pl"
fi

if [ $# = 0 ]
then
    # No arguments, so use stdin/stdout
    $SCRIPT | textutil -convert $TYPE -stdin -stdout
else
    until [ "$*" = "" ]
    do
        # process each argument separately
        FILENAME=`echo "$1"| sed 's/\.[^.]*$//'`
        # gotta use 'cat' here, MMD scripts don't seem to work well in file mode
        cat "$1" | $SCRIPT | textutil -convert $TYPE -stdin -stdout > "$FILENAME.$TYPE"
        shift
    done
fi