#!/bin/bash


if [[ $# -ne 1 ]]; then
  echo "usage: generate_light_gauge.sh OUTFILENAME" 1>&2 ;
  echo "aborted" 1>&2 ;
  exit 1
fi

outfile="$1"; shift;

#Calibration Board definitions
disc_radius_mm=30
border_size_mm=5
screw_height_mm=26.5
pixels_per_mm=20
azim_interval=10
elev_interval=5
font_size_mm=2

#Usefull constants

PI=`echo "scale=10; 4*a(1)" | bc -l`



disc_radius_px=`echo "(${disc_radius_mm}*${pixels_per_mm})"| bc -l`
border_size_px=`echo "(${border_size_mm}*${pixels_per_mm})"| bc -l`
screw_height_px=`echo "(${screw_height_mm}*${pixels_per_mm})"| bc -l`

im_size=`echo "(${disc_radius_px}+${border_size_px})*2" | bc -l`
center_x=`echo "scale=0;(${im_size}/2)" | bc -l`
center_y=`echo "scale=0;(${im_size}/2)" | bc -l`
font_size_px=`echo "scale=0;(${pixels_per_mm}*${font_size_mm})" | bc -l`



command_line="-size ${im_size}x${im_size} xc:skyblue -fill white -stroke black -pointsize ${font_size_px} "
#draw circle
tx=`echo "scale=0;(${im_size}-${border_size_px})" | bc -l`
ty=${center_y}
command_line="${command_line} -draw \"circle ${center_x},${center_y} ${tx},${ty}\" "

#Set the fiolling as transparent
command_line="${command_line} -fill transparent "

#Draw concentric circles
current_angle=0
while [ ${current_angle} -lt 90 ] ; do
	
	radii_angle=`echo "(${current_angle}*${PI})/180" | bc -l`
	radius=`echo "scale=2;(s(${radii_angle})*${screw_height_px})/c(${radii_angle})" | bc -l`
	#echo "ANGLE ${current_angle} (${radii_angle}) RADIUS ${radius} "
	
	tx=`echo "scale=0; (${center_x}-${radius})" | bc -l`
	ty=${center_y}
	
	test_radius=`echo "scale=0;(${radius} < ${disc_radius_px} )" | bc -l`
	
	if [ ${test_radius} -eq 1 ]  ; then
# 		echo DRAW ${center_x},${center_y} ${tx},${ty}

		command_line="${command_line} -draw \"circle ${center_x},${center_y} ${tx},${ty}\" "
		command_line="${command_line} -draw 'text ${ty},${tx} \"${current_angle}\" ' "
	fi
	
	
	current_angle=$((${current_angle} + ${elev_interval} ))	
	
done

#Draw lines 

#Draw concentric circles
current_angle=0
while [ ${current_angle} -lt 360 ] ; do
	    
	radii_angle=`echo "(${current_angle}*${PI})/180" | bc -l`

	tx=`echo "scale=2;(c(${radii_angle})*${disc_radius_px})+${center_x}" | bc -l`
	ty=`echo "scale=2;(s(${radii_angle})*${disc_radius_px})+${center_y}" | bc -l`
	 
	text_x=`echo "scale=2;(c(${radii_angle})*(${disc_radius_px} ))+${center_x}" | bc -l`
	text_y=`echo "scale=2;(s(${radii_angle})*(${disc_radius_px} ))+${center_y}" | bc -l`

	angle_color="black"

	if [ ${current_angle} -eq 0 ] ; then
	  angle_color="red"
	fi

	if [ ${current_angle} -eq 90 ] ; then
	  angle_color="red"
	fi

	if [ ${current_angle} -eq 180 ] ; then
	  angle_color="red"
	fi

	if [ ${current_angle} -eq 270 ] ; then
	  angle_color="red"
	fi

# 	echo ANGLE ${current_angle} TX ${tx} TY ${ty}
	command_line="${command_line} -stroke ${angle_color} -draw 'line ${center_x},${center_y} ${tx},${ty}' "
	command_line="${command_line} -fill ${angle_color} -draw 'text ${text_y},${text_x} \"${current_angle}\" ' "

	current_angle=$((${current_angle} + ${azim_interval} ))	
	
done



#End of command line 
command_line="${command_line}	${outfile}"

#generate the image
echo convert ${command_line} > line.sh
source ./line.sh

