#!/bin/bash

#IMG_URL=http://193.225.174.140/allsky/current/aver.jpg  
#IMG_URL=http://193.225.174.140/allsky/current/gamma_aver-dark.jpg
#IMG_URL=http://193.225.174.154/allsky/current/gamma_aver-dark.jpg
IMG_FILE=/data/apal/allsky/allsky-latest.jpeg


LON=19.8983
LAT=47.9189

ADDR=193.225.174.141
PORT=8873

tau=""
dec=""
out=""

DRAW_HORIZONTAL_CIRCLES=0
DRAW_EQUATORIAL_CIRCLES=0

transbase=/usr/local/share/allsky/base.trans
lon=19.8983
lat=47.9189

k1=-0.02

time=`date +%s`
jd=`echo $time | awk '{ printf("%.5f\n",$1/86400.0+2440587.5); }'`
st=`/usr/local/bin/celcoord 0 0 $jd | awk -v lon=$lon '{ printf("%.5f\n",$3+lon); }'`

function radec_to_xy()
{
 local	jd=$3
 local	st=$4

 echo $1 $2 | \
 /usr/local/bin/celcoord $jd | \
 awk '{ print $1,$2; }' | \
 /usr/local/bin/grtrans --proj ra=$st,dec=$lat,max=80,arc,k1=$k1 \
 	--col-radec 1,2 | \
 /usr/local/bin/grtrans --input-transformation $transbase \
	--col-xy 1,2 
}

function taudec_to_xy()
{
 awk '{ print -$1,$2; }' | \
 /usr/local/bin/grtrans --proj ra=0,dec=$lat,max=80,arc,k1=$k1 \
 	--col-radec 1,2 | \
 /usr/local/bin/grtrans --input-transformation $transbase \
	--col-xy 1,2 | \
 awk '{ print $1,1040-$2; }'
}

while [ -n "$1" ] ; do
    case "$1" in
	-c|--coordinates)
		A=($(echo "$2" | tr "," " "))
		tau=${A[0]}
		dec=${A[1]}
		shift
		;;
	-z|--horizontal)
		DRAW_HORIZONTAL_CIRCLES=1
		;;
	--no-horizontal)
		DRAW_HORIZONTAL_CIRCLES=0
		;;
	-q|--equatorial)
		DRAW_EQUATORIAL_CIRCLES=1
		;;
	--no-equatorial)
		DRAW_EQUATORIAL_CIRCLES=0
		;;
	-b|--base)
		base="$2"
		shift
		;;
	-o|--output)
		out="$2"
		shift
		;;
	*)
		echo -e "$0: error: invalid command line argument near '$1'" >> /dev/stderr
		exit	1
		;;
    esac ; shift
done

test "$out" == "-" && out=""

n=0
for sys in psch prc80; do

	case "$sys" in
	prc80)	A=(`socket 193.225.174.181 2488 2>/dev/null | tr "=" " "`)
		if [ ${#A[*]} == 0 ]; then
			tau=0
			dec=10
			daz=0
			status=unreachable
		else
			tau=${A[13]}
			dec=${A[9]}
			daz=0
			status=ok
		fi
		;;
	psch)	ADDR=193.225.174.141
		PORT=8873
		if ping $ADDR -c 1 -w 1 >/dev/null 2>/dev/null; then
			A=($(echo -e -n  "mountstatus\ndomeazimuth\nquit\n" | nc $ADDR $PORT 2>/dev/null | head -n 3 | tr "=" " "))
		else
			A=()
		fi
		if [ ${#A[*]} == 0 ] || [ "${A[2]}" == unreachable ]; then
			tau=0
			dec=45
			daz=70
			status=unreachable
		else
	                tau=${A[8]}
        	        dec=${A[4]}
                	daz=${A[13]}
			status=ok
		fi
		;;
	prcc)	ADDR=193.225.174.141
		PORT=8878
		if ping $ADDR -c 1 -w 1 >/dev/null 2>/dev/null; then
			A=(`nc --recv-only $ADDR $PORT 2>/dev/null`)
		else
			A=()
		fi
		if [ ${#A[*]} == 0 ]; then
			tau=0
			dec=45
			daz=70
			status=unreachable
		else
	                tau=${A[0]}
        	        dec=${A[1]}
                	daz=${A[2]}
			status=ok
		fi
		;;
	esac

	TARR[n]="$tau $dec $daz"
	n=$((n+1))
done


if test $DRAW_HORIZONTAL_CIRCLES -ne 0 ; then

	A=($(	(	
			echo; for (( a=0 ; a<=360 ; a+=6 )) ; do echo  30 $a; done
			echo; for (( a=0 ; a<=360 ; a+=6 )) ; do echo  60 $a; done
			echo; for (( h=30 ; h<=60 ; h+=5 )) ; do echo $h +60; done
			echo; for (( h=30 ; h<=60 ; h+=5 )) ; do echo $h -60; done
			echo; for (( h=30 ; h<=60 ; h+=5 )) ; do echo $h 180; done
		
		) | \
		awk -v st=$st -v lat=$LAT \
		 'function sina(x) \
		  {	return(sin(x*atan2(1,1)/45));
		  } \
		 function cosa(x) \
		  {	return(cos(x*atan2(1,1)/45));
		  } \
		 function arga(x,y) \
		  {	return(atan2(y,x)*45.0/atan2(1,1));
		  } \
		  {	if ( NF < 2 )
				printf("x 0 0\n");
			else
			 {	hh=$1;
				az=$2;
				x=cosa(hh)*cosa(az);
				y=cosa(hh)*sina(az);
				z=sina(hh);
				printf("y %7.4f %7.4f\n",x,y);
			 }
		 }' | \
		grtrans	--input-transformation $base.trans \
			--col-xy 2,3 --col-out 2,3 \
			--output - | \
		awk \
		 '{	printf("%s %g %g\n",$1,$2*1.2,576-$3*1.2); 
		  }' 	))
	
	polygon_horiz=""
	d=0
	b=0
	for p in ${A[*]} ; do
		if test $p == "x" ; then 
			polygon_horiz="$polygon_horiz polygon "
			d=0
			b=0
		elif test $p == "y" ; then
			d=1
		elif test $d == 1 ; then
			test $b == 0 || polygon_horiz="$polygon_horiz,"
			polygon_horiz=${polygon_horiz}$p
			b=1
		fi
	done
else
	polygon_horiz=""
fi

if test $DRAW_EQUATORIAL_CIRCLES -ne 0 ; then

	A=($(	(	
			echo; for (( t=0 ; t<=360 ; t+=6 )) ; do echo  60 $t; done
			echo; for (( t=-96 ; t<=96; t+=6 )) ; do echo  30 $t; done
			echo; for (( t=-70 ; t<=70; t+=6 )) ; do echo   0 $t; done
			echo; for (( d=-30; d<=120 ;  d+=5 )) ; do echo $d   0; done
			echo; for (( d=-15; d<=125 ;  d+=5 )) ; do echo $d  45; done
			echo; for (( d=-15; d<=125 ;  d+=5 )) ; do echo $d -45; done
			echo; for (( d=20 ; d<=160 ;  d+=5 )) ; do echo $d  90; done
		
		) | \
		awk -v st=$st -v lat=$LAT \
		 'function sina(x) \
		  {	return(sin(x*atan2(1,1)/45));
		  } \
		 function cosa(x) \
		  {	return(cos(x*atan2(1,1)/45));
		  } \
		 function arga(x,y) \
		  {	return(atan2(y,x)*45.0/atan2(1,1));
		  } \
		  {	if ( NF < 2 )
				printf("x 0 0\n");
			else
			 {	d=$1;
				t=$2;
				phi=47.5
				x=sina(phi)*cosa(d)*cosa(t)-cosa(phi)*sina(d);
				y=cosa(d)*sina(t);
				z=cosa(phi)*cosa(d)*cosa(t)+sina(phi)*sina(d);
				printf("y %7.4f %7.4f\n",x,y);
			 }
		 }' | \
		grtrans	--input-transformation $base.trans \
			--col-xy 2,3 --col-out 2,3 \
			--output - | \
		awk \
		 '{	printf("%s %g %g\n",$1,$2*1.2,576-$3*1.2); 
		  }' 	))
	
	polygon_equat=""
	d=0
	b=0
	for p in ${A[*]} ; do
		if test $p == "x" ; then 
			polygon_equat="$polygon_equat polyline "
			d=0
			b=0
		elif test $p == "y" ; then
			d=1
		elif test $d == 1 ; then
			test $b == 0 || polygon_equat="$polygon_equat,"
			polygon_equat=${polygon_equat}$p
			b=1
		fi
	done
else
	polygon_equat=""
fi

T0=(${TARR[0]})
tau=${T0[0]}
dec=${T0[1]}

A=($(	echo $tau $dec | \
	awk -v H=1.50 \
	 '{	t=$1;d=$2;
		pi=4*atan2(1,1);
		hd=H;
		ht=H/cos(d*pi/180.0);
		print t-ht,d-hd;
		print t-ht,d+hd;
		print t+ht,d+hd;
		print t+ht,d-hd;
	 }' | \
	taudec_to_xy ))

X01=${A[0]}
Y01=${A[1]}
X02=${A[2]}
Y02=${A[3]}
X03=${A[4]}
Y03=${A[5]}
X04=${A[6]}
Y04=${A[7]}

T0=(${TARR[1]})
tau=${T0[0]}
dec=${T0[1]}

A=($(	echo $tau $dec | \
	awk -v H=1.0 \
	 '{	t=$1;d=$2;
		pi=4*atan2(1,1);
		hd=H;
		ht=H/cos(d*pi/180.0);
		print t-ht,d;
		print t,d+hd;
		print t+ht,d;
		print t,d-hd;
	 }' | \
	taudec_to_xy ))

X11=${A[0]}
Y11=${A[1]}
X12=${A[2]}
Y12=${A[3]}
X13=${A[4]}
Y13=${A[5]}
X14=${A[6]}
Y14=${A[7]}

T0=(${TARR[2]})
tau=${T0[0]}
dec=${T0[1]}

A=($(	echo $tau $dec | \
	awk -v H=1.0 \
	 '{	t=$1;d=$2;
		pi=4*atan2(1,1);
		hd=H;
		ht=H/cos(d*pi/180.0);
		print t-ht,d;
		print t,d+hd;
		print t+ht,d;
		print t,d-hd;
		print t,d;
	 }' | \
	taudec_to_xy ))

X21=${A[0]}
Y21=${A[1]}
X22=${A[2]}
Y22=${A[3]}
X23=${A[4]}
Y23=${A[5]}
X24=${A[6]}
Y24=${A[7]}
X20=${A[8]}
Y20=${A[9]}

#	-stroke yellow -draw "ellipse $X,$Y 3,3 0,360" \
#	-stroke red -draw "ellipse $X,$Y 5,5 0,360" \

#	-stroke red  -draw "$polygon_horiz" \
#	-stroke blue -draw "$polygon_equat" \

convert	jpeg:${IMG_FILE} \
	-strokewidth 1 -fill none \
	-stroke yellow -draw "polygon $X01,$Y01,$X02,$Y02,$X03,$Y03,$X04,$Y04" \
	-stroke cyan   -draw "polygon $X11,$Y11,$X12,$Y12,$X13,$Y13,$X14,$Y14" \
	-stroke lightgreen -draw "circle $X20,$Y20 $X21,$Y21" \
	png:- | \
if [ -n "$out" ] ; then
	cat > "$out"
else
	cat
fi

rm -f $tmp
