#!/bin/bash

TVMARK_VERSION=0.2
TVMARK_RELEASE=2013.03.02

XPASET=xpaset

CX=1
CY=2
CL=0
LF=""

RAD=4
COLOR="green"
LCOLOR="red"
IN=

IS_DELETE=

SHAPE=circle

SELF=$0

function print_error()
{
 echo "${SELF}: error: $*." >/dev/stderr
}

while [ -n "$1" ] ; do
    case "$1" in 
	-x|--col-xy)
		CC=($(echo $2 | tr "," " "))
		CX=${CC[0]}
		CY=${CC[1]}
		( test $CX -gt 0 -a $CY -gt 0 &>/dev/null ) || \
		 {	print_error "invalid column format: '$2'"
			exit 1;
		 }
		shift
		;;
	-l|--col-label)
		CL=$2
		( test $CL -gt 0 &>/dev/null ) || \
		 {	print_error "invalid label column: '$2'" 
			exit 1;
		 }
		shift
		;;
	-f|--label)
		LF="$2"
		shift
		;;
	-r|--radius|-s|--size)
		RAD=$2
		#( test $RAD -gt 0 &>/dev/null ) || \
		# {	print_error "invalid radius or size: '$2'" 
		#	exit 1;
		# }
		shift
		;;
	-c|--color|--colour)
		[ -n "$2" ] || 
		 {	print_error "invalid color: '$2'"
			exit 1;
		 }
		COLOR=$2
		shift
		;;
	-a|--label-color|--label-colour)
		[ -n "$2" ] || 
		 {	print_error "invalid color: '$2'"
			exit 1;
		 }
		LCOLOR=$2
		shift
		;;
	-d|--delete)
		IS_DELETE=1
		;;
	-e|--circle)
		SHAPE=circle
		;;
	-q|--square|--box)
		SHAPE=square
		;;
	-v|--version)
		echo -e "$0 ${TVMARK_VERSION} (${TVMARK_RELEASE})"
		echo -e "(c) 2007, 2013; Pal, A. <apal@szofi.net>"
		exit	0
		;;
	-h|--help)
		echo -e "Usage:\t$0 [-h|--help] [-v|--version]"
		echo -e "\t[<input>]" 
		echo -e "\t[-x|--col-xy <column_index_x>,<column_index_y>] "
		echo -e "\t[-e|--circle [-r|--radius <r>]]"
		echo -e "\t[-q|--square [-s|--size <s>]]"
		echo -e "\t[-c|--color <c>]"
		echo -e	"\t[-l|--col-label <column_index_label>"
		echo -e "\t[-f|--label '<label_format>']"
		echo -e "\t[-a|--label-color <c>]"
		echo -e "\t[-d|--delete]"
		echo -e "Notes:"
		echo -e " - in the label string, \$<N> is replaced by the content of the <N>th column"
		echo -e "   (like in AWK)"
		echo -e " - default format: --col-xy $CX,$CY --circle --color $COLOR --radius $RAD"
		exit 0
		;;
	-*)
		print_error "invalid command line argument near '$1'"
		exit 1
		;;
	*)
		[ -n "$IN" ] &&
		 {	print_error "input file has been specified yet"
			exit 1;
		 }
		IN=$1
		;;
    esac ; shift
done


( ${XPASET} -h 2>&1 > /dev/null ) >/dev/null
if test $? == 127; then
	print_error "program ${XPASET} is not available, exiting" 
	exit 1
fi

if [ -n "$IS_DELETE" ] ; then
	${XPASET} ds9 regions delete all </dev/null 2>/dev/null || \
	 {	print_error "${XPASET} failed (maybe DS9 is not running?)"
		exit 1;
	 }
	exit 0
fi

[ -n "$IN" ] && { [ -f "$IN" ] || \
 {	print_error "file '$IN' not exists" 
	exit 1;
 }; }

if [ ! -n "$IN" -o "$IN" == "-" ] ; then
	TMP=`mktemp`
	cat $IN >$TMP
	IN="$TMP"
else
	TMP=""
fi

(	echo "global color=$COLOR" 
	echo "image"
	case $SHAPE in 
	    square)
		cat $IN | awk -v CX=$CX -v CY=$CY -v RAD=$RAD \
		'!/^#/{ printf ("box(%g,%g,%g,%g)\n",$(CX)+0.5,$(CY)+0.5,RAD,RAD); }'
		;;
	    circle|*)
		cat $IN | awk -v CX=$CX -v CY=$CY -v RAD=$RAD \
		'!/^#/{ printf ("circle(%g,%g,%g)\n",$(CX)+0.5,$(CY)+0.5,RAD); }'
		;;
	esac	

	if [ $CL -gt 0 ] || [ -n "$LF" ] ; then
		echo "global color=$LCOLOR" 
		cat $IN | awk -v CX=$CX -v CY=$CY -v CL=$CL -v LF="$LF" -v RAD=$RAD \
		'!/^#/{  
			if ( LF != "" )
			 {	f=LF;
				for ( i=NF ; i>=1 ; i-- )
				 {	r=sprintf("\\$%d",i);
					gsub(r,$(i),f);
				 }
				printf("text(%g,%g,\"%s\")\n",$(CX),$(CY)-RAD/2-10,f);
			 }
			else if ( CL>0 )
				printf("text(%g,%g,\"%s\")\n",$(CX),$(CY)-RAD/2-10,$(CL));
		}' 
	fi

) | ${XPASET} ds9 regions 2>/dev/null || \
 {	print_error "${XPASET} failed (maybe DS9 is not running)"
	exit 1;
 }

test -n "$TMP" && rm -f "$TMP"

