#!/bin/bash
#                       /usr/local/bin/calibre
# https://crystalfaeries.net/posix/bin/calibre
# celeste@crystalfaeries.net CALIBRE 2021-04-18 00:07:44+00:00
# manual command line wrapper script to switch between calibre-server and GUI
# we've uninstalled calibre and hope to never deal with it again, preferring
# doc2html to turn e-pubs into webpages
let help=7	# this line's number minus one

case $# in
0)	# no	options or arguments
	for signal in hup term kill
	do	# kill off the stand-alone calibre-server process so we can run the GUI
		kill -"$signal"	\
		$(ps -ef | grep /usr/bin/calibre-server | grep -v 'color=auto' | sed 's/^celeste *//;s/ .*$//')	\
		2>/dev/null	||	sleep 6
	done
	/usr/bin/calibre	# like magic, server out of the way while we run GUI, then restart server:
	/usr/bin/calibre-server --disable-local-write --port 8008 --daemonize --log=/var/log/calibre.log --access-log=/var/log/calibre.log --url-prefix /calibre /home/celeste/crystalfaeries.net/documents/calibre & # OPEN ACCESS
	;;
1)	# one	option or argument
	case $1 in
	-v | --version)
		head -n 4 $0 | tail -n 1
		exit
		;;
	-h | --help)
		head -n $help $0
		exit
		;;
	*)	#
		echo	"We're not feeling very argumentative today."
		shift
		exec	$0	"${@}"
		;;
	esac
	;;
2)	# two	options or arguments
	echo	"$0 not feeling that argumentative today."
	shift
	exec	$0	"${@}"
	;;
*)	# many	options or arguments
	echo	"$0 not hungry enough to eat all those arguments."
	shift
	exec	$0	"${@}"
	;;
esac

