#!/usr/local/bin/zsh

function prefix() { PYENV_VERSION=3.9.5 pyenv exec "$@"; }
function suffix_grep() { prefix "$@" | grep ${JUP_PORT:-9888}; }
function suffix_null() { $@ >/dev/null 2>&1; }

function jupyter_start() {
	prefix nohup jupyter lab --no-browser --ServerApp.disable_check_xsrf=True --port ${JUP_PORT:-9888} -y >$HOME/jup.log 2>&1 &
	export JUP_PID=$!
}

function jupyter_kill() {
	# prefix jupyter server stop ${JUP_PORT:-9888} >>$HOME/jup.log 2>&1
	suffix_null jupyter server stop ${JUP_PORT:-9888}
	unset JUP_PID
}

function jupyter_help() {
	printf "Usage: jupyter_run [-h, --help] [--stop] [-s, --show] [-r, --restart]\n"
}

function jupyter_run() {
	typeset -i num_arg
	num_arg=$#
	if [[ $num_arg == '0' ]]; then
		suffix_null suffix_grep jupyter server list || jupyter_start
		return 0 2>/dev/null
	fi
	while [[ "$1" =~ ^- && ! "$1" == "--" ]]; do
		case $1 in
		-r | --restart)
			jupyter_kill
			jupyter_start
			return 0 2>/dev/null
			;;
		--stop)
			jupyter_kill
			return 0 2>/dev/null
			;;
		-s | --show)
			suffix_grep jupyter server list
			return 0 2>/dev/null
			;;
		-h | --help)
			jupyter_help
			return 0 2>/dev/null
			;;
		*)
			jupyter_help
			return 1 2>/dev/null
			;;
		esac
		shift
	done
	if [[ "$1" == '--' ]]; then shift; fi
}

jupyter_run
