#!/bin/bash

# Script which checks whether the blockages in argument_2
# are next to a via in argument_1 and removes them if not.

if ! test -d $MBK_TARGET_LIB
then
  echo "#?! No MBK_TARGET_LIB found. Please check." 1>&2
  exit 1
else
  library=$(basename $MBK_TARGET_LIB)
  case $library in
  rgalib013)
    metal_width=4; metal_pitch=8
    ;;
  ssxlib013)
    metal_width=4; metal_pitch=10
    ;;
  stxlib013)
    metal_width=4; metal_pitch=10
    ;;
  sxlib100)
    metal_width=2; metal_pitch=5
    ;;
  vgalib013)
    metal_width=4; metal_pitch=8
    ;;
  vsclib013)
    metal_width=4; metal_pitch=8
    ;;
  vtclib013)
    metal_width=4; metal_pitch=8
    ;;
  vsxlib013)
    metal_width=4; metal_pitch=10
    ;;
  vtxlib013)
    metal_width=4; metal_pitch=10
    ;;
  vxlib013)
    metal_width=2; metal_pitch=5
    ;;
  wsclib013)
    metal_width=4; metal_pitch=8
    ;;
  wtclib013)
    metal_width=4; metal_pitch=8
    ;;
  *)
    echo "#?! Library "$library" not supported yet. Please check." 1>&2
    exit 1
    ;;
  esac
fi

scale=$MBK_SCALE_X
if [ "$scale" = '' ]
then
  echo "# Error: environment variable \$MBK_SCALE_X must be set. Please check." 1>&2
  exit 1
fi

let "scaled_pitch=metal_pitch*scale"
let "scaled_width=metal_width*scale"

if [ "$#" -eq 0 ]
then
  echo "# Usage: pnr_strip_old_blockages cell blockage_cell H|V" 1>&2
  echo "#" 1>&2
  echo "#   will check whether the blockages in file blockage_cell.ap" 1>&2
  echo "#   are next to a via in cell.ap, and remove them if not. The" 1>&2
  echo "#   H|V argument is the preferred direction of ALU2 and is used." 1>&2
  echo "#   to check whether the block is next to the via.." 1>&2
  echo "#   eg  pnr_strip_old_blockages fred fred_bg H  will read fred_bg.ap and" 1>&2
  echo "#   remove TALU geometries which are not next to a via, starting." 1>&2
  echo "#   with TALU2 not immediately east or west of a VIA or VIA2." 1>&2
  exit 1
fi

if test -f $1.ap
then
  layout_cell=$1.ap
else
  echo "# Usage: pnr_strip_old_blockages cell blockage_cell H|V" 1>&2
  echo "#" 1>&2
  echo "#   The cell name supplied "$1".ap does not exist. Please check." 1>&2
  exit 1
fi

if [ "$#" -eq 2 ]
then
  echo "# Usage: pnr_strip_old_blockages cell blockage_cell H|V" 1>&2
  echo "#" 1>&2
  echo "#   The blockage_cell name must be supplied. Please check." 1>&2
  exit 1
fi

if test -f $2.ap
then
  block_cell=$2.ap
else
  echo "# Usage: pnr_strip_old_blockages cell blockage_cell H|V" 1>&2
  echo "#" 1>&2
  echo "#   The cell name supplied "$2".ap does not exist. Please check." 1>&2
  exit 1
fi

if [ "$#" -eq 2 ]
then
  echo "# Usage: pnr_strip_old_blockages cell blockage_cell H|V" 1>&2
  echo "#" 1>&2
  echo "#   Argument H or V must be given. Please check." 1>&2
  exit 1
else
  if [ "$3" = H ]
  then
    orient=H
  else
    if [ "$3" = V ]
    then
      orient=V
    else
      echo "# Usage: pnr_strip_old_blockages cell blockage_cell H|V" 1>&2
      echo "#"
      echo "#   Third argument must be H or V. Please check."
      exit 1
    fi
  fi
fi

# Blockage geometries identified by string "mosl"

cp $block_cell $$new_block
for layer in TALU2 TALU3 TALU4 TALU5
do
  if [ "$orient" = H ]
  then
    case $layer in
    TALU2)
      xdelta=1; ydelta=0; via=CONT_VIA,; via2=CONT_VIA2,
      ;;
    TALU3)
      xdelta=0; ydelta=1; via=CONT_VIA2,; via2=CONT_VIA3,
      ;;
    TALU4)
      xdelta=1; ydelta=0; via=CONT_VIA3,; via2=CONT_VIA4,
      ;;
    TALU5)
      xdelta=0; ydelta=1; via=CONT_VIA4,; via2=CONT_VIA5,
      ;;
    esac
  else
    case $layer in
    TALU2)
      xdelta=0; ydelta=1; via=CONT_VIA,; via2=CONT_VIA2,
      ;;
    TALU3)
      xdelta=1; ydelta=0; via=CONT_VIA2,; via2=CONT_VIA3,
      ;;
    TALU4)
      xdelta=0; ydelta=1; via=CONT_VIA3,; via2=CONT_VIA4,
      ;;
    TALU5)
      xdelta=1; ydelta=0; via=CONT_VIA4,; via2=CONT_VIA5,
      ;;
    esac
  fi

  block_coords=$(grep "$layer" $$new_block | \
    grep 'mosl' | \
    sed 's/ /,/' | \
    cut -f2,3 -d, | \
    sed 's/,/ /')

  index=0
  for point in $block_coords
  do
    let index=$index+1
#   x-coord is token 1,3,5 etc
#   y-coord is token 2,4,6 etc
    if [ "$index" -eq 1 ]
    then
      xblock=$point
    else
      yblock=$point

      let "xvia1=xblock-xdelta*scaled_pitch"
      let "yvia1=yblock-ydelta*scaled_pitch"
      let "xvia2=xblock+xdelta*scaled_pitch"
      let "yvia2=yblock+ydelta*scaled_pitch"

      count1=$(grep -c "V $xvia1,$yvia1,$via" $layout_cell)
      count2=$(grep -c "V $xvia1,$yvia1,$via2" $layout_cell)
      count3=$(grep -c "V $xvia2,$yvia2,$via" $layout_cell)
      count4=$(grep -c "V $xvia2,$yvia2,$via2" $layout_cell)
      let "count=count1+count2+count3+count4"
      if [ "$count" -eq 0 ]
      then
        sed -i "/S $xblock,$yblock,$xblock,$yblock,800,mosl,[^,][^,]*,$layer/ d" $$new_block
      fi
      index=0
    fi
  done #point in $block_coords
done #for layer in TALU2 TALU3 TALU4 TALU5

mv $$new_block $block_cell
