#!/bin/sh
#
# Filename
#
# The description of the file 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:45 $
# $Locker:  $
# $State: Exp $
#
#------------------------------------------ CVS
#
# (C) University of Passau 1995-1999, Graphlet Project
#

$new=0;
$mode="local";
$linking="";

$ledash="ledash";
$version="latest";

$tcl_library="";
$tk_library="";

$tcl_ld_path="unknown";
$tk_ld_path="unknown";
$leda_ld_path="unknown";

#
# sub environment_variable
# inserts a environment variable (shell mode)
#

sub environment_variable {
    ($var,$val) = @_;
    print "# Tcl cannot see the next line, but the shell can \\\n";
    print "$var=$val; export $var;\n";
}

#
# sub add_ld_path
# inserts a path into LD_LIBRARY_PATH
#

sub add_ld_path {

    ($path) = @_;

    if ($linking eq "dynamic") {
	$var = "LD_LIBRARY_PATH";
	print "# Tcl cannot see the next line, but the shell can \\\n";
	print "$var=$path:\$$var; export $var;\n";
    }
}



while ($#ARGV >= 0) {

    $option = $ARGV[0]; shift;

    if ($option eq "-mode") {
	$mode = $ARGV[0];
	shift;

    } elsif ($option eq "-linking") {
	$linking = $ARGV[0];
	shift;

    } elsif ($option eq "-new") {
	$new = 1;

    } elsif ($option eq "-base") {
	$base = $ARGV[0];
	shift;

    } elsif ($option eq "-ledash") {
	$ledash = $ARGV[0];
	shift;

    } elsif ($option eq "-version") {
	$version = $ARGV[0];
	shift;

    } elsif ($option eq "-tcl_library") {
	$tcl_library = $ARGV[0];
	shift;
    } elsif ($option eq "-tk_library") {
	$tk_library = $ARGV[0];
	shift;

    } elsif ($option eq "-tcl_ld_path") {
	$tcl_ld_path = $ARGV[0];
	shift;
    } elsif ($option eq "-tk_ld_path") {
	$tk_ld_path = $ARGV[0];
	shift;
    } elsif ($option eq "-leda_ld_path") {
	$leda_ld_path = $ARGV[0];
	shift;

    } else {
	die "unknown option $option\n";
    }
}


# if ($mode eq "local") {
#     if (-d "$ENV{'HOME'}/graphlet") {
# 	$base="$ENV{'HOME'}/graphlet";
#     } else {
# 	die "$ENV{'HOME'}/graphlet does not exist !\n";
#     }
# } else {
# }

if ($version eq "latest") {
    $ledash_version = $ledash;
} else {
    $ledash_version = $ledash."-".$version;
}



#
# Header
#

print "#!/bin/sh\n";

#
# Switch to local Tcl library if neccessary
#

if ($tcl_library ne "") {
    &environment_variable (TCL_LIBRARY, "$tcl_library");
}

if ($tk_library ne "") {
    &environment_variable (TK_LIBRARY, "$tk_library");
}


#
# Shared Libraries
#

if ($tcl_ld_path ne "") {
    &add_ld_path ($tcl_ld_path);
}

if ($tk_ld_path ne "") {
    &add_ld_path ($tk_ld_path);
}

if ($leda_ld_path ne "") {
    &add_ld_path ($leda_ld_path);
}


#
# Add a line that switches to Tcl mode
#

print "# the next line restarts using ledash \\\n";
if ($mode eq "global" || $mode eq "template") {
    print "exec $base/bin/$ledash_version \"\$0\" \"\$@\"\n" ;
} else {
    print "GRAPHLETBASE=\${GRAPHLETBASE:=\$HOME/graphlet};";
    print "if test -x \$GRAPHLETBASE/src/graphscript/$ledash ; then ";
    print "exec \$GRAPHLETBASE/src/graphscript/$ledash \"\$0\" \"\$@\";";
    print " else ";
    print "exec \$GRAPHLETBASE/src/graphscript/$ledash_version \"\$0\" \"\$@\";";
    print "fi\n";
}

print "#\n";
print "# Tcl code starts here\n";
print "#\n";

#
# Find out where graphlet is installed.
#
# If mode is "global", the path is in $base.
#
# Options for mode "local" are
# - environment variable GRAPHLETBASE (undocumented, experimental)
# - $HOME/graphlet
#

if ($mode eq "global" || $mode eq "template") {
    print "set GT(base) $base\n";
} else {
    print "if [info exists env(GRAPHLETBASE)] {\n";
    print "  set GT(base) \$env(GRAPHLETBASE)\n" ;
    print "} elseif [file isdirectory \$env(HOME)/graphlet] {\n";
    print "  set GT(base) \$env(HOME)/graphlet\n" ;
    print "} else {\n";
    print "  puts \"Error: Directory \$env(HOME)/graphlet not found\"\n";
    print "  puts \"Cannot use this script; contact your local guru for help.\"\n";
    print "  exit 42;\n";
    print "}\n";
}


#
# Set initial GT variables
#

if ($mode eq "global" || $mode eq "template") {
    print "set GT(graphscriptdir) \"\$GT(base)/lib/graphscript\"\n";
} else {
    print "set GT(graphscriptdir) \"\$GT(base)/src/graphscript\"\n";
}
print "set GT(imagedir) \$GT(graphscriptdir)/images\n" ;
print "set GT(bitmapdir) \$GT(graphscriptdir)/bitmaps\n" ;

#
# Output a message with version and path identification
#

print "set GT(version) \"$version\"\n" ;
print "\n";
print "puts \"Using GraphScript version \$GT(version) at\"\n";
print "puts \"\$GT(graphscriptdir)\"\n";
print "\n";

#
# Adjust auto_path
#

print "if [file exists \"\$GT(graphscriptdir)/tclIndex\"] {\n";
print "  set auto_path [linsert \$auto_path 0 \$GT(graphscriptdir)]\n";
print "  GT_init\n";
print "} else {\n";
print "  puts \"Warnung: GraphScript library not found  in \$GT(graphscriptdir)\"\n";
print "}\n";
print "\n";

#
# Add a final line that marks the end of the generated code.
#

print "# DO NOT CHANGE ANYTHING ABOVE THIS LINE\n";

if ($new == 0) {
    while (<>) {
	
	if (/#!/ .. /# DO NOT CHANGE ANYTHING ABOVE THIS LINE/) {
	    ;
	} else {
	    print $_ ;
	}
    }
}
