#!/bin/bash
set -e
ARGS=`getopt -o e:l:m:p:u:w:j:k:t --l conda_env:,language:,password:,port:,base_url:,workdir:,jupyter_timeout:,kernel_timeout:,terminal_timeout: -n "$0" -- "$@"`
if [ $? != 0 ]; then
    echo "Terminating.Unknown parameters..."
    exit 1
fi
eval set -- "${ARGS}"
while true
do
    case "$1" in
        -e|--conda_env)
            conda_env=$2
            shift 2
            ;;
        -l|--language)
            language=$2
            shift 2
            ;;
        -m|--password)
            password=$2
            shift 2
            ;;
        -p|--port)
            port=$2
            shift 2
            ;;
        -u|--base_url)
            base_url=$2
            shift 2
            ;;
        -w|--workdir)
            workdir=$2
            shift 2
            ;;
        -j|--jupyter_timeout)
            jupyter_timeout=$2
            shift 2
            ;;
        -k|--kernel_timeout)
            kernel_timeout=$2
            shift 2
            ;;
        -t|--terminal_timeout)
            terminal_timeout=$2
            shift 2
            ;;
        --)
            shift
            break
            ;;
        *)
            echo "Internal error!"
            exit 1
            ;;
    esac
done
if [[ -z $jupyter_timeout ]]; then
    jupyter_timeout=0
fi
if [[ -z $kernel_timeout ]]; then
    kernel_timeout=0
fi
if [[ -z $terminal_timeout ]]; then
    terminal_timeout=0
fi
is_empty_dir(){
    return `ls -A $1|wc -w`
}

# Accept Anaconda Terms of Service for required channels
conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/main
conda tos accept --override-channels --channel https://repo.anaconda.com/pkgs/r

if is_empty_dir $conda_env
then
    conda create -y --clone $IMAGE_CONDA_ENV -p $conda_env --quiet
    source activate $conda_env
else
    set +e
    source activate $conda_env
    if [ $? -ne 0 ]; then
        echo "This directory is not a non-empty directory and not have a canda environment!"
        exit 1
    fi
    set -e
fi
jupyter notebook --no-browser --ip=0.0.0.0 --port=$port --ServerApp.port_retries=0 \
--PasswordIdentityProvider.hashed_password=$password --ServerApp.root_dir=$workdir \
--ServerApp.base_url=$base_url --ServerApp.allow_origin='*' \
--ServerApp.allow_remote_access=True --ServerApp.disable_check_xsrf=True \
--ServerApp.shutdown_no_activity_timeout=$jupyter_timeout \
--MappingKernelManager.cull_idle_timeout=$kernel_timeout \
--TerminalManager.cull_inactive_timeout=$terminal_timeout \
--allow-root
