#! /bin/csh -f
#
#/*****************************************************************************
#                Copyright Carnegie Mellon University 1992
#
#                      All Rights Reserved
#
# Permission to use, copy, modify, and distribute this software and its
# documentation for any purpose and without fee is hereby granted,
# provided that the above copyright notice appear in all copies and that
# both that copyright notice and this permission notice appear in
# supporting documentation, and that the name of CMU not be
# used in advertising or publicity pertaining to distribution of the
# software without specific, written prior permission.
#
# CMU DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
# ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
# CMU BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
# ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
# WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
# ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
# SOFTWARE.
#*****************************************************************************/
#
#
# Translate IFF files to IPQL files that load boxes and arrows into IPQL
# hierarchical data structures.
#
# $Header: iff2ipql,v 1.13 91/11/05 11:54:32 heydon Locked $
#
# Written by Allan Heydon for the Miro project at Carnegie Mellon
#
# SYNTAX
#   iff2ipql [ -a | -A ] [ -f ] [ -p ] [ -q ] file ... [ directory ]
#
# SYNOPSIS
#   For each 'file' named "fname.ext", produce five Common Lisp files, called
#   "fname.cl", "fname-box.cl", "fname-syn.cl", "fname-con.cl", and
#   "fname-sem.cl", containing IPQL commands for adding boxes, syntax arrows,
#   direct containment arrows, and semantics arrows to the IPQL data
#   structures. Each IFF file is first "compressed" by iff2ciff(1).
#
#   If "-a" is specified, then "-a" is passed to the program ambig(1). If "-A"
#   is specified, the ambiguity check ambig(1) is *not* invoked to generate
#   semantic arrow information at all.
#
#   If the last argument names a directory, then the files are placed in that
#   directory; the default is for the files to be created in the current
#   directory.
#
#   By default, if a file named "fname.cl" already exists, the user is prompted
#   to determine if it should be overwritten. If -f is specified, overwriting
#   is forced.
#
#   By default, the top-level "control file" "fname.cl" loads the other four
#   data files relative to the current directory. If "-p" is specified, then
#   the directory pathname 'directory' is prependended to the name supplied
#   to the load function.
#
#   By default, the program prints messages describing its progress. If "-q"
#   (quiet) is specified, these messages are surpressed.
#
# FILES
#   /tmp/iff2ipql_<pid>			"compressed" input IFF file
#   /usr/miro/libi/awk/nonatoms.awk	gawk(1) script for extracting nonatoms
#   /usr/miro/libi/awk/iff2ipql.awk	gawk(1) script for initial processing
#
# SEE ALSO
#   ambig(1), iff2ciff(1)

# remove temp file on interrupt
set error_code = 128
onintr shutdown

# set file names
set awkdir = "/usr/miro/libi/awk"
set awkfile1 = $awkdir/nonatoms.awk
set awkfile2 = $awkdir/iff2ipql.awk
set tempfile = /tmp/iff2ipql_$$

# process command-line arguments
set syntax = "iff2ipql [-a|-A] [-f] [-p] [-q] file ... [directory]"
set atomic_only = ""
set gen_sem_arrows = 1
set compress = 0
set forced = 0
set prepend = 0
set loud = 1
set error = 0
while (1)
  switch ( "$1" )
    case "-a":
      set atomic_only = "-a"
      shift argv
      breaksw
    case "-A":
      set gen_sem_arrows = 0
      shift argv
      breaksw
    case "-f":
      set forced = 1
      shift argv
      breaksw
    case "-p":
      set prepend = 1
      shift argv
      breaksw
    case "-q":
      set loud = 0
      shift argv
      breaksw
    case "-*":
      echo "iff2ipql: unknown flag:" $1
      set error = 1
      shift argv
      breaksw
    default
      break	# break out of while loop entirely
  endsw
end

# test that there is a file to process
if ( $#argv == 0 ) then
  echo "iff2ipql: no file specified"
  set error = 1
endif

# print error message if necessary
if ( $error ) then
  echo "SYNTAX: $syntax"
  set error_code = 128
  goto shutdown
endif

# determine if destination directory is specified
if ( -d $argv[$#argv] ) then
  set arg_cnt = $#argv
  set dest_dir = $argv[$arg_cnt]
  @ arg_cnt--
  set argv = ($argv[-$arg_cnt])
else
  set dest_dir = "."
endif

# determine the output directory for the "load" function
if ( $prepend ) then
  set loaddir = "$dest_dir/"
else
  set loaddir = ""
endif

# process each file
if ( $loud ) echo "Translating files:"
while ( $#argv > 0 )
  # form output filename
  set outroot = $1:t
  set outroot = ${outroot:r}
  set outpath = $dest_dir/$outroot
  set loadpath = $loaddir$outroot
  set outfile = ${outpath}.cl
  if ( $loud ) echo "  $1 -> $outfile"
  if ( $1 =~ /* ) then
    set fullsource = $1
  else
    set fullsource = `pwd`/$1
  endif

  # see if the output file exists
  if ( -e $outfile ) then
    if ( ! $forced ) then
      if ( $loud ) echo -n "    "
      echo -n "Overwrite existing file $outfile:t (y or n) [n]? "
      set ans = $<
      if ( "$ans" != "y" ) then
        if ( $loud ) echo -n "    "
	echo "Not overwritten."
        goto continue
      endif
    endif
  endif

  # write output file
  echo ";;; Iff2ipql(1)-created file" >! $outfile
  echo ";;;" >> $outfile
  echo ";;; Source: " $fullsource >> $outfile
  echo ";;; Created:" `date` >> $outfile
  echo "" >> $outfile
  echo "(require 'hds)" >> $outfile
  echo "(use-package 'hds)" >> $outfile
  echo "" >> $outfile
  echo '(when *box-dt* (load "'${loadpath}'-box"))' >> $outfile
  echo '(when *syn-dt* (load "'${loadpath}'-syn"))' >> $outfile
  echo '(when *con-dt* (load "'${loadpath}'-con"))' >> $outfile

  # create auxiliary output files
  if ( $loud ) echo "    Compressing if necessary..."
  /usr/miro/bin/iff2ciff $1 >! $tempfile
  set error_code = $status
  if ( $error_code != 0 ) goto shutdown
  if ( $loud ) echo "    Generating box, syn, con entries..."
  egrep '^[ \t]*>[ \t]*INSIDE' $tempfile \
  | gawk -f $awkfile1 - \
  | gawk -f $awkfile2 "path=$outpath" - $tempfile
  set error_code = $status
  if ( $error_code != 0 ) goto shutdown
  if ( $gen_sem_arrows ) then
    if ( $loud ) then
      echo -n "    Generating access matrix"
      if ( "$atomic_only" != "" ) echo -n " (with $atomic_only)"
      echo "..."
    endif
    echo '(when *obj-dt* (load "'${loadpath}'-sem"))' >> $outfile
    set semfile = "${outpath}-sem.cl"
    echo "(in-package 'constraints)" >! $semfile
    echo "" >> $semfile
    echo "(require 'objs)" >> $semfile
    echo "(require 'ipql)" >> $semfile
    echo "(use-package 'ipql)" >> $semfile
    echo "" >> $semfile
    /usr/miro/bin/ambig -m $atomic_only $1 >! $tempfile
    set error_code = $status
    if ( $error_code != 0 ) goto shutdown
    grep '^(init-sem' $tempfile >> $semfile
    grep -v '^(init-sem' $tempfile >> $semfile
  else if ( $loud ) then
    echo "    No access matrix generated."
  endif

continue:
  shift argv
end
set error_code = 0

shutdown:
rm -f $tempfile
if ( $loud ) echo "Done."
exit($error_code)
