#! /usr/bin/env bash
#   check_extraction: checks all the ext files in the directory specified for merge shorts.

#   Copyright 2022 D. Mitch Bailey  cvc at shuharisystem dot com

#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.

echo "Merge shorts: shorts between ports that do not appear in the extraction report."
for ext_path in $1/*.ext*; do
	ext_file=${ext_path##*/}
	ext_cell=${ext_file%%.ext*}
	if [[ ${ext_file##*.} == "gz" ]]; then
		CAT=zcat
	else
		CAT=cat
	fi
	$CAT $ext_path |
		awk -v CELL=$ext_cell -f $LVS_ROOT/scripts/merge_short.awk
done

zgrep -c ^port $1/*.ext* |
	grep ':0$' |
	sed -e 's,.*/,,' -e 's/.ext:.*//' |
	sort > $1/../noport.subckt

awk '/^.subckt/ {print $2}' $1/*.gds.spice |
	sort > $1/../layout.subckt

zegrep -c '^device' $1/*.ext* |
	grep -v ':0$' |
	sed -e 's,.*/,,' -e 's/.ext:.*//' |
	sort > $1/../device.subckt

zegrep -c '^use' $1/*.ext* |
	grep ':0$' |
	sed -e 's,.*/,,' -e 's/.ext:.*//' |
	sort |
	comm -12 - $1/../device.subckt > $1/../leaf.subckt

echo "
Leaf cells without explict ports"
comm -12 $1/../noport.subckt $1/../layout.subckt |
	comm -12 - $1/../leaf.subckt 

echo "
Non-leaf cells without explict ports"
comm -12 $1/../noport.subckt $1/../layout.subckt |
	comm -23 - $1/../leaf.subckt
