#!/usr/local/bin/perl5.000
$tkmaillib = "/u/ra/raines/prog/tk/tkmail2";
$wishbin = "/usr/local/bin/wish";
$version = "2.0beta17";
$base64dec = "/usr/local/bin/mmencode -u -b";
$quotprdec = "/usr/local/bin/mmencode -u -q";
$base64enc = "/usr/local/bin/mmencode -b";
$quotprenc = "/usr/local/bin/mmencode -q";
$stupidformat = 0;
$usemovemail = 1;
$movemail = "/usr/local/emacs/19.28/rs6000-ibm-aix3.2.5/movemail";
$uselockfile = 0;
$lockfile = "/usr/local/bin/lockfile";
$LOCK_SH = 1;
$LOCK_EX = 2;
$LOCK_NB = 4;
$LOCK_UN = 8;
$EWOULDBLOCK = 11;
$SOCK_STREAM = 1;
$AF_UNIX = 1;
$WNOHANG = 1;

# WARNING: DO NOT add or delete any of the lines above. You may comment
# any of the setting out above and add code below this comment to
# set them in a system dependent manner. For example,
#
# chop( $system = `uname -s` );
# if ( $system eq "AIX" ) {
#   $movemail = "/usr/local/emacs/19.28/rs6000-ibm-aix3.2.5/movemail";
# } elsif ( $system eq "SunOS" ) {
#   $movemail = "/usr/local/emacs/19.28/sparc-sun-sunos4.1.3/movemail";
# } else { die "Can't find movemail for $system.\n"; }

# $Header: /u/ra/raines/src/tk/tkmail2/tkmail,v 1.6 1995/07/01 01:49:48 raines Exp $
#####################################################################
#  This script is part of TkMail program. Full details can be found
#  by reading the help.txt file in the $tkmaillib directory defined
#  above. Written by Paul Raines <raines@slac.stanford.edu>
#####################################################################
die "Can't run $wishbin.\n" if ! -x $wishbin;

if ( $stupidformat ) { $gt = ">*"; } else { $gt = ""; }

# whether or not in debug mode (can use -debug option to turn on)
$debug = 0;

# make sure mail is secure (temp files read-only by user)
umask 077;

if ($usemovemail && ! -x $movemail) {
  die "ERROR: The program \"$movemail\" is not executable by user\n";
}
if ($uselockfile && ! -x $lockfile) {
  die "ERROR: The program \"$lockfile\" is not executable by user\n";
}

require "pwd.pl";
&initpwd;

###########################################################################
# translate command line arguments
require "newgetopt.pl";

&NGetOpt("iconic", "i", "library=s", "l=s", "global=s", "g=s", 
	 "personal=s", "p=s", "display=s", "debug", "help", "h") 
  || die "Error parsing command line options.\n$usage";

$usage = "Usage: tkmail [-i] [-l lib_dir] [-g set_file] [-p dot_file] [folder]\n";
@startargs = ();

if ($opt_help || $opt_h) { print $usage; exit(0); }

if ($opt_debug) { $debug = 1; }

if ($opt_iconic || $opt_i) {
  push(@startargs, "-iconic");
}

if ($opt_l ne "") {
  $tkmaillib = $opt_l;
} elsif ($opt_library ne "") {
  $tkmaillib = $opt_library;
}
push(@INC,$tkmaillib);

require "mailtime.pl";

if ($opt_p ne "") {
  $opt_personal = $opt_p;
} elsif ($opt_personal eq "") {
  $opt_personal = "$ENV{HOME}/tk/.tkmailrc";
  if ( ! -r $opt_personal ) {
    $opt_personal = "$ENV{HOME}/.tkmailrc";
  }
}

if ($opt_g ne "") {
  $opt_global = $opt_g;
} elsif ($opt_global eq "") {
  $opt_global = "$tkmaillib/settings";
}

if ($opt_display ne "") {
  $display = "-display $opt_display";
} else {
  $display = "";
}

if (@ARGV > 1) {
  die "Only one folder may be opened at a time";
} elsif (@ARGV == 1) {
  push(@startargs, $ARGV[0]);
}
@ARGV = ();

###########################################################################
# Initial signal handling code

sub segfault {
  local($sig) = @_;
  kill 9, $wishpid;
  die "Caught a SIG$sig--shutting down\n";
}

sub testpid {
  if ( waitpid($wishpid,$WNOHANG) ) {
    print STDERR "PERL: Wish process had died ($?)! Shutting down.\n";
    &safequit(1);
  }
  $SIG{'CHLD'} = "testpid";
}

###########################################################################
# start Perl/Wish connection
$origpid = $$;

die "socketpair unsuccessful: $!!\n" 
  unless socketpair(W0,WISH,$AF_UNIX,$SOCK_STREAM,0);

if ($wishpid=fork) {
  $SIG{'SEGV'} = "segfault";
  $SIG{'CHLD'} = "testpid";
  select(WISH); $| = 1;
  open(STDIN,">&WISH");
  print WISH "wm withdraw .\n";
  print STDERR "WISH PID: $wishpid   PERL PID: $$\n" if $debug;
  if (defined($stat = eval('require "maildb.pl"'))) {
      waitpid($wishpid,$flags);
  } else {
    print STDERR "$@\n";
    kill 9, $wishpid;
  }
} elsif (defined $wishpid) {
  open(STDOUT, ">&W0");
  open(STDIN, ">&W0");
  close(W0);
  select(STDOUT); $| = 1;
  exec "$wishbin -name tkmail $display --";
  kill 9, $origpid;
  die "Error executing $wishbin: $!\n";
  
} else {
  die "fork error: $!\n";
}
close(WISH);
