#!/bin/bash

ECPATH=$(type -p voxren)

ECPATH=${ECPATH%/voxren}
ECPATH=${ECPATH%/bin}
DICOMCONFPATH=${ECPATH}/share/eccet/config/modules/save/PPM
TEMPLATECONFPATH=${ECPATH}/share/eccet/config/modules/save/PPMTODICOM

echo "Install Path is $ECPATH"
echo

#
# Do some preliminary checks about available programs.
#


# ppmtodicom filter for eccet.

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

# eccet_dicom_synthesizer for eccet.

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

# m4

PROG=$(type -p m4)
[ "$PROG" == "" ] && \
	{ echo "The m4 macro processor is required for writing DICOM 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; }

if [ $# == 0 ] ; then
	echo "Usage: $0 [-d] -t template|all"
	echo "Sets up or deletes usage of templates use by the Eccet "
	echo "DICOM 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 "dp:t:" OPTCHAR; do
	case "$OPTCHAR" in
		"d")
			DO_DELETE=1
		;;
		"t")
			TEMPLATES=$TEMPLATES" "$OPTARG
		;;
	esac
done

TEMPLATES=${TEMPLATES# }

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

	TODELETE=""
	cd $DICOMCONFPATH
	for template in $TEMPLATES ; do
		if [ "$template" == "all" ] ; then
			template="*"
		fi
		TDNEW=$(ls DICOM_${template}.test 2>/dev/null | sed -e 's/\.test//g' )
		TODELETE=$TODELETE" "${TDNEW}
	done
	TODELETE=${TODELETE# }
	echo
	echo "The following template bindings (format: DICOM_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 DICOM writing bindings for "
echo "these templates: $TEMPLATES"

for b in $TEMPLATES; do
	if [ -f $DICOMCONFPATH/DICOM_${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 >$DICOMCONFPATH/DICOM_${b} <<EOF
|eccet_ppmtodicom $b %s %s %08d
EOF
		cat >$DICOMCONFPATH/DICOM_${b}.test <<EOF
#!/bin/bash

PROG=\$(type -p eccet_ppmtodicom)
[ "\$PROG" == "" ] && exit 1
[ -x \$PROG ] || exit 1 
PROG=\$(type -p m4)
[ "\$PROG" == "" ] && exit 1
[ -x \$PROG ] || exit 1
EOF
		chmod a+x $DICOMCONFPATH/DICOM_${b}.test
	fi
done
