#!/bin/sh
#
# Copyright (c) 2014, Juniper Networks, Inc.
# All rights reserved.
# This SOFTWARE is licensed under the LICENSE provided in the
# ../Copyright file. By downloading, installing, copying, or otherwise
# using the SOFTWARE, you agree to be bound by the terms of that
# LICENSE.
# Phil Shafer, July 2014
#

VERSION=2.1.0
LOCAL=/usr/share/libxo
WEB=http://juniper.github.io/libxo/${VERSION}/xohtml
MODE=internal
BASE=
CMD=cat
DONE=
KEEP=
FILE=

do_help () {
    echo "xohtml: wrap libxo-enabled output in HTML"
    echo "Usage: xohtml [options] [command [arguments]]"
    echo "Valid options are:"
    echo "    -b --base <basepath>        Use a different base location for js/css files"
    echo "    -c | --command <command>    Execute the given command"
    echo "    -f | --file <output-file>   Write all output to the given file "
    echo "    -i | --internal             js/css files are written directly into the output"
    echo "    -k | --keep                 Keep the output file (no auto-delete)"
    echo "    -l | --local                js/css files use a local path reference"
    echo "    -m | --minimal              Pass minimal libxo options to the command"
    echo "    -w | --web                  js/css files use a github URL reference"
    exit 1
}

XOPTS=html,xpath,info

while [ -z "$DONE" -a ! -z "$1" ]; do
    case "$1" in
        -b|--base)
            shift;
            MODE=ref;
            BASE="$1";
	    shift;
            ;;
        -c|--command)
            shift;
            CMD="$1";
	    shift;
            ;;
        -f|--file)
            shift;
            FILE="$1";
	    shift;
            ;;
        -i|--internal)
            shift;
            MODE=internal;
            ;;
        -k|--keep)
            shift;
            KEEP=1;
            ;;
        -l|--local)
            shift;
            MODE=ref;
            BASE="${LOCAL}";
            ;;
        -m|--minimal)
            shift;
            XOPTS=html
            ;;
        -w|--web)
            shift;
            MODE=ref;
            BASE="${WEB}";
            ;;

	-*)
	    do_help
	    ;;
	*)
	    DONE=1;
	    XX=$1;
	    shift;
	    CMD="$XX --libxo=$XOPTS $@"
	    ;;
    esac
done

if [ "$CMD" = "cat" -a -t 0 ]; then
    do_help
fi

# Default output goes to an auto-named file under /tmp, which we then
# open in a browser.  "-f" opts out of both: the caller gets exactly
# the file they asked for and nothing gets opened for them.
OPEN=
if [ -z "$FILE" ]; then
    DIR="/tmp/${USER}.xohtml"
    mkdir -p "$DIR"

    # Sweep old plain runs.  Files saved with -k/--keep use a
    # different prefix (xohtml-keep.*) and are never touched here.
    rm -f "$DIR"/xohtml.*.html

    TS=`/bin/date "+%y%m%d%H%M.%S"`
    if [ -n "$KEEP" ]; then
        FILE="$DIR/xohtml-keep.${TS}$$.html"
    else
        FILE="$DIR/xohtml.${TS}$$.html"
    fi
    OPEN=1
fi

exec > "$FILE"

esc () {
    printf '%s' "$1" | sed -e 's/&/\&amp;/g' -e 's/</\&lt;/g' -e 's/>/\&gt;/g'
}

echo '<html>'
echo '<head>'
echo '<meta http-equiv="content-type" content="text/html; charset=utf-8"/>'
if [ "$MODE" = internal ]; then
    echo '<style>'
    cat "${LOCAL}/xohtml.css"
    echo '</style>'
    echo '<script>'
    cat "${LOCAL}/xohtml.js"
    echo '</script>'
else
    echo '<link rel="stylesheet" href="'$BASE'/xohtml.css">'
    echo '<script src="'$BASE'/xohtml.js"></script>'
fi
echo '</head>'
echo '<body>'
echo '<div id="xohtml-header">'
echo '<span class="xohtml-cmd">'"$(esc "$CMD")"'</span>'
echo '<span class="xohtml-time">'"$(/bin/date)"'</span>'
echo '</div>'
echo '<div id="xohtml-content">'

$CMD

echo '</div>'
echo '</body>'
echo '</html>'

exec 1>&2

if [ -n "$OPEN" ]; then
    if command -v open >/dev/null 2>&1; then
        open "$FILE"
    elif command -v xdg-open >/dev/null 2>&1; then
        xdg-open "$FILE"
    else
        echo "xohtml: output written to $FILE"
    fi
fi

exit 0
