#!/usr/local/bin/perl
$tkmaillib = "/u/ra/raines/prog/tk/tkmail2";
$wishbin = "/usr/local/bin/wish";
$version = "2.0p6";
$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 = 0;
$movemail = "/usr/local/emacs/19.28/rs6000-ibm-aix3.2.5/movemail";
$uselockfile = 1;
$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/cvs/tk/tkmail2/tkmail,v 1.14 1995/12/14 00:49:46 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>
#####################################################################

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

# 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";
}

###########################################################################
# translate command line arguments

# argument defaults
@startargs = ();
$debug = 0;
$iconic = 0;
$personalrc = "$ENV{HOME}/tk/.tkmailrc";
if ( ! -r $personalrc ) {
  $personalrc = "$ENV{HOME}/.tkmailrc";
}
$globalrc = "$tkmaillib/settings";
$display = "";

while (@ARGV && $ARGV[0] =~ m/^-/) {
  $arg = shift(@ARGV);

  $debug=1,		next if $arg =~ m/^-d(ebug)?$/;
  $iconic=1,		next if $arg =~ m/^-i(conic)?$/;
  &usage(0)		if $arg =~ m/^-h(elp)?$/;

  if ($arg =~ m/^-p(ersonal)?$/) { $personalrc = shift; next; }
  if ($arg =~ m/^-l(ibrary)?$/) { $tkmaillib = shift; next; }
  if ($arg =~ m/^-g(lobal)?$/) { $globalrc = shift; next; }
  if ($arg =~ m/^-w(ish)?$/) { $wishbin = shift; next; }
  if ($arg eq '-display') { $display = shift; next; }

  &usage(1, qq/DSTANAL(ERROR): unknown option "$arg"\n/);
}

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

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

push(@INC,$tkmaillib);

require "mailtime.pl";
require "pwd.pl"; &initpwd;

#########################################################################
# usage -- Print out usage information

sub usage {
  local($exit, $message) = @_;

  print STDERR $message if defined $message;
  print STDERR <<INLINE_LITERAL_TEXT;
Usage: $0 [options] [folder]

Options are from among:
  -debug		Run program in debug mode.
  -display DISPLAY	Run program on X server DISPLAY.
  -global FILE		Use FILE for global settings file.
  -help       		Prints this message.
  -iconic		Startup program in iconic mode.
  -library DIRECTORY	Use DIRECTORY as location of tkmail library files.
  -personal FILE	Use FILE for user settings file
  -wish BINARY		Use BINARY as the wish program to spawn.

INLINE_LITERAL_TEXT

    exit($exit) if defined $exit;
}

###########################################################################
# 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" if $debug;
    &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);
