#!/bin/bash

# Script which adds a metal segment around each via
# with the extra overlap defined by the overlap
# variables below.
# The geometries are sorted at the end and duplicates removed.

# The ALL option adds metal-1 (ALU2) ovrlaps for technolgies
# which use ALU2 for metal-1 routing. This makes the script much
# slower, so should only be used when the final layout has
# nearly been achieved.

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)
    alu2_width=4; alu3_width=4; alu4_width=4; alu5_width=4; alu6_width=8
    alu2_pitch=8
    alu2_overlap_via1=1; alu2_overlap_via2=1
    alu3_overlap_via2=1; alu3_overlap_via3=1
    alu4_overlap_via3=1; alu4_overlap_via4=1
#   alu5_overlap_via4 set to 3 instead of 1 to ensure overlap for stacked vias
    alu5_overlap_via4=3; alu5_overlap_via5=3
    alu6_overlap_via5=1
    xoffset=0; yoffset=4
    type=s
    ;;
  ssxlib013|vsxlib013)
    alu2_width=4; alu3_width=4; alu4_width=4; alu5_width=4; alu6_width=8
    alu2_pitch=10
    alu2_overlap_via1=1; alu2_overlap_via2=1
    alu3_overlap_via2=1; alu3_overlap_via3=1
    alu4_overlap_via3=1; alu4_overlap_via4=1
#   alu5_overlap_via4 set to 3 instead of 1 to ensure overlap for stacked vias
    alu5_overlap_via4=3; alu5_overlap_via5=3
    alu6_overlap_via5=1
    xoffset=0; yoffset=0
    type=s
    ;;
  stxlib013|vtxlib013)
    alu2_width=4; alu3_width=4; alu4_width=4; alu5_width=4; alu6_width=4; alu7_width=4
    alu2_pitch=10
    alu2_overlap_via1=1; alu2_overlap_via2=1
    alu3_overlap_via2=1; alu3_overlap_via3=1
    alu4_overlap_via3=1; alu4_overlap_via4=1
    alu5_overlap_via4=1; alu5_overlap_via5=1
#   alu6_overlap_via4 set to 3 instead of 1 to ensure overlap for stacked vias
    alu6_overlap_via5=3; alu6_overlap_via6=3
    alu7_overlap_via6=1
    xoffset=0; yoffset=0
    type=t
    ;;
  sxlib100)
    alu2_width=2; alu3_width=2; alu4_width=2; alu5_width=2; alu6_width=4
    alu2_pitch=5
    alu2_overlap_via1=0.5; alu2_overlap_via2=0.5
    alu3_overlap_via2=0.5; alu3_overlap_via3=0.5
    alu4_overlap_via3=0.5; alu4_overlap_via4=0.5
#   alu5_overlap_via4 set to 1.5 instead of 0.5 to ensure overlap for stacked vias
    alu5_overlap_via4=1.5; alu5_overlap_via5=1.5
    alu6_overlap_via5=0.5
    xoffset=0; yoffset=0
    type=s
    ;;
  sxlib013|vxlib013)
    alu2_width=2; alu3_width=2; alu4_width=2; alu5_width=2; alu6_width=4
    alu2_pitch=5
    alu2_overlap_via1=0.5; alu2_overlap_via2=0.5
    alu3_overlap_via2=0.5; alu3_overlap_via3=0.5
    alu4_overlap_via3=0.5; alu4_overlap_via4=0.5
#   alu5_overlap_via4 set to 1.5 instead of 0.5 to ensure overlap for stacked vias
    alu5_overlap_via4=1.5; alu5_overlap_via5=1.5
    alu6_overlap_via5=0.5
    xoffset=0; yoffset=0
    type=s
    ;;
  vgalib013)
    alu2_width=4; alu3_width=4; alu4_width=4; alu5_width=4; alu6_width=8
    alu2_pitch=8
    alu2_overlap_via1=1; alu2_overlap_via2=1
    alu3_overlap_via2=1; alu3_overlap_via3=1
    alu4_overlap_via3=1; alu4_overlap_via4=1
#   alu5_overlap_via4 set to 3 instead of 1 to ensure overlap for stacked vias
    alu5_overlap_via4=3; alu5_overlap_via5=3
    alu6_overlap_via5=1
    xoffset=0; yoffset=0
    type=s
    ;;
  vsclib013)
    alu2_width=4; alu3_width=4; alu4_width=4; alu5_width=4; alu6_width=8
    alu2_pitch=8
    alu2_overlap_via1=1; alu2_overlap_via2=1
    alu3_overlap_via2=1; alu3_overlap_via3=1
    alu4_overlap_via3=1; alu4_overlap_via4=1
#   alu5_overlap_via4 set to 3 instead of 1 to ensure overlap for stacked vias
    alu5_overlap_via4=3; alu5_overlap_via5=3
    alu6_overlap_via5=1
    xoffset=4; yoffset=0
    type=s
    ;;
  vtclib013)
    alu2_width=4; alu3_width=4; alu4_width=4; alu5_width=4; alu6_width=4; alu7_width=8
    alu2_pitch=8
    alu2_overlap_via1=1; alu2_overlap_via2=1
    alu3_overlap_via2=1; alu3_overlap_via3=1
    alu4_overlap_via3=1; alu4_overlap_via4=1
    alu5_overlap_via4=1; alu5_overlap_via5=1
#   alu6_overlap_via4 set to 3 instead of 1 to ensure overlap for stacked vias
    alu6_overlap_via5=3; alu6_overlap_via6=3
    alu7_overlap_via6=1
    xoffset=4; yoffset=0
    type=t
    ;;
  wsclib013)
    alu2_width=4; alu3_width=4; alu4_width=4; alu5_width=4; alu6_width=8
    alu2_pitch=8
    alu2_overlap_via1=1; alu2_overlap_via2=1
    alu3_overlap_via2=1; alu3_overlap_via3=1
    alu4_overlap_via3=1; alu4_overlap_via4=1
#   alu5_overlap_via4 set to 3 instead of 1 to ensure overlap for stacked vias
    alu5_overlap_via4=3; alu5_overlap_via5=3
    alu6_overlap_via5=1
    xoffset=4; yoffset=4
    type=s
    ;;
  wtclib013)
    alu2_width=4; alu3_width=4; alu4_width=4; alu5_width=4; alu6_width=8
    alu2_pitch=8
    alu2_overlap_via1=1; alu2_overlap_via2=1
    alu3_overlap_via2=1; alu3_overlap_via3=1
    alu4_overlap_via3=1; alu4_overlap_via4=1
    alu5_overlap_via4=1; alu5_overlap_via5=1
#   alu6_overlap_via4 set to 3 instead of 1 to ensure overlap for stacked vias
    alu6_overlap_via5=3; alu6_overlap_via6=3
    alu7_overlap_via6=1
    xoffset=4; yoffset=4
    type=t
    ;;
  *)
    echo "#?! Library "$library" not supported yet. Please check." 1>&2
    exit 1
    ;;
  esac
fi

if [ "$#" -eq 0 ]
then
  echo "# Usage: pnr_via_do_overlap cell H|V [ALL]" 1>&2
  echo "#" 1>&2
  echo "#   will add metal rectangles around each via to ensure that" 1>&2
  echo "#   there is a 2 lambda end overlap. The second argument will" 1>&2
  echo "#   set the preferred direction of ALU2." 1>&2
  echo "#   eg  pnr_via_do_overlap fred H  will add ALU2 rectangles horizontally," 1>&2
  echo "#   ALU3 rectangles vertically etc up to ALU6." 1>&2
  exit 1
fi

if test -f $1.ap
then
  cell=$1.ap
else
  echo "# Usage: pnr_via_do_overlap 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 1 ]
then
  echo "# Usage: pnr_via_do_overlap cell H|V" 1>&2
  echo "#" 1>&2
  echo "#   Argument H or V must be given. Please check." 1>&2
  exit 1
else
  if [ "$2" = H ]
  then
    orient=H
    echo "# Horizontal ALU2"
  else
    if [ "$2" = V ]
    then
      orient=V
      echo "# Vertical ALU2"
    else
      echo "# Usage: pnr_via_do_overlap cell H|V"
      echo "#"
      echo "#   Second argument must be H or V. Please check."
      exit 1
    fi
  fi
fi

if [ "$#" -eq 2 ]
then
  extent=UPPER
elif [ "$3" = ALL -o "$3" = all ]
then
  extent=ALL
else
  extent=UPPER
fi

#                cell    ,      P      ,     date    , scale
scale=$(grep '^H ' $cell | sed 's/^H  *\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\) *$/\4/')
let "scaled_xoffset=xoffset*scale"
let "scaled_yoffset=yoffset*scale"

let "scaled_alu2_width=alu2_width*$scale"
let "scaled_alu2_pitch=alu2_pitch*scale"
scaled_alu2_overlap_via1=$(echo $alu2_overlap_via1" "$scale | awk '{print $1*$2 }')

let "scaled_alu3_width=alu3_width*$scale"
scaled_alu2_overlap_via2=$(echo $alu2_overlap_via2" "$scale | awk '{print $1*$2 }')
scaled_alu3_overlap_via2=$(echo $alu3_overlap_via2" "$scale | awk '{print $1*$2 }')

let "scaled_alu4_width=alu4_width*$scale"
scaled_alu3_overlap_via3=$(echo $alu3_overlap_via3" "$scale | awk '{print $1*$2 }')
scaled_alu4_overlap_via3=$(echo $alu4_overlap_via3" "$scale | awk '{print $1*$2 }')

let "scaled_alu5_width=alu5_width*$scale"
scaled_alu4_overlap_via4=$(echo $alu4_overlap_via4" "$scale | awk '{print $1*$2 }')
scaled_alu5_overlap_via4=$(echo $alu5_overlap_via4" "$scale | awk '{print $1*$2 }')

let "scaled_alu6_width=alu6_width*$scale"
scaled_alu5_overlap_via5=$(echo $alu5_overlap_via5" "$scale | awk '{print $1*$2 }')
scaled_alu6_overlap_via5=$(echo $alu6_overlap_via5" "$scale | awk '{print $1*$2 }')

if [ "$type" = s ]
then
  if [ "$orient" = H ]
  then

    via=,CONT_VIA,
#                                        1   2   3   4   5   6   2   3   2   3   w   5   6   e
    awk -v "FS= ||," '/'$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
    $1,$2,$3,$4,$5,$6 RS \
    "S",$2-'$scaled_alu2_overlap_via1',$3,$2+'$scaled_alu2_overlap_via1',$3,'$scaled_alu2_width',$5,$6,"LEFT,ALU2" }
    !/'$via'/ {print}' $cell > $$temp

    via=,CONT_VIA2,
#                                        1   2   3   4   5   6   2   3   2   3   w   5   6   e   2   3   2   3   w   5   6   e
    awk -v "FS= ||," '/'$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
    $1,$2,$3,$4,$5,$6 RS \
    "S",$2-'$scaled_alu2_overlap_via2',$3,$2+'$scaled_alu2_overlap_via2',$3,'$scaled_alu2_width',$5,$6,"LEFT,ALU2" RS \
    "S",$2,$3-'$scaled_alu3_overlap_via2',$2,$3+'$scaled_alu3_overlap_via2','$scaled_alu3_width',$5,$6,"UP,ALU3" }
    !/'$via'/ {print}' $$temp > $$temp1

    via=,CONT_VIA3,
#                                        1   2   3   4   5   6   2   3   2   3   w   5   6   e   2   3   2   3   w   5   6   e
    awk -v "FS= ||," '/'$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
    $1,$2,$3,$4,$5,$6 RS \
    "S",$2-'$scaled_alu4_overlap_via3',$3,$2+'$scaled_alu4_overlap_via3',$3,'$scaled_alu4_width',$5,$6,"LEFT,ALU4" RS \
    "S",$2,$3-'$scaled_alu3_overlap_via3',$2,$3+'$scaled_alu3_overlap_via3','$scaled_alu3_width',$5,$6,"UP,ALU3" }
    !/'$via'/ {print}' $$temp1 > $$temp

    via=,CONT_VIA4,
#                                        1   2   3   4   5   6   2   3   2   3   w   5   6   e   2   3   2   3   w   5   6   e
    awk -v "FS= ||," '/'$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
    $1,$2,$3,$4,$5,$6 RS \
    "S",$2-'$scaled_alu4_overlap_via4',$3,$2+'$scaled_alu4_overlap_via4',$3,'$scaled_alu4_width',$5,$6,"LEFT,ALU4" RS \
    "S",$2,$3-'$scaled_alu5_overlap_via4',$2,$3+'$scaled_alu5_overlap_via4','$scaled_alu5_width',$5,$6,"UP,ALU5" }
    !/'$via'/ {print}' $$temp > $$temp1

  else

    via=,CONT_VIA,
#                                        1   2   3   4   5   6   2   3   2   3   w   5   6   e
    awk -v "FS= ||," '/'$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
    $1,$2,$3,$4,$5,$6 RS \
    "S",$2,$3-'$scaled_alu2_overlap_via1',$2,$3+'$scaled_alu2_overlap_via1','$scaled_alu2_width',$5,$6,"UP,ALU2" }
    !/'$via'/ {print}' $cell | sed '/^S / s/  *,/,/' > $$temp

    via=,CONT_VIA2,
#                                        1   2   3   4   5   6   2   3   2   3   w   5   6   e   2   3   2   3   w   5   6   e
    awk -v "FS= ||," '/'$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
    $1,$2,$3,$4,$5,$6 RS \
    "S",$2-'$scaled_alu3_overlap_via2',$3,$2+'$scaled_alu3_overlap_via2',$3,'$scaled_alu3_width',$5,$6,"LEFT,ALU3" RS \
    "S",$2,$3-'$scaled_alu2_overlap_via2',$2,$3+'$scaled_alu2_overlap_via2','$scaled_alu2_width',$5,$6,"UP,ALU2" }
    !/'$via'/ {print}' $$temp | sed '/^S / s/  *,/,/' > $$temp1

    via=,CONT_VIA3,
#                                        1   2   3   4   5   6   2   3   2   3   w   5   6   e   2   3   2   3   w   5   6   e
    awk -v "FS= ||," '/'$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
    $1,$2,$3,$4,$5,$6 RS \
    "S",$2-'$scaled_alu3_overlap_via3',$3,$2+'$scaled_alu3_overlap_via3',$3,'$scaled_alu3_width',$5,$6,"LEFT,ALU3" RS \
    "S",$2,$3-'$scaled_alu4_overlap_via3',$2,$3+'$scaled_alu4_overlap_via3','$scaled_alu4_width',$5,$6,"UP,ALU4" }
    !/'$via'/ {print}' $$temp1 > $$temp

    via=,CONT_VIA4,
#                                        1   2   3   4   5   6   2   3   2   3   w   5   6   e   2   3   2   3   w   5   6   e
    awk -v "FS= ||," '/'$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
    $1,$2,$3,$4,$5,$6 RS \
    "S",$2-'$scaled_alu5_overlap_via4',$3,$2+'$scaled_alu5_overlap_via4',$3,'$scaled_alu5_width',$5,$6,"LEFT,ALU5" RS \
    "S",$2,$3-'$scaled_alu4_overlap_via4',$2,$3+'$scaled_alu4_overlap_via4','$scaled_alu4_width',$5,$6,"UP,ALU4" }
    !/'$via'/ {print}' $$temp > $$temp1

  fi
#
# Special treatment for ALU6 (=metal-6)
#
  if [ "$extent" = ALL ]
  then
    via=,CONT_VIA5,
    via5_coords=$(grep ',CONT_VIA5,' $cell | \
      sed 's/^V  *\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\) *$/\1,\2/')

    for via5 in $via5_coords
    do
      alu5_dir=RIGHT
      alu6_dir=UP
      alu6_coords=$(grep ',ALU6' $cell | \
        sed 's/^S  *\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\) *$/\1,\2 \3,\4/')
      for alu6_segment in $alu6_coords
      do
        if [ "$alu6_segment" != "$via5" ]
        then
          continue
        else
          alu5_coords=$(grep ',ALU5' $cell | \
            sed 's/^S  *\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\) *$/\1,\2 \3,\4/')
          for alu5_segment in $alu5_coords
          do
            if [ "$alu5_segment" != "$via5" ]
            then
              continue
            else
#             have found the ALU5 and ALU6 segments which meet at this VIA5
              alu5_dir=$(grep ',ALU5' $cell | \
                grep "$alu5_segment" | \
                sed 's/^S  *\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\) *$/\7/')
              alu6_dir=$(grep ',ALU6' $cell | \
                grep "$alu6_segment" | \
                sed 's/^S  *\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\) *$/\7/')

              if [ "$alu5_dir" = LEFT -o "$alu5_dir" = RIGHT ]
              then
                if [ "$alu6_dir" = LEFT -o "$alu6_dir" = RIGHT ]
                then
#                                                             1   2   3   4   5   6   2   3   2   3   w   5   6   e   2   3   2   3   w   5   6   e
                  awk -v "FS= ||," '/ '$via5''$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
                  $1,$2,$3,$4,$5,$6 RS \
                  "S",$2-'$scaled_alu6_overlap_via5',$3,$2+'$scaled_alu6_overlap_via5',$3,'$scaled_alu6_width',$5,$6,"LEFT,ALU6" RS \
                  "S",$2-'$scaled_alu5_overlap_via5',$3,$2+'$scaled_alu5_overlap_via5',$3,'$scaled_alu6_width',$5,$6,"LEFT,ALU5" }
                  !/ '$via5''$via'/ {print}' $$temp1 > $$temp
                  cp $$temp $$temp1
                elif [ "$alu6_dir" = UP -o "$alu6_dir" = DOWN ]
                then
#                                                             1   2   3   4   5   6   2   3   2   3   w   5   6   e   2   3   2   3   w   5   6   e
                  awk -v "FS= ||," '/ '$via5''$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
                  $1,$2,$3,$4,$5,$6 RS \
                  "S",$2-'$scaled_alu5_overlap_via5',$3,$2+'$scaled_alu5_overlap_via5',$3,'$scaled_alu6_width',$5,$6,"LEFT,ALU5" RS \
                  "S",$2,$3-'$scaled_alu6_overlap_via5',$2,$3+'$scaled_alu6_overlap_via5','$scaled_alu6_width',$5,$6,"UP,ALU6" }
                  !/ '$via5''$via'/ {print}' $$temp1 > $$temp
                  cp $$temp $$temp1
                fi
              elif [ "$alu5_dir" = UP -o "$alu5_dir" = DOWN ]
              then
                if [ "$alu6_dir" = LEFT -o "$alu6_dir" = RIGHT ]
                then
#                                                             1   2   3   4   5   6   2   3   2   3   w   5   6   e   2   3   2   3   w   5   6   e
                  awk -v "FS= ||," '/ '$via5''$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
                  $1,$2,$3,$4,$5,$6 RS \
                  "S",$2-'$scaled_alu6_overlap_via5',$3,$2+'$scaled_alu6_overlap_via5',$3,'$scaled_alu6_width',$5,$6,"LEFT,ALU6" RS \
                  "S",$2,$3-'$scaled_alu5_overlap_via5',$2,$3+'$scaled_alu5_overlap_via5','$scaled_alu6_width',$5,$6,"UP,ALU5" }
                  !/ '$via5''$via'/ {print}' $$temp1 > $$temp
                  cp $$temp $$temp1
                elif [ "$alu6_dir" = UP -o "$alu6_dir" = DOWN ]
                then
#                                                             1   2   3   4   5   6   2   3   2   3   w   5   6   e   2   3   2   3   w   5   6   e
                  awk -v "FS= ||," '/ '$via5''$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
                  $1,$2,$3,$4,$5,$6 RS \
                  "S",$2,$3-'$scaled_alu6_overlap_via5',$2,$3+'$scaled_alu6_overlap_via5','$scaled_alu6_width',$5,$6,"UP,ALU6" RS \
                  "S",$2,$3-'$scaled_alu5_overlap_via5',$2,$3+'$scaled_alu5_overlap_via5','$scaled_alu6_width',$5,$6,"UP,ALU5" }
                  !/ '$via5''$via'/ {print}' $$temp1 > $$temp
                  cp $$temp $$temp1
                fi
              fi
              break
            fi
          done # looped thru all ALU5 segments
        fi
      done # looped thru all ALU6 segments
    done # looped thru all the CONT_VIA5's
  fi

elif [ "$type" = t ]
then

  let "scaled_alu7_width=alu7_width*scale"
  scaled_alu6_overlap_via6=$(echo $alu6_overlap_via6" "$scale | awk '{print $1*$2 }')
  scaled_alu7_overlap_via6=$(echo $alu7_overlap_via6" "$scale | awk '{print $1*$2 }')

  if [ "$orient" = V ]
  then
#   CONT_VIA not used with this library design
#    via=,CONT_VIA,
#                                        1   2   3   4   5   6   2   3   2   3   w   5   6   e
#    awk -v "FS= ||," '/'$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
#    $1,$2,$3,$4,$5,$6 RS \
#    "S",$2-'$scaled_alu2_overlap_via1',$3,$2+'$scaled_alu2_overlap_via1',$3,'$scaled_alu2_width',$5,$6,"LEFT,ALU2" }
#    !/'$via'/ {print}' $cell > $$temp
    cp $cell $$temp

    via=,CONT_VIA2,
#                                        1   2   3   4   5   6   2   3   2   3   w   5   6   e
    awk -v "FS= ||," '/'$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
    $1,$2,$3,$4,$5,$6 RS \
    "S",$2,$3-'$scaled_alu3_overlap_via2',$2,$3+'$scaled_alu3_overlap_via2','$scaled_alu3_width',$5,$6,"UP,ALU3" }
    !/'$via'/ {print}' $$temp > $$temp1

    via=,CONT_VIA3,
#                                        1   2   3   4   5   6   2   3   2   3   w   5   6   e   2   3   2   3   w   5   6   e
    awk -v "FS= ||," '/'$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
    $1,$2,$3,$4,$5,$6 RS \
    "S",$2-'$scaled_alu4_overlap_via3',$3,$2+'$scaled_alu4_overlap_via3',$3,'$scaled_alu4_width',$5,$6,"LEFT,ALU4" RS \
    "S",$2,$3-'$scaled_alu3_overlap_via3',$2,$3+'$scaled_alu3_overlap_via3','$scaled_alu3_width',$5,$6,"UP,ALU3" }
    !/'$via'/ {print}' $$temp1 > $$temp

    via=,CONT_VIA4,
#                                        1   2   3   4   5   6   2   3   2   3   w   5   6   e   2   3   2   3   w   5   6   e
    awk -v "FS= ||," '/'$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
    $1,$2,$3,$4,$5,$6 RS \
    "S",$2-'$scaled_alu4_overlap_via4',$3,$2+'$scaled_alu4_overlap_via4',$3,'$scaled_alu4_width',$5,$6,"LEFT,ALU4" RS \
    "S",$2,$3-'$scaled_alu5_overlap_via4',$2,$3+'$scaled_alu5_overlap_via4','$scaled_alu5_width',$5,$6,"UP,ALU5" }
    !/'$via'/ {print}' $$temp > $$temp1

    via=,CONT_VIA5,
#                                        1   2   3   4   5   6   2   3   2   3   w   5   6   e   2   3   2   3   w   5   6   e
    awk -v "FS= ||," '/'$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
    $1,$2,$3,$4,$5,$6 RS \
    "S",$2-'$scaled_alu6_overlap_via5',$3,$2+'$scaled_alu6_overlap_via5',$3,'$scaled_alu6_width',$5,$6,"LEFT,ALU6" RS \
    "S",$2,$3-'$scaled_alu5_overlap_via5',$2,$3+'$scaled_alu5_overlap_via5','$scaled_alu5_width',$5,$6,"UP,ALU5" }
    !/'$via'/ {print}' $$temp1 > $$temp

  else

#    via=,CONT_VIA,
#                                        1   2   3   4   5   6   2   3   2   3   w   5   6   e
#    awk -v "FS= ||," '/'$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
#    $1,$2,$3,$4,$5,$6 RS \
#    "S",$2,$3-'$scaled_alu2_overlap_via1',$2,$3+'$scaled_alu2_overlap_via1','$scaled_alu2_width',$5,$6,"UP,ALU2" }
#    !/'$via'/ {print}' $cell | sed '/^S / s/  *,/,/' > $$temp
    cp $cell $$temp

    via=,CONT_VIA2,
#                                        1   2   3   4   5   6   2   3   2   3   w   5   6   e
    awk -v "FS= ||," '/'$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
    $1,$2,$3,$4,$5,$6 RS \
    "S",$2-'$scaled_alu3_overlap_via2',$3,$2+'$scaled_alu3_overlap_via2',$3,'$scaled_alu3_width',$5,$6,"LEFT,ALU3" }
    !/'$via'/ {print}' $$temp | sed '/^S / s/  *,/,/' > $$temp1

    via=,CONT_VIA3,
#                                        1   2   3   4   5   6   2   3   2   3   w   5   6   e   2   3   2   3   w   5   6   e
    awk -v "FS= ||," '/'$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
    $1,$2,$3,$4,$5,$6 RS \
    "S",$2-'$scaled_alu3_overlap_via3',$3,$2+'$scaled_alu3_overlap_via3',$3,'$scaled_alu3_width',$5,$6,"LEFT,ALU3" RS \
    "S",$2,$3-'$scaled_alu4_overlap_via3',$2,$3+'$scaled_alu4_overlap_via3','$scaled_alu4_width',$5,$6,"UP,ALU4" }
    !/'$via'/ {print}' $$temp1 > $$temp

    via=,CONT_VIA4,
#                                        1   2   3   4   5   6   2   3   2   3   w   5   6   e   2   3   2   3   w   5   6   e
    awk -v "FS= ||," '/'$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
    $1,$2,$3,$4,$5,$6 RS \
    "S",$2-'$scaled_alu5_overlap_via4',$3,$2+'$scaled_alu5_overlap_via4',$3,'$scaled_alu5_width',$5,$6,"LEFT,ALU5" RS \
    "S",$2,$3-'$scaled_alu4_overlap_via4',$2,$3+'$scaled_alu4_overlap_via4','$scaled_alu4_width',$5,$6,"UP,ALU4" }
    !/'$via'/ {print}' $$temp > $$temp1

    via=,CONT_VIA5,
#                                        1   2   3   4   5   6   2   3   2   3   w   5   6   e   2   3   2   3   w   5   6   e
    awk -v "FS= ||," '/'$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
    $1,$2,$3,$4,$5,$6 RS \
    "S",$2-'$scaled_alu5_overlap_via5',$3,$2+'$scaled_alu5_overlap_via5',$3,'$scaled_alu5_width',$5,$6,"LEFT,ALU5" RS \
    "S",$2,$3-'$scaled_alu6_overlap_via5',$2,$3+'$scaled_alu6_overlap_via5','$scaled_alu6_width',$5,$6,"UP,ALU6" }
    !/'$via'/ {print}' $$temp1 > $$temp

  fi
  cp $$temp $$temp1
#
# Special treatment for ALU7 (=metal-6)
#
  via=,CONT_VIA6,
  via6_coords=$(grep "$via" $cell | \
    sed 's/^V  *\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\) *$/\1,\2/')

  for via6 in $via6_coords
  do
    alu6_dir=RIGHT
    alu7_dir=UP
    alu7_coords=$(grep ',ALU7' $cell | \
      sed 's/^S  *\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\) *$/\1,\2 \3,\4/')
    for alu7_segment in $alu7_coords
    do
      if [ "$alu7_segment" != "$via6" ]
      then
        continue
      else
        alu6_coords=$(grep ',ALU6' $cell | \
          sed 's/^S  *\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\) *$/\1,\2 \3,\4/')
        for alu6_segment in $alu6_coords
        do
          if [ "$alu6_segment" != "$via6" ]
          then
            continue
          else
#           have found the ALU6 and ALU7 segments which meet at this VIA6
            alu6_dir=$(grep ',ALU6' $cell | \
              grep "$alu6_segment" | \
              sed 's/^S  *\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\) *$/\7/')
            alu7_dir=$(grep ',ALU7' $cell | \
              grep "$alu7_segment" | \
              sed 's/^S  *\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\) *$/\7/')

            if [ "$alu6_dir" = LEFT -o "$alu6_dir" = RIGHT ]
            then
              if [ "$alu7_dir" = LEFT -o "$alu7_dir" = RIGHT ]
              then
#                                                           1   2   3   4   5   6   2   3   2   3   w   5   6   e   2   3   2   3   w   5   6   e
                awk -v "FS= ||," '/ '$via6''$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
                $1,$2,$3,$4,$5,$6 RS \
                "S",$2-'$scaled_alu7_overlap_via6',$3,$2+'$scaled_alu7_overlap_via6',$3,'$scaled_alu7_width',$5,$6,"LEFT,ALU7" RS \
                "S",$2-'$scaled_alu6_overlap_via6',$3,$2+'$scaled_alu6_overlap_via6',$3,'$scaled_alu7_width',$5,$6,"LEFT,ALU6" }
                !/ '$via6''$via'/ {print}' $$temp1 > $$temp
                cp $$temp $$temp1
              elif [ "$alu7_dir" = UP -o "$alu7_dir" = DOWN ]
              then
#                                                           1   2   3   4   5   6   2   3   2   3   w   5   6   e   2   3   2   3   w   5   6   e
                awk -v "FS= ||," '/ '$via6''$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
                $1,$2,$3,$4,$5,$6 RS \
                "S",$2-'$scaled_alu6_overlap_via6',$3,$2+'$scaled_alu6_overlap_via6',$3,'$scaled_alu7_width',$5,$6,"LEFT,ALU6" RS \
                "S",$2,$3-'$scaled_alu7_overlap_via6',$2,$3+'$scaled_alu7_overlap_via6','$scaled_alu7_width',$5,$6,"UP,ALU7" }
                !/ '$via6''$via'/ {print}' $$temp1 > $$temp
                cp $$temp $$temp1
              fi
            elif [ "$alu6_dir" = UP -o "$alu6_dir" = DOWN ]
            then
              if [ "$alu7_dir" = LEFT -o "$alu7_dir" = RIGHT ]
              then
#                                                           1   2   3   4   5   6   2   3   2   3   w   5   6   e   2   3   2   3   w   5   6   e
                awk -v "FS= ||," '/ '$via6''$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
                $1,$2,$3,$4,$5,$6 RS \
                "S",$2-'$scaled_alu7_overlap_via6',$3,$2+'$scaled_alu7_overlap_via6',$3,'$scaled_alu7_width',$5,$6,"LEFT,ALU7" RS \
                "S",$2,$3-'$scaled_alu6_overlap_via6',$2,$3+'$scaled_alu6_overlap_via6','$scaled_alu7_width',$5,$6,"UP,ALU6" }
                !/ '$via6''$via'/ {print}' $$temp1 > $$temp
                cp $$temp $$temp1
              elif [ "$alu7_dir" = UP -o "$alu7_dir" = DOWN ]
              then
#                                                           1   2   3   4   5   6   2   3   2   3   w   5   6   e   2   3   2   3   w   5   6   e
                awk -v "FS= ||," '/ '$via6''$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
                $1,$2,$3,$4,$5,$6 RS \
                "S",$2,$3-'$scaled_alu7_overlap_via6',$2,$3+'$scaled_alu7_overlap_via6','$scaled_alu7_width',$5,$6,"UP,ALU7" RS \
                "S",$2,$3-'$scaled_alu6_overlap_via6',$2,$3+'$scaled_alu6_overlap_via6','$scaled_alu7_width',$5,$6,"UP,ALU6" }
                !/ '$via6''$via'/ {print}' $$temp1 > $$temp
                cp $$temp $$temp1
              fi
            fi
            break
          fi
        done # looped thru all ALU6 segments
      fi
     done # looped thru all ALU7 segments
  done # looped thru all the CONT_VIA6's

  if [ "$extent" = ALL ]
  then
#
# Special treatment for ALU2 (=metal-1)
#
  via=,CONT_VIA2,
  via2_coords=$(grep "$via" $cell | \
    sort -u | \
    sed 's/^V  *\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\) *$/\1,\2/')

  grep "^S .*,TALU2 *$" $cell > $$talu2_segments

  let "scaled_talu2_width=scaled_alu2_width"
  grep "^S  *[^,][^,]*,[^,][^,]*,[^,][^,]*,[^,][^,]*,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $cell > $$talu2_04

  let "scaled_talu2_width=scaled_talu2_width+scaled_alu2_pitch"
  grep "^S  *[^,][^,]*,[^,][^,]*,[^,][^,]*,[^,][^,]*,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $cell > $$talu2_12

  let "scaled_talu2_width=scaled_talu2_width+scaled_alu2_pitch"
  grep "^S  *[^,][^,]*,[^,][^,]*,[^,][^,]*,[^,][^,]*,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $cell > $$talu2_20

  let "scaled_talu2_width=scaled_talu2_width+scaled_alu2_pitch"
  grep "^S  *[^,][^,]*,[^,][^,]*,[^,][^,]*,[^,][^,]*,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $cell > $$talu2_28

  let "scaled_talu2_width=scaled_talu2_width+scaled_alu2_pitch"
  grep "^S  *[^,][^,]*,[^,][^,]*,[^,][^,]*,[^,][^,]*,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $cell > $$talu2_36

  let "scaled_talu2_width=scaled_talu2_width+scaled_alu2_pitch"
  grep "^S  *[^,][^,]*,[^,][^,]*,[^,][^,]*,[^,][^,]*,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $cell > $$talu2_44

  let "scaled_talu2_width=scaled_talu2_width+scaled_alu2_pitch"
  grep "^S  *[^,][^,]*,[^,][^,]*,[^,][^,]*,[^,][^,]*,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $cell > $$talu2_52

  for via2 in $via2_coords
  do
    if test -e talu2_blockages
    then
#     Use this file to determine whether to add ALU2 overlap around via
      let "xvia2=($(echo "$via2" | cut -f1 -d,)-scaled_xoffset)/scaled_alu2_pitch"
      let "yvia2=($(echo "$via2" | cut -f2 -d,)-scaled_yoffset)/scaled_alu2_pitch"
      block_count=$(grep -c "^$xvia2  *$yvia2 *$" talu2_blockages)
      if [ "$block_count" -eq 0 ]
      then
        alu2_dir1=$(grep "^S  *$via2,[^,][^,]*,[^,][^,]*,[^,][^,]*,[^,][^,]*,[^,][^,]*,[C]*ALU2 *$" $cell | \
          sed "s/^S  *$via2,\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),[C]*ALU2 *$/\5/" )
        alu2_dir2=$(grep "^S  *[^,][^,]*,[^,][^,]*,$via2,[^,][^,]*,[^,][^,]*,[^,][^,]*,[C]*ALU2 *$" $cell | \
          sed "s/^S  *\([^,][^,]*\),\([^,][^,]*\),$via2,\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),[C]*ALU2 *$/\5/" )
        if [ "$alu2_dir1" = UP -o "$alu2_dir1" = DOWN ]
        then
          alu2_dir=$alu2_dir1
        elif [ "$alu2_dir2" = UP -o "$alu2_dir2" = DOWN ]
        then
          alu2_dir=$alu2_dir2
        elif [ "$alu2_dir1" = LEFT -o "$alu2_dir1" = RIGHT ]
        then
          alu2_dir=$alu2_dir1
        elif [ "$alu2_dir2" = LEFT -o "$alu2_dir2" = RIGHT ]
        then
          alu2_dir=$alu2_dir2
        else
          alu2_dir=NONE
        fi

        if [ "$alu2_dir" = LEFT -o "$alu2_dir" = RIGHT ]
        then
#         This ALU2 segment needs extending in an EAST-WEST direction
#                                                      1   2   3   4   5   6   2   3   2   3   w   5   6   e
          awk -v "FS= ||," '/ '$via2''$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
          $1,$2,$3,$4,$5,$6 RS \
          "S",$2-'$scaled_alu2_overlap_via2',$3,$2+'$scaled_alu2_overlap_via2',$3,'$scaled_alu2_width',$5,$6,"LEFT,ALU2" }
          !/ '$via2''$via'/ {print}' $$temp1 > $$temp
          cp $$temp $$temp1
        elif [ "$alu2_dir" = UP -o "$alu2_dir" = DOWN ]
        then
#         This ALU2 segment needs extending in a NORTH-SOUTH direction
#                                                      1   2   3   4   5   6   2   3   2   3   w   5   6   e
          awk -v "FS= ||," '/ '$via2''$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
          $1,$2,$3,$4,$5,$6 RS \
          "S",$2,$3-'$scaled_alu2_overlap_via2',$2,$3+'$scaled_alu2_overlap_via2','$scaled_alu2_width',$5,$6,"UP,ALU2" }
          !/ '$via2''$via'/ {print}' $$temp1 > $$temp
          cp $$temp $$temp1
        fi
      fi
    else
#     Without talu2_blockages file use TALU2 geometries and labels to determine ALU2 overlap
      for metal in ,ALU2 ,CALU2
      do
        alu2_count1=$(grep -c "^S  *$via2,\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\)$metal *$" $cell)
        alu2_count2=$(grep -c "^S  *\([^,][^,]*\),\([^,][^,]*\),$via2,\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\)$metal *$" $cell)
        let "alu2_count=alu2_count1+alu2_count2"
        if [ "$alu2_count" -gt 0 ]
        then
#         This via2 has an ALU2 segment terminating on it which might need extending
#         If metal = ALU2, there is no extension if
#           (i)  there is a TALU2 segment labelled obs and
#           (ii) there is a TALU2 segment labelled *.
#         If metal = CALU2, there is no extension if
#           (i)  there is a TALU2 segment (any label)
#
          alu2_dir1=$(grep "^S  *$via2,[^,][^,]*,[^,][^,]*,[^,][^,]*,[^,][^,]*,[^,][^,]*$metal *$" $cell | \
            sed "s/^S  *$via2,\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\)$metal *$/\5/" )
          alu2_dir2=$(grep "^S  *[^,][^,]*,[^,][^,]*,$via2,[^,][^,]*,[^,][^,]*,[^,][^,]*$metal *$" $cell | \
            sed "s/^S  *\([^,][^,]*\),\([^,][^,]*\),$via2,\([^,][^,]*\),\([^,][^,]*\),\([^,][^,]*\)$metal *$/\5/" )
          if [ "$alu2_dir1" = UP -o "$alu2_dir1" = DOWN ]
          then
            alu2_dir=$alu2_dir1
          elif [ "$alu2_dir2" = UP -o "$alu2_dir2" = DOWN ]
          then
            alu2_dir=$alu2_dir2
          elif [ "$alu2_dir1" = LEFT -o "$alu2_dir1" = RIGHT ]
          then
            alu2_dir=$alu2_dir1
          elif [ "$alu2_dir2" = LEFT -o "$alu2_dir2" = RIGHT ]
          then
            alu2_dir=$alu2_dir2
          else
            alu2_dir=UP
          fi

          obs1=$(grep -c "^S $via2,\([^,][^,]*\),\([^,][^,]*\),$scaled_alu2_width,obs,\([^,][^,]*\),TALU2 *$" $$talu2_segments)
          obs2=$(grep -c "^S \([^,][^,]*\),\([^,][^,]*\),$via2,$scaled_alu2_width,obs,\([^,][^,]*\),TALU2 *$" $$talu2_segments)
          let "obs=obs1+obs2"
          none=0
          if [ "$obs" -gt 0 -a "$metal" = ,ALU2 -o "$obs" -eq 0 -a "$metal" = ,CALU2 ]
          then
#           Scan TALU2 to see whether a segment labelled '*' is at y=yvia2
#           Then vary yvia2 to account for wide TALU2 blockages
#
            xvia2=$(echo "$via2" | cut -f1 -d,)
            yvia2=$(echo "$via2" | cut -f2 -d,)
#
#           x1x2_10 is the min and max x-values for each TALU2 segment whose y coord matches the CONT_VIA2
#
#           1 track wide TALU2
            let "scaled_talu2_width=scaled_alu2_width"
            yvia2_lo=$yvia2
            x1x2_10=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_04 | \
              sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")

            for xloc in $x1x2_10
            do
              xmin=$(echo "$xloc" | cut -f1 -d,)
              xmax=$(echo "$xloc" | cut -f2 -d,)
#
#             xmin and xmax are the 2 x coords of a TALU2 segment on the same y coord as the CONT_VIA2
#
              if [ "$xmin" -le "$xvia2" -a "$xmax" -ge "$xvia2" \
               -o  "$xmin" -ge "$xvia2" -a "$xmax" -le "$xvia2" ]
              then
#               CONT_VIA2 lies above this TALU2 segment
#
                none=1
                break
              fi
            done # looped thru all 4 lambda TALU2 segments at y=y(CONT_VIA2)

            if [ "$none" -eq 0 ]
            then
#             2 track wide TALU2
              let "scaled_talu2_width=scaled_talu2_width+scaled_alu2_pitch"
              let "yvia2_lo=yvia2_lo-scaled_alu2_pitch/2"
              let "yvia2=yvia2_lo"
              x1x2_20=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_12 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")
              let "yvia2=yvia2+scaled_alu2_pitch"
              x1x2_21=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_12 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")

#             3 track wide TALU2
              let "scaled_talu2_width=scaled_talu2_width+scaled_alu2_pitch"
              let "yvia2_lo=yvia2_lo-scaled_alu2_pitch/2"
              let "yvia2=yvia2_lo"
              x1x2_30=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_20 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")
              let "yvia2=yvia2+scaled_alu2_pitch"
              x1x2_31=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_20 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")
              let "yvia2=yvia2+scaled_alu2_pitch"
              x1x2_32=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_20 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")

#             4 track wide TALU2
              let "scaled_talu2_width=scaled_talu2_width+scaled_alu2_pitch"
              let "yvia2_lo=yvia2_lo-scaled_alu2_pitch/2"
              let "yvia2=yvia2_lo"
              x1x2_40=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_28 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")
              let "yvia2=yvia2+scaled_alu2_pitch"
              x1x2_41=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_28 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")
              let "yvia2=yvia2+scaled_alu2_pitch"
              x1x2_42=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_28 | \
              sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")
              let "yvia2=yvia2+scaled_alu2_pitch"
              x1x2_43=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_28 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")

#             5 track wide TALU2
              let "scaled_talu2_width=scaled_talu2_width+scaled_alu2_pitch"
              let "yvia2_lo=yvia2_lo-scaled_alu2_pitch/2"
              let "yvia2=yvia2_lo"
              x1x2_50=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_36 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")
              let "yvia2=yvia2+scaled_alu2_pitch"
              x1x2_51=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_36 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")
              let "yvia2=yvia2+scaled_alu2_pitch"
              x1x2_52=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_36 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")
              let "yvia2=yvia2+scaled_alu2_pitch"
              x1x2_53=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_36 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")
              let "yvia2=yvia2+scaled_alu2_pitch"
              x1x2_54=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_36 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")

#             6 track wide TALU2
              let "scaled_talu2_width=scaled_talu2_width+scaled_alu2_pitch"
              let "yvia2_lo=yvia2_lo-scaled_alu2_pitch/2"
              let "yvia2=yvia2_lo"
              x1x2_60=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_44 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")
              let "yvia2=yvia2+scaled_alu2_pitch"
              x1x2_61=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_44 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")
              let "yvia2=yvia2+scaled_alu2_pitch"
              x1x2_62=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_44 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")
              let "yvia2=yvia2+scaled_alu2_pitch"
              x1x2_63=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_44 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")
              let "yvia2=yvia2+scaled_alu2_pitch"
              x1x2_64=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_44 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")
              let "yvia2=yvia2+scaled_alu2_pitch"
              x1x2_65=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_44 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")

#             7 track wide TALU2
              let "scaled_talu2_width=scaled_talu2_width+scaled_alu2_pitch"
              let "yvia2_lo=yvia2_lo-scaled_alu2_pitch/2"
              let "yvia2=yvia2_lo"
              x1x2_68=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_52 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")
              let "yvia2=yvia2+scaled_alu2_pitch"
              x1x2_69=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_52 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")
              let "yvia2=yvia2+scaled_alu2_pitch"
              x1x2_70=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_52 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")
              let "yvia2=yvia2+scaled_alu2_pitch"
              x1x2_71=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_52 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")
              let "yvia2=yvia2+scaled_alu2_pitch"
              x1x2_72=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_52 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")
              let "yvia2=yvia2+scaled_alu2_pitch"
              x1x2_73=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_52 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")
              let "yvia2=yvia2+scaled_alu2_pitch"
              x1x2_74=$(grep "^S  *[^,][^,]*,$yvia2,[^,][^,]*,$yvia2,$scaled_talu2_width,\*,[^,][^,]*,TALU2 *$" $$talu2_52 | \
                sed "s/^S  *\([^,][^,]*\),$yvia2,\([^,][^,]*\),$yvia2,$scaled_talu2_width,\*,\([^,][^,]*\),TALU2 *$/\1,\2/")

              for xloc in $x1x2_20 $x1x2_21 $x1x2_30 $x1x2_31 $x1x2_32 $x1x2_40 $x1x2_41 $x1x2_42 $x1x2_43 \
                          $x1x2_50 $x1x2_51 $x1x2_52 $x1x2_53 $x1x2_54 $x1x2_60 $x1x2_61 $x1x2_62 $x1x2_63 $x1x2_64 $x1x2_65 \
                          $x1x2_68 $x1x2_69 $x1x2_70 $x1x2_71 $x1x2_72 $x1x2_73 $x1x2_74
              do
                xmin=$(echo "$xloc" | cut -f1 -d,)
                xmax=$(echo "$xloc" | cut -f2 -d,)
#
#               xmin and xmax are the 2 x coords of a TALU2 segment on the same y coord as the CONT_VIA2
#
                if [ "$xmin" -le "$xvia2" -a "$xmax" -ge "$xvia2" \
                 -o  "$xmin" -ge "$xvia2" -a "$xmax" -le "$xvia2" ]
                then
#                 CONT_VIA2 lies above this TALU2 segment
#
                  none=1
                  break
                fi
              done # looped thru all TALU2 segments wider than 4 lambda at y=y(CONT_VIA2)
            fi
          fi

          let "any=obs+none"
          if [ "$metal" = ,ALU2 -a "$obs" -eq 0 -o \
               "$metal" = ,ALU2 -a "$none" -eq 0 -o \
               "$metal" = ,CALU2 -a "$any" -eq 0 ]
          then
#           This ALU2/CALU2 segment terminating on a CONT_VIA2 needs extending
            if [ "$alu2_dir" = LEFT -o "$alu2_dir" = RIGHT ]
            then
#             This ALU2 segment needs extending in an EAST-WEST direction
#                                                          1   2   3   4   5   6   2   3   2   3   w   5   6   e
              awk -v "FS= ||," '/ '$via2''$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
              $1,$2,$3,$4,$5,$6 RS \
              "S",$2-'$scaled_alu2_overlap_via2',$3,$2+'$scaled_alu2_overlap_via2',$3,'$scaled_alu2_width',$5,$6,"LEFT,ALU2" }
              !/ '$via2''$via'/ {print}' $$temp1 > $$temp
              cp $$temp $$temp1
            elif [ "$alu2_dir" = UP -o "$alu2_dir" = DOWN ]
            then
#             This ALU2 segment needs extending in a NORTH-SOUTH direction
#                                                          1   2   3   4   5   6   2   3   2   3   w   5   6   e
              awk -v "FS= ||," '/ '$via2''$via'/ {printf "%1s %1s,%1s,%1s,%1s %1s %1s,%1s,%1s,%1s,%1s,%1s %1s,%1s\n", \
              $1,$2,$3,$4,$5,$6 RS \
              "S",$2,$3-'$scaled_alu2_overlap_via2',$2,$3+'$scaled_alu2_overlap_via2','$scaled_alu2_width',$5,$6,"UP,ALU2" }
              !/ '$via2''$via'/ {print}' $$temp1 > $$temp
              cp $$temp $$temp1
            fi
            break
          fi
        fi # operations if alu2_count > 0
      done # looped thru ALU2 and CALU2
    fi
  done # looped thru all CONT_VIA2's
  rm $$talu2_segments $$talu2_04 $$talu2_12 $$talu2_20 $$talu2_28 $$talu2_36 $$talu2_44 $$talu2_52
  fi

else
  echo "#? Something wrong. ALU3 ne metal-2 (t) or metal-3 (s). Please check." 1>&2
  exit 1
fi

sed 's/^V  *ALLIANCE/000V ALLIANCE/' $$temp1 | \
  sed 's/^H /00H /' | \
  sed 's/^EOF/zzzEOF/' | \
  sort -u | \
  sed 's/^000V /V /' | \
  sed 's/^00H /H /' | \
  sed 's/^zzz//' | \
  sed 's/  *,/,/' | \
  sed 's/  *$//' > ${cell}

rm $$temp $$temp1
