#!/bin/tcsh
#
# gdsquery.sh <gds_filename> [view]
#
#   Create a techfile for, and optionally view, any GDS layout in magic
#

if ( $# == 0) then
  echo "Usage:  $0 gds_filename [view]"
  exit 1
endif

#
# Read in the GDS file quickly, using a null techfile, and grep the
# error output for information about GDS layers encountered in the input.
#

rm -f gds_out.txt
touch gds_out.txt
magic -dnull -noconsole -nowrapper -T gdsquery >& gds_out.txt <<EOF
drc off
gds rescale false
gds polygon subcell true
gds warning default
gds read $1
exit
quit
EOF

cat gds_out.txt | egrep -o "layer=.*" | sort -u > types.txt
cat types.txt | sed -e 's/layer=//' | sort -g | sed -e 's/type=//' > layer_map.txt
set layers=(`cat layer_map.txt | cut -d\  -f1`)
set numtypes=$#layers
set datatypes=(`cat layer_map.txt | cut -d\  -f2`)
set styles=(polysilicon ndiffusion pdiffusion capacitor metal1 metal2 metal3 \
metal4 metal5 metal6 metal7 metal8 metal9 implant1 implant2 implant3 implant4 \
ntransistor ptransistor electrode poly_light mvndiff hvndiff ncontact mvpdiff \
hvpdiff pcontact poly_resist metal10 mems)

#
# Generate a simple techfile that will represent all of the layers found
# in the GDS file as magic layers, one per plane.
#

rm -f gdstemp.tech
touch gdstemp.tech
cat > gdstemp.tech <<EOF
tech
   30
   gdstemp
end

version
   version 0.0
   description "Auto-generated techfile for unknown GDS read-in"
end

planes
EOF


foreach i ( $layers )
   echo "   plane$i" >> gdstemp.tech
end
cat >> gdstemp.tech <<EOF
end

types
EOF

foreach i ( $layers )
   echo "   plane$i type$i" >> gdstemp.tech
end

cat >> gdstemp.tech <<EOF
end 

contact
end

styles
   styletype mos
EOF

set i=1
while ( $i <= $numtypes )
   echo "   type${layers[$i]} ${styles[$i]}" >> gdstemp.tech
   @ i++
end

cat >> gdstemp.tech <<EOF
end

compose
end

connect
end

cifoutput
style generic
   scalefactor 1
end

cifinput
style generic
   scalefactor 1

EOF
foreach i ( $layers )
   echo "   layer type$i GDS$i" >> gdstemp.tech
   echo "   labels GDS$i" >> gdstemp.tech
   echo "" >> gdstemp.tech
end

set i=1
while ( $i <= $numtypes )
   echo "   calma GDS${layers[$i]} ${layers[$i]} ${datatypes[$i]}" >> gdstemp.tech
   @ i++
end

cat >> gdstemp.tech <<EOF
end

# mzrouter
# end

drc
end

extract
style generic
   cscale 1
   lambda 1
   step 10
   sidehalo 0

EOF
set j=0
foreach i ( $layers )
   echo "  planeorder plane$i $j" >> gdstemp.tech
   @ j++
end
cat >> gdstemp.tech <<EOF
end

# wiring
# end

# router
# end

# plowing
# end

plot
  style pnm
end
EOF

#
# Clean up temporary files
#

rm -f gds_out.txt types.txt layers.txt datatypes.txt

#
# Generate a small script telling magic how to load and view the GDS file
# this is a self-destructive script that removes itself after the GDS
# file has been read.
#

cat >> gds_load.tcl <<EOF
tech load gdstemp -noprompt
drc off
gds polygon subcells true
gds read $1
foreach i [cellname list top] { if {[llength [cellname list children \$i]] == 0} { cellname delete \$i -noprompt } }
load [lindex [cellname list top] 0]
select top cell
expand
rm gds_load.tcl
EOF

if ( $# != 2) then
   exit 0
endif

# If a third argument is given (nominally "view", but this is not checked),
# then run magic and feed it the gds_load.tcl script.
# Effectively, this is the same as doing "magic -d OGL" followed by
# "source gds_load.tcl" on the magic command-line.  It duplicates the
# tkcon startup from /usr/local/bin/magic script but sources the above
# script as the last part of the startup procedure.

set magicbin=`which magic`
set execline=`egrep "^([[:space:]])*exec.*tkcon" $magicbin`
set line1=`echo ${execline:s/exec//} | cut -d\\ -f1`

exec $line1 \
        -slave "package require Tk; set argc 2; set argv [list -d OGL]; \
        source /usr/local/lib/magic/tcl/magic.tcl; \
	source gds_load.tcl"
