#!/bin/bash
set -e
if [ -n "$DEBUG" ]; then
    set -x
fi

# The release name we are configured to run under.
typeset dmathicsdoc_version='1.0.0'

# Allow customization using POSIX environment variables:
DOCKER=${DOCKER:-docker}

TAG=${TAG:-latest}
MATHICS_IMAGE=${MATHICS_IMAGE:-mathicsorg/mathics:${TAG}}

for arg in "$@" ; do
    case "$arg" in
        -h | --help | help )
			cat <<_EOH
usage:
   dmathicsdoc [options]

Run a PDF vewier on the Mathics Reference contiained in the Mathics docker image.

Options specfic to dmathicsdoc:

  -u | -U | --upgrade | upgrade
  pull updates to the docker container if there is a new image

  -h| --help | help
  show this help and exit

  -V| --version
  show the dmathicsdoc version and exit

_EOH
			exit 101
			;;
        -u | -U | --upgrade | upgrade)
			$DOCKER pull $MATHICS_IMAGE
			;;
        -v | -V | --version)
			echo "dmathicsdoc version ${dmathicsdoc_version}"
			exit 100
			;;
		default)
			stripped_args+=($arg)
    esac
done

# Show environment variables
for env_setting in MATHICS_IMAGE DISPLAY; do
	echo $env_setting = ${!env_setting}
done

$DOCKER run -it \
  --name mathics-doc \
  --rm \
  --env="DISPLAY" \
  --workdir=/app \
  --volume="$PWD":/app \
  --volume="/tmp/.X11-unix:/tmp/.X11-unix:rw" \
  --volume="/tmp:/usr/src/app/data" \
  $MATHICS_IMAGE \
  --mode pdf -- $@
