#! /usr/local/bin/perl

##---------------------------------------------------------------------------##
##  File:
##      MosaicMail
##  Author:
##      Earl Hood       ehood@convex.com
##	Copyright (C) 1994, Wed Oct 26 15:00:44 CDT 1994
##  Thanks:
##	Roman Czyborra <czyborra@cs.tu-berlin.de> -- Whose feedback
##		and sh script greatly improved the program.
##
##  Description:
##      MosaicMail is a simple Perl program that loads a single mail
##	message from STDIN into a running Mosaic process.  If no Mosaic
##	process is running, the script will exec Mosaic.
##	MHonArc is called to do the actual conversion of the e-mail
##	message.  Therefore, MIME messages will be converted.
##
##	Change the values in the CONFIG section below to suit your
##	needs.
##
##  Notes:
##	o  For MH users, the following can be used to process the current
##	   mail message:
##
##		% show -noshowproc -noheader | MosaicMail
##
##	o  For Trn users, you can load a newsgroup post into Mosaic
##	   by piping the post to MosaicMail:
##
##		| MosaicMail
##
##	o  MHonArc needs to be in your search path
##
##	o  If extra files are created by MHonArc (i.e. for graphics,
##	   binaries, etc), they will not get removed.  You'll have
##	   to manually remove them.
##
##---------------------------------------------------------------------------##
##    Copyright (C) 1995        Earl Hood, ehood@convex.com
##
##    This program is free software; you can redistribute it and/or modify
##    it under the terms of the GNU General Public License as published by
##    the Free Software Foundation; either version 2 of the License, or
##    (at your option) any later version.
##
##    This program is distributed in the hope that it will be useful,
##    but WITHOUT ANY WARRANTY; without even the implied warranty of
##    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
##    GNU General Public License for more details.
##
##    You should have received a copy of the GNU General Public License
##    along with this program; if not, write to the Free Software
##    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
##---------------------------------------------------------------------------##

##---------------------------------------------------------------------------
#############################################################################
##---------------------------------------------------------------------------
##	CONFIG: Change the following variables to reflect your needs

## Location of converted message; directory must exist and be a FULL
## pathname.
$DESTDIR = "$ENV{'HOME'}/tmp";

## Set the following variable to "goto" or "newwin".  "goto" will make
## Mosaic load the message into the current window.  "newwin" causes
## Mosaic to load the message into a new window.
$directive = "newwin";

## Give location of mknod command for creating FIFO files to communicate
## with Mosaic.
$mknod = '/etc/mknod';

## Uncomment and set the following variable to a MHonArc resource file if
## you want to customize the format of the converted message
#$ENV{'M2H_RCFILE'} = "mhonarc.rc";

##	CONFIG: End change section
##---------------------------------------------------------------------------
#############################################################################
##---------------------------------------------------------------------------

##---------------------------------------------------------------------------
##	Actual code to do the conversion (Should not have to edit)

## Read .mosaicpid to find PID of last Mosaic process
if (open(PID, "$ENV{'HOME'}/.mosaicpid")) {
    $pid = <PID>;  chop $pid;  close(PID);
    $tmpfile = "/tmp/Mosaic.$pid";
} else {
    $pid = 0;	# No Moasic process to attach to
}

## Set html filename
$htmlfile  = "$DESTDIR/" . time() . ".$$.html";

## Set handler to clean up if program is interrupted
$SIG{'ABRT'} =
$SIG{'HUP'}  =
$SIG{'INT'}  =
$SIG{'QUIT'} =
$SIG{'TERM'} = 'cleanup';

## Make FIFO for html file
system("$mknod $htmlfile p") && &error("Unable to create $htmlfile: $!\n");

## Set Mosaic command to use if Mosaic is not currently running
$Mosaiccmd = "Mosaic file://localhost/$htmlfile 2>&1 > /dev/null";

if ($pid && (kill 'CONT', $pid)) {	# Mosaic session active
    ## Set Mosaic control file
    system("$mknod $tmpfile p") && &error("Unable to create $tmpfile: $!\n");

    ## Signal Mosaic to read control file
    kill 'USR1', $pid;

    ## Write instructions to control file
    open(MTMP, ">$tmpfile") || &error("Unable to open $tmpfile: $!\n");
    print MTMP "$directive\n",
	       "file://localhost/$htmlfile\n";
    close(MTMP);

} else {				# Call Mosaic explicitly
    if (!fork()) {	# child
	exec($Mosaiccmd);
	&error("Unable to exec $Mosaiccmd: $!\n");
    }
}

## Convert message to HTML by using MHonArc
open(MHONARC, "|mhonarc -single -outdir $DESTDIR > $htmlfile");
print MHONARC <STDIN>;
close(MHONARC);

&cleanup();

##---------------------------------------------------------------------------
sub cleanup {
    unlink "/tmp/Mosaic.$pid", $htmlfile;
    exit 0;
}
##---------------------------------------------------------------------------
sub error {
    warn @_;
    &cleanup();
}
