#!/bin/bash

# move the EAST or WEST connectors over a power supply
# arg1 is file on which to do it
# eg.
# fix_conn multi8_p

if test $# -eq 0
then
  echo "# Usage vsc_align_conn cell_name" 1>&2
  echo "#" 1>&2
  echo "#   will move the EAST and WEST connectors of cell_name over the nearest" 1>&2
  echo "#   power supply." 1>&2
  echo "#   eg  vsc_align_conn fred  will move the left and right connectors" 1>&2
  echo "#   above the nearest horizontal ALU1 power supplies on cell cell_name.ap." 1>&2
  exit 1
fi

if test -f $1.ap
then
  cell=$1.ap
else
  echo "# Usage vsc_align_conn cell_name" 1>&2
  echo "#" 1>&2
  echo "#   The cell name supplied "$1".ap does not exist. Please check." 1>&2
  exit 1
fi

#                cell    ,      P      ,     date    , scale
scale=$(grep '^H ' $cell | sed 's/^H  *\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\) *$/\4/')
pitch=72
lambda_metal_pitch=8
let "metal_pitch=lambda_metal_pitch*scale"

for side in EAST WEST
do
#  awk -v FS=, '$6 ~ /'$side'/ {print $1","int($2/'$scale'/'$pitch')*'$scale'*'$pitch'","$3","$4","$5","$6","$7}
#  $6 !~ /'$side'/ {print}' $cell | \
  awk -v FS=, '$6 ~ /'$side'/ {print $1","int(($2+'$pitch'*'$scale'/2)/'$scale'/'$pitch')*'$scale'*'$pitch'","$3","$4","$5","$6","$7}
  $6 !~ /'$side'/ {print}' $cell | \
  sed "/$side/ s/^C  *\([^,][^,]*\),*\(0\),*\([^,][^,]*\),*\([^,][^,]*\),*\([^,][^,]*\),*\([^,][^,]*\),*\([^,][^,]*\) *$/C \1,$metal_pitch,\3,\4,\5,\6,\7/" > $$temp
  mv -f $$temp $cell
done





