#!/bin/tcsh
#
# cifquery.sh <cif_filename> [view]
#
#   Create a techfile for, and optionally view, any CIF layout in magic
#

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

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

rm -f cif_out.txt
touch cif_out.txt
magic -dnull -noconsole -nowrapper -T gdsquery >& cif_out.txt <<EOF
drc off
cif rescale no
cif warning default
cif read $1
exit
quit
EOF

cat cif_out.txt | egrep -o "layer.*isn.t known.*" | sort -u > types.txt
cat types.txt | cut -d' ' -f 2 | sort -g > layer_map.txt
set layers=`cat layer_map.txt`
set numtypes=$#layers
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 CIF file as magic layers, one per plane.
#

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

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

planes
EOF


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

types
EOF

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

cat >> ciftemp.tech <<EOF
end 

contact
end

styles
   styletype mos
EOF

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

cat >> ciftemp.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 $i" >> ciftemp.tech
   echo "   labels $i" >> ciftemp.tech
   echo "" >> ciftemp.tech
end

cat >> ciftemp.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" >> ciftemp.tech
   @ j++
end
cat >> ciftemp.tech <<EOF
end

# wiring
# end

# router
# end

# plowing
# end

plot
  style pnm
end
EOF

#
# Clean up temporary files
#

rm -f cif_out.txt types.txt layers.txt

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

cat >> cif_load.tcl <<EOF
tech load ciftemp -noprompt
drc off
cif read $1
select top cell
expand
shell rm cif_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 cif_load.tcl script.
# Effectively, this is the same as doing "magic -d OGL" followed by
# "source cif_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 cif_load.tcl"

