#!/bin/bash

ECPATH=$(type -p voxren)

ECPATH=${ECPATH%/voxren}
ECPATH=${ECPATH%/bin}
PSCONFPATH=${ECPATH}/share/eccet/config/modules/save/GENPPM
TEMPLATECONFPATH=${ECPATH}/share/eccet/config/modules/save/PPMTOHTML

echo "Install Path is $ECPATH"
echo

#
# Do some preliminary checks about available programs.
#


# ppmtops filter for eccet.

PROG=$(type -p eccet_ppmtops)
[ "$PROG" == "" ] && \
	{ echo "eccet_ppmtops is required for writing postscript output."; \
	  echo "Looks like your Eccet setup is imcomplete."; \
	  exit 1; }
[ -x $PROG ] || \
	{ echo "eccet_ppmtops is there but not executable."; \
	  echo "Looks like your Eccet setup is imcomplete."; \
	  exit 1; }


# ppmtohtml filter for eccet.

PROG=$(type -p eccet_ppmtohtml)
[ "$PROG" == "" ] && \
	{ echo "eccet_ppmtohtml is required for writing postscript output."; \
	  echo "Looks like your Eccet setup is imcomplete."; \
	  exit 1; }
[ -x $PROG ] || \
	{ echo "eccet_ppmtohtml is there but not executable."; \
	  echo "Looks like your Eccet setup is imcomplete."; \
	  exit 1; }

# ppmtopng

PROG=$(type -p pnmtopng)
[ "$PROG" == "" ] && \
	{ echo "pnmtopng is required for writing postscript output."; \
	  echo "Please install the netpbm package."; \
	  exit 1; }
[ -x $PROG ] || \
	{ echo "pnmtopng is there but not executable."; \
	  echo "Please install the netpbm package correctly."; \
	  exit 1; }

# m4

PROG=$(type -p m4)
[ "$PROG" == "" ] && \
	{ echo "The m4 macro processor is required for writing postscript output."; \
	  echo "Please install the m4 package."; \
	  exit 1; }
[ -x $PROG ] || \
	{ echo "The m4 macro processor is there but not executable."; \
	  echo "Please install the m4 package correctly."; \
	  exit 1; }

# html2ps

PROG=$(type -p html2ps)
[ "$PROG" == "" ] && \
	{ echo "The html2ps utility is required for writing postscript output."; \
	  echo "Please install the html2ps package."; \
	  exit 1; }
[ -x $PROG ] || \
	{ echo "The html2ps utility is there but not executable."; \
	  echo "Please install the html2ps package correctly."; \
	  exit 1; }


if [ $# == 0 ] ; then
	echo "Usage: $0 [-d] -t template|all"
	echo "Sets up or deletes usage of templates use by the Eccet "
	echo "Postscript(TM) save module."
	echo "See man ${0##*/} for details."
	exit 1
fi

DO_DELETE=0
TEMPLATES=""
ALLTEMPLATES=$(cd $TEMPLATECONFPATH; ls *.m4 | sed -e 's/\.m4$//' | tr "
" " ")

while getopts "dt:" OPTCHAR; do
	case "$OPTCHAR" in
		"d")
			DO_DELETE=1
		;;
		"t")
			TEMPLATES=$TEMPLATES" "$OPTARG
		;;
	esac
done

TEMPLATES=${TEMPLATES# }

if [ "$DO_DELETE" == "1" ] ; then

	TODELETE=""
	cd $PSCONFPATH
	for template in $TEMPLATES ; do
		if [ "$template" == "all" ] ; then
			template="*"
		fi
		TDNEW=$(ls PS_${template}.test 2>/dev/null | sed -e 's/\.test//g' )
		TODELETE=$TODELETE" "${TDNEW}
	done
	TODELETE=${TODELETE# }
	echo
	echo "The following template bindings (format: PS_template)"
	echo "have been selected for deletion:"
	for a in $TODELETE; do
		echo $a
	done
	if [ "$TODELETE" == "" ] ; then
		echo "No configured templates found. Aborting."
		exit 1;
	fi
	echo 
	echo "Do you want to delete these bindings? (yes or no)"
	while read a; do
		if [ "$a" == "yes" ]; then
			break;
		elif [ "$a" == "no" ] ;  then
			echo "O.k. - aborting."
			exit 1;
		else
			echo "Please answer yes or no."
		fi
	done
	echo "O.k. - deleting."
	for a in $TODELETE; do
		rm -f $a $a.test
	done
	exit
fi

if [ "$TEMPLATES" == "all" ] ; then
	TEMPLATES="$ALLTEMPLATES"
fi

echo "You requested to add Eccet Postscript bindings for "
echo "these templates: $TEMPLATES"

for b in $TEMPLATES; do
	if [ -f $PSCONFPATH/PS_${b} ] ; then
		echo "Config file for template $b exists. Unchanged."
		echo "Please delete it first, if you want to update that."
	else
		echo "No config file for template $b. Setting up one."
		cat >$PSCONFPATH/PS_${b} <<EOF
|eccet_ppmtops $b %s %s %08d
EOF
		cat >$PSCONFPATH/PS_${b}.test <<EOF
#!/bin/bash

PROG=\$(type -p eccet_ppmtops)
[ "\$PROG" == "" ] && exit 1
[ -x \$PROG ] || exit 1
PROG=\$(type -p eccet_ppmtohtml)
[ "\$PROG" == "" ] && exit 1
[ -x \$PROG ] || exit 1 
PROG=\$(type -p pnmtopng)
[ "\$PROG" == "" ] && exit 1
[ -x \$PROG ] || exit 1
PROG=\$(type -p m4)
[ "\$PROG" == "" ] && exit 1
[ -x \$PROG ] || exit 1
PROG=\$(type -p html2ps)
[ "\$PROG" == "" ] && exit 1
[ -x \$PROG ] || exit 1
EOF
		fi
done
