#!/bin/sh

# process text(s)

set -e

# resolve options
eval set -- "$(getopt -s sh -o f -- "$@")"
while true; do case "$1" in -f) force=-f; shift;; --) shift; break;; esac; done

. /lib/lsb/init-functions

# fetch PDF files
./mkpdf $force "$@"

# resolve stems from available PDF files if none provided
[ "$#" -gt 0 ] || eval set -- $(ls -1 *.pdf | sed 's/.pdf$//')

# run tasks - either generic or with "_$stem" suffix
log_action_begin_msg "Apply tasks"
for stem in "$@"; do
#	log_action_cont_msg $stem
#	for task in pdf2txt txt2text slice; do
#	for task in pdf2htm htm2html html2md md2mdwn slice; do
	for task in pdf2htm htm2html; do
#		log_action_cont_msg $task
#		test ! -x $task-all || ./$task-all $force $stem
		taskscript=mk$task-$stem
		[ -x $taskscript ] || taskscript=mk$task-default
		./$taskscript $force $stem
	done
done
log_action_end_msg $?