#!/usr/bin/env bash

fail () {
    echo "ERROR: ${1}" 1>&2
    exit 1
}

help () {
    cat << EOF
Usage:

    sage -rst2ipynb <source> [<destination>]

Generates IPython worksheet (.ipynb) from standalone reStructuredText source.

If the destination is not specified, the IPython worksheet is written in the
standard output.

Examples:

    sage -rst2ipynb file.rst file.ipynb

    sage -rst2ipynb file.rst

Options:
  -h, --help            show this help message and exit
EOF
}

if [ "${1}" = '-h' ] || [ "${1}" = '--help' ] ; then help ; exit 0 ; fi

# tests if dependencies are available
pandoc -v > /dev/null 2>&1 || fail 'rst2ipnb requires pandoc; please install it on your system.'
[ $(sage-list-packages installed | grep '^rst2ipynb\.\.\.') ] || fail \
    'The rst2ipynb optional package is required; please install it with `sage -i rst2ipynb`.'

case  "${#}" in
    (1)
        rst2ipynb --kernel='sagemath' ${1} 2> /dev/null || help ;;
    (2)
        rst2ipynb --kernel='sagemath' ${1} -o ${2}  2> /dev/null || help ;;
    (*)
        help ;;
esac

exit 0
