eval '(exit $?0)' && eval '[ -f /usr/local/bin/perl ] && exec /usr/local/bin/perl -S $0 ${1+"$@"}; exec perl -S $0 ${1+"$@"};'
& eval 'if ( -f /usr/local/bin/perl ) exec /usr/local/bin/perl -S $0 $argv:q ; exec perl -S $0 $argv:q'
     if 0;

# @(#)[Hyper-G] [HGC-T] hgedit	1.10 [edit - Terminal Client] [Gerald Pani]

# 
#<copyright>
# 
# Copyright (c) 1993-1995
# Institute for Information Processing and Computer Supported New Media (IICM),
# Graz University of Technology, Austria.
# 
#</copyright>
 
#<file>
#
# Name:       hgedit.pl
#
# Purpose:    edit text documents via Terminal Client
#
# Created:       Mai 93    Gerald Pani
#
# Modified:   27 Jan 94    Gerald Pani
#             95/03/07 Wolfram Schneider <wosch@cs.tu-berlin.de>, Gerald Pani
#                      add $HYPERG_HOME and $EDITOR
#		       use default display if no commands defined
#                      
#
#
# Description:
# 
# The program reads parameters from the file 
# 	$HOME/.hgedit.mnu or 
# 	$HYPERG_HOME/hgedit.mnu or
# 	/usr/local/Hyper-G/hgedit.mnu or
# uses default values for the parameters. 
# (See /usr/local/Hyper-G/samples/hgedit.rc)
# 
#</file>

die "usage: $0 <filename> <language>\n" unless $#ARGV == 1;

sub readable {
    local($file) = @_;

    return $file if (-r $file && -f $file);
    warn "file: ``$file'' exist, but unusable!\a\n" 
	if -e $file || -l $file;
    return 0;
}

sub standard_menue {
    push(@display, "\tDefault hgedit-menu\n");
    push(@display, "\tChoose an Editor (type digit):\n");
    push(@display, "\n");
    push(@commands, "vi \$text\n");
    push(@display, "1 vi           (UNIX standard editor)\n");
    push(@commands, "emacs -nw \$text -f end-of-buffer\n");
    push(@display, "2 emacs        (\"Escape-Meta-Alt-Control-Shift\")\n");
    push(@display, "\n");
}

$hgeditmenue = &readable("$ENV{'HOME'}/.hgedit.mnu") ||
    &readable("$ENV{'HYPERG_HOME'}/hgedit.mnu") ||
    &readable("/usr/local/Hyper-G/hgedit.mnu");

@display;		
@commands;
if ($hgeditmenue) {
  open(HGE, $hgeditmenue) || die "Couldn't open $hgeditmenue\n";
  while(<HGE>) {
    next if /^\s*#.*$/;
    (/^<LINE>(.*)$/) && (push(@display, $1)) && next;
    (/^<LINE\/$ARGV[1]>(.*)$/) && (push(@display, $1)) && next;
    (/^<ENTRY>(.*)$/) && ($lastentry = $1) && next;
    (/^<ENTRY\/$ARGV[1]>(.*)$/) && ($lastentry = $1) && next;
    if (/^<COMM>(.*)$/ && defined($lastentry)) {
      push(@commands, $1);
      $number = $#commands+1;
      $lastentry =~ s/\$number/$number/g;
      push(@display, $lastentry);
      undef($lastentry);
    }
    if (/^<PAGE>(.*)$/) {
      while(@display) {
	$d = shift(@display);
	print "$d\n";
      }
      print "$1";
      local($what) = 1;
      $what = <STDIN>;
      @display = ();		
      @commands = ();
    }
  }
  close(HGE);
}
else {
    &standard_menue;
}
      
$#commands >= 0 || &standard_menue;
$what = 1;
if ($#commands > 0) {
    print join("\n", @display);

    # Default Editor
    $editor = $ENV{'EDITOR'} || "vi";
    $editor =~ s|.*/+||;
    $defaulteditor = 1;

    for($i = 0; $i <= $#commands; $i++) {
	if ($commands[$i] =~ /^\s*$editor\s+/) {
	    $defaulteditor = $i + 1;
	    last;
	}
    }

    # Choise your editor
    while(1) {
	print ">> [$defaulteditor] ";
	$what = <STDIN>; $what =~ s/^\s*//; $what =~ s/\s*$//;
	$what = $defaulteditor unless $what;
	
	last if $what >= 1 && $what <= $#commands + 1;
	warn "Wrong choise! Use 1-" . ($#commands + 1 ) . "\a\n";
    }
}

# Start editor
$comm = $commands[$what - 1];
$comm =~ s/\$text/$ARGV[0]/g;
@comms = split(/[ \t\n]+/, $comm);
shift(@comms) while (@comms && $comms[0] eq '');
exec (@comms) || die "exec failed! $!\n";
