#!/bin/sh
# The shell cannot see the next line \
exec graphscript "$0" "$@"

#
# filename
#
# The description of filename goes HERE.
#
#------------------------------------------ CVS
#
# CVS Headers -- The following headers are generated by the CVS
# version control system. Note that especially the attribute
# Author is not necessarily the author of the code.
#
# $source: /home/br/CVS/graphlet/src/gt_base/Attributes.h,v $
# $Author: himsolt $
# $Revision: 1.2 $
# $Date: 1999/03/05 20:39:08 $
# $Locker:  $
# $State: Exp $
#
#------------------------------------------ CVS
#
# (C) University of Passau 1995-1999, Graphlet Project
#

#
# Remove default window from the screen
#

#
# Parse Command Line
#

set Usage "$argv0 ?-layout algorithm? ?-postscript? ?-view? ?-nodesize w h? files"

for {set i 0} {$i < [llength $argv]} {incr i} {
    switch -- [lindex $argv $i] {
	-layout {
	    lappend operation layout
	    incr i
	    set action "layout_[lindex $argv $i]"
	    if { [info exists GT(action,$action)] == {} } {
		puts stderr "Unknown layout action $action"
		exit 1
	    }
	}
	-postscript {
	    lappend operation postscript
	}
	-tiff {
	    lappend operation tiff
	}
	-view {
	    lappend operation view
	}
	-nodesize {
	    lappend operation nodesize
	    if { $i+1 >= [llength $argv] } {
		puts stderr "-nodesize needs two coordinates"
		puts stderr $Usage
		exit 1
	    }
	    set w [lindex $argv [incr i]]
	    set h [lindex $argv [incr i]]
	}
	default {
	    lappend files [lindex $argv $i]
	}
    }
}


#
# Check Parameters
#

if ![info exists files] {
    puts stderr "No files specified"
    puts stderr $Usage
    exit 1
}

if ![info exists files] {
    puts stderr "No files specified"
    puts stderr $Usage
    exit 1
}



#
# layout all files on the command line
#


switch -regexp $operation {
    layout {
	if { ! $GT(no_window_system) } {
	    wm withdraw .
	}
    }
    view|postscript|tiff {
	canvas .c
	pack .c
	$g canvas .c
    }
}


foreach f $files {

    #
    # create a graph and load it
    #

    set g [graph]
    set GT(batch,graph) $g
    $g load -file $f

    #
    # Interpret operations
    #

    foreach op $operation {
	switch -regexp $op {

	    layout {
		GT_action batch $action
		$g save -file $f
	    }

	    postscript|tiff {
		$g draw
		set bbox [$g bbox -node [$g nodes]]
		.c configure \
		    -width [lindex $bbox 2] \
		    -height [lindex $bbox 3]
		update
		.c postscript -file [file rootname $f].ps \
		    -x [lindex $bbox 0] \
		    -y [lindex $bbox 1] \
		    -width [lindex $bbox 2] \
		    -height [lindex $bbox 3]	
		exec chmod u+rw [file rootname $f].ps
		if { $op == "tiff" } {
		    exec gs \
			-sDEVICE=tifflzw \
			-sOutputFile=[file rootname $f].tiff \
			[file rootname $f].ps \
			-q -dNOPAUSE -c quit
		}
	    }

	    view {
		$g draw
		bind . <Return> { puts hello; set batch_next 42 }
		update
		vwait batch_next
		puts $batch_next
	    }

	    nodesize {
		foreach node [$g nodes] {
		    $g nodeconfigure $node graphics -w $w
		    $g nodeconfigure $node graphics -h $h
		}
		$g save -file $f
	    }

	    default {
		puts stderr "Unknown operation"
		exit 1
	    }
	}
    }
}

exit 0

##########################################
#
# Set emacs variables
#
##########################################

# ;;; Local Variables: ***
# ;;; mode: tcl ***
# ;;; tcl-indent-level: 4 ***
# ;;; End: ***
