#!/bin/sh
#   subc-aux-verify - detect missing plugin-plugin dependencies
#   Copyright (C) 2025 Tibor 'Igor2' Palinkas
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License along
#   with this program; if not, write to the Free Software Foundation, Inc.,
#   31 Milk Street, # 960789 Boston, MA 02196 USA.
#
#   http://repo.hu/projects/pcb-rnd
#

# Read a footprint in lihata fp format, pick out subc-aux line objects and
# verify the vectors are present and point to the standard direction.
# Supply a list of file names on the command line.

# input sample:
#data	text	/pcb-rnd-subcircuit-v6/0::subc.5/data/layers/1::subc-aux/objects/0::line.15/y2	1.28016mm
#data	text	/pcb-rnd-subcircuit-v6/0::subc.5/data/layers/1::subc-aux/objects/0::line.15/attributes/subc-role	pnp-origin

subc_aux_verify()
{
lhtflat < $1 | awk -v "fn=$1" '
	BEGIN {
		retval = 0
	}
	
	function error(msg)
	{
		print fn ":", msg > "/dev/stderr"
		retval = 1
	}

	function coord(str      ,num,suff)
	{
		num=str
		suff=str
		sub("^[+-]?[0-9]*[.]?[0-9]*", "", suff)
		sub(suff, "", num)
		if (suff == "mm")   return num
		if (suff == "um")   return num/1000
		if (suff == "nm")   return num/1000000
		if (suff == "mil")  return num*0.0254
		if (suff == "in")  return num*25.4
		if (suff == "inch")  return num*25.4
		error("unknown unit suffix " suff)
		return "???"
	}

	function is_one(num)
	{
		num = num - 1;
		return ((num > -0.000001) && (num-1 < -0.000001))
	}

	($1 == "data") && ($2 == "text") && ($3 ~ "layers/[0-9]*::subc-aux") {
		tmp = $3
		sub("^.*::line.", "", tmp)
		id = tmp
		key = tmp
		sub("/.*$", "", id)
		sub("^[^/]*/", "", key)
		
		IDS[id]++
		VAL[id, key] = $4
	}

	END {
		for(id in IDS) {
			role = VAL[id, "attributes/subc-role"]
			if (role == "origin") {
				found_origin=1
				if (coord(VAL[id, "y1"]) != coord(VAL[id, "y2"]))
					error("subc-aux origin y mismatch")
				if (coord(VAL[id, "x1"]) != coord(VAL[id, "x2"]))
					error("subc-aux origin x mismatch")
			}
			else if (role == "pnp-origin") {
				if (coord(VAL[id, "y1"]) != coord(VAL[id, "y2"]))
					error("subc-aux origin y mismatch")
				if (coord(VAL[id, "x1"]) != coord(VAL[id, "x2"]))
					error("subc-aux pnp-origin x mismatch")
			}
			else if (role == "x") {
				found_x=1
				if (coord(VAL[id, "y1"]) != coord(VAL[id, "y2"]))
					error("subc-aux x vector not horizontal")
				len = coord(VAL[id, "x2"]) - coord(VAL[id, "x1"])
				if (!is_one(len))
					error("subc-aux x vector is not +1 but " len)
			}
			else if (role == "y") {
				found_y=1
				if (coord(VAL[id, "x1"]) != coord(VAL[id, "x2"]))
					error("subc-aux y vector not vertical")
				len = coord(VAL[id, "y2"]) - coord(VAL[id, "y1"])
				if (!is_one(len))
					error("subc-aux y vector is not +1 but " len)
			}
		}

		if (!found_origin)
			error("subc-aux origin missing")
		if (!found_x)
			error("subc-aux x vector missing")
		if (!found_y)
			error("subc-aux y vector missing")

		exit(retval)
	}
'
}

for n in "$@"
do
	subc_aux_verify "$n"
done
