/*
 *      $Filename: PGPMore.c $
 *      $Revision: 1.3 $
 *      $Date: 1994/03/11 13:10:37 $
 *
 *      Copyright (C) 1993 by Peter Simons <simons@peti.GUN.de>
 *
 *      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.
 *
 *
 *      $Id: PGPMore.c,v 1.3 1994/03/11 13:10:37 simons Exp simons $
 *
 */

/**************************** includes ********************/
#include <stdio.h>
#include <string.h>
#include <dos/dos.h>
#include <dos/dostags.h>
#include <proto/dos.h>
#include <proto/exec.h>

#include "PGPMore_rev.h"

/**************************** prototypes ******************/

/**************************** defines, macros *************/
#define LINEBUFSIZE 4096
#define BEGIN     "-----BEGIN PGP MESSAGE----"
#define END       "-----END PGP MESSAGE----"
#define BEGINSIG  "-----BEGIN PGP SIGNED MESSAGE----"
#define ENDSIG    "-----END PGP SIGNATURE----"
#define MARKBEGIN "======== BEGIN OF PGP BLOCK ========\n"
#define MARKEND   "======== END OF PGP BLOCK ========\n"

/**************************** global variables ************/
static const char __RCSId[] = "$Id: PGPMore.c,v 1.3 1994/03/11 13:10:37 simons Exp simons $";
static const char __OSVer[] = VERSTAG;

/**************************** program *********************/
int main(int argc, char **argv)
{
        BPTR pipe, input = Input(), output = Output();
        STRPTR linebuffer, pipename, cmdbuffer;
        LONG rc = 0;

        if (argc != 1) {
                Printf(VERS " -- written by Peter Simons <simons@peti.GUN.de>\n");
                Printf("Usage: %s <[infile] >[outfile]\n", argv[0]);
                return 0;
        }

        if (!(linebuffer = AllocVec(LINEBUFSIZE+(512*2), 0L))) {
                Printf("Couldn't allocate my buffers!\n");
                return 20;
        }
        pipename = linebuffer+LINEBUFSIZE;
        cmdbuffer = pipename+512;
        sprintf(pipename, "T:PGPMore.%ld", FindTask(NULL));
        sprintf(cmdbuffer, "PGP <%s -f", pipename);

        Flush(input); Flush(output);    /* initialize stdio for buffered access */

        while (!rc && FGets(input, linebuffer, LINEBUFSIZE-1)) {
                if (!strncmp(linebuffer, BEGIN, strlen(BEGIN)) || !strncmp(linebuffer, BEGINSIG, strlen(BEGINSIG)) ) {
                        FPuts(output, MARKBEGIN);
                        if (pipe = Open(pipename, MODE_NEWFILE)) {
                                FPuts(pipe, linebuffer);
                                while (FGets(input, linebuffer, LINEBUFSIZE-1)) {
                                        FPuts(pipe, linebuffer);
                                        if (!strncmp(linebuffer, END, strlen(END)) || !strncmp(linebuffer, ENDSIG, strlen(ENDSIG)) )
                                                break;
                                }
                                Close(pipe);
                                Flush(output);
                                System(cmdbuffer, NULL);
                                Flush(output);
                                FPuts(output, MARKEND);
                        }
                        else
                                FPuts(output, "Can't open temporary file!\n");
                }
                else
                        FPuts(output, linebuffer);
        }

        DeleteFile(pipename);
        FreeVec(linebuffer);

        return rc;
}

