/*
  Mail2guide by CTS
*/
/*+ "includes and defines" */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAXL 128
#define english 1
/* english 1 for english error messages, anything else for german */
/*- */
const char ver[]  = "$VER: Mail2guide 0.13 (17.11.1995)";
const char space80[]="                                                                                ";

/*+ "Subroutines" */
/*+ "usage()" */
void usage()
{   printf("Usage: Mail2guide <infile.mail> <outfile.guide>\n");
}
/*- */
/*+ "fin_error()" */
void fin_error()
#if english == 1
{   printf("Can't open input file.\n"); }
#else
{   printf("Kann Eingabedatei nicht öffnen.\n"); }
#endif
/*- */
/*+ "fout_error()" */
void fout_error()
#if english == 1
{   printf("Can't open output file.\n"); }
#else
{   printf("Kann Ausgabedatei nicht öffnen.\n"); }
#endif
/*- */
/*+ "fout_exists()" */
void fout_exists()
#if english == 1
{   printf("output file exists!\n"); }
#else
{   printf("Ausgabedatei existiert schon!\n"); }
#endif
/*- */
/*+ "tmp_error()" */
void tmp_error()
#if english == 1
{   printf("Can't open tmp file.\n"); }
#else
{   printf("Kann tmp datei nicht öffnen.\n"); }
#endif
/*- */
/*+ "help()" */
void help()
#if english == 1
{   printf("This program converts a mail file to a file readable with AmigaGuide.\n\n"); }
#else
{   printf("Dieses Program wandelt ein mail file in eine AmigaGuide datei.\n\n"); }
#endif
/*- */
/*+ "del_at(lin)" */
void del_at(lin)
char lin[MAXL];
{   int i;

    i=0;
    while (lin[i]!=0)
    {
       if (lin[i]=='@') { lin[i]='*'; }
       i++;
    }
}
/*- */
/*+ "del_quote(lin)" */
void del_quote(lin)
char lin[MAXL];
{   int i;

    i=0;
    while (lin[i]!=0)
    {
       if (lin[i]=='"') { lin[i]='\''; }
       i++;
    }
}
/*- */
/*- */

/*+ "main(argc,argv)" */
main(argc,argv)
int argc;
char *argv[];
{   long int  i,j,k;
    FILE *in,*out,*tmp;
    int header,body,from_to,subject;
    char line[MAXL],title[MAXL],name[MAXL],from_to_name[MAXL],nline[MAXL];

/*+ "Copyright" */
    printf ("%s\n",ver+6);
    printf ("(C)1995 Christian T. Steigies (steigies@physik.uni-kiel.d400.de)\n");
    printf ("Freeware, NO commercial usage ;-)\n\n");
/*- */
/*+ "in und out files checken, öffnen" */
    if  (argc==2)
    {   if ( !( strcmp(argv[1],"-h") ) || !( strcmp(argv[1],"?") ) )
        {
          atexit(&usage);
          atexit(&help);
          exit(EXIT_SUCCESS);
        }
    }

    if  (argc!=3)
    {   atexit(&usage);
        exit(EXIT_SUCCESS);
    }

    if  ((in=fopen(argv[1],"r"))==0) /* Eingabefile zum lesen öffnen */
    {   atexit(&usage);
        atexit(&fin_error);
        exit(EXIT_SUCCESS);
    }

    if  ((out=fopen(argv[2],"r"))==0) /* Ausgabefile öffnen */
    {
        if  ((out=fopen(argv[2],"w"))==0)
        {   atexit(&usage); atexit(&fout_error); exit(EXIT_SUCCESS); }
    }
    else /* file existiert schon! */
    {   atexit(&usage); atexit(&fout_exists); exit(EXIT_SUCCESS); }

    if  ((tmp=tmpfile())==0) /* tmp file öffnen */
    {   atexit(&tmp_error);
        exit(EXIT_SUCCESS);
    }

/*- */
/*+ "Variablen initialisieren" */
    printf("converting %s to %s\n\n",argv[1],argv[2]);
    fprintf(out,"@DATABASE Mail2guide.guide\n\n");
    fprintf(out,"@MASTER mail file: %s\n\n",argv[1]);
    fprintf(out,"@WIDTH 74\n\n");

    i=0;j=0; /* i: Zeilen einer mail, j: # der mails */
    header=0;body=0;
    from_to=0;subject=0;
/*- */
/*+ "Headerbeginn suchen" */
    while ( !feof(in) )
    {   fgets(line,80,in);
        if ( header==0 && 0==strncmp(line,"From ",5) )
        {  j++;
           if ( body==1 )
           {  fprintf(out,"@ENDNODE\n\n");
           }
           header=1; body=0; from_to=0; subject=0;
           fprintf(out,"@NODE h%08i \"%s",j,line); /* nur ein " reicht! */
           fprintf(out,"@PREV h%08i\n",j-1);
           fprintf(out,"@NEXT h%08i\n",j+1);
           fprintf(out,"@TOC main\n");
        }
/*- */
/*+ "Header NODE" */
        if ( header==1 )
        {
           del_at(line);
           fprintf(out,"%s",line);
        }
/*- */
/*+ "Absender suchen" */
        if ( header==1 && from_to==0 && 0==strncmp(line,"From: ",6) )
        {  strcpy(name,line+6);
           strcpy(from_to_name,line);
           from_to=1;
        }
/*- */
/*+ "Empfänger suchen" */
        if ( header==1 && from_to==0 && 0==strncmp(line,"To: ",4) )
        {  strcpy(name,line+4);
           strcpy(from_to_name,line);
           from_to=1;
        }
/*- */
/*+ "Subject suchen" */
        if ( header==1 && subject==0 && 0==strncmp(line,"Subject: ",9) )
        {  strcpy(title,line+9);
           subject=1;
        }
/*- */
/*+ "Header Ende suchen" */
        if ( header==1 && line[1]==0 ) /* Leerzeile ist header-ende */
        {  body=1; header=0;
           do
           { if (!feof(in))
                { fgets(line,80,in);
                  i++;
                }
           }
           while ( line[1]==0 && !feof(in) );
           /* Leerzeilen zwischen header und body entfernen */
           fprintf(out,"\n                    @{\"      Read the mail      \" LINK m%08i }\n",j);
           fprintf(out,"@ENDNODE\n\n",j);
           fprintf(out,"@NODE m%08i \"%s",j,from_to_name);
           /* nur ein " reicht! */
           fprintf(out,"@PREV m%08i\n",j-1);
           fprintf(out,"@NEXT m%08i\n",j+1);
           fprintf(out,"@TOC main\n");
           fprintf(out,"                    @{\"      Read the header      \" LINK h%08i }\n\n",j);
           if ( from_to==1 )
           {  fprintf(tmp,"%s",name);
           }
           else
           {  fprintf(tmp,"nobody...\n");
           }
           printf("%s",name);
           if ( subject==1 )
           {  fprintf(tmp,"%s",title);
           }
           else
           {  fprintf(tmp," <none> \n");
           }
           printf("%s",title);
        }
/*- */
/*+ "Body NODE" */
        if ( body==1 )
        {
           del_at(line);
           fprintf(out,"%s",line);
        }
    }
    fprintf(out,"@ENDNODE\n");
/*- */
/*+ "empty body before 1" */
    fprintf(out,"@NODE m%08i \"This mail does not exist\"\n",0);
    fprintf(out,"@PREV m%08i\n",0);
    fprintf(out,"@NEXT m%08i\n",1);
    fprintf(out,"@TOC main\n");
    fprintf(out,"This mail exists only in your imagination...\n");
    fprintf(out,"@ENDNODE\n");
/*- */
/*+ "empty body after last" */
    fprintf(out,"@NODE m%08i \"This mail does not exist\"\n",j+1);
    fprintf(out,"@PREV m%08i\n",j);
    fprintf(out,"@NEXT m%08i\n",j+1);
    fprintf(out,"@TOC main\n");
    fprintf(out,"This mail exists only in your imagination...\n");
    fprintf(out,"@ENDNODE\n");
/*- */
/*+ "empty header before 1" */
    fprintf(out,"@NODE h%08i \"I'm serious! This mail does NOT exist!\"\n",0);
    fprintf(out,"@PREV h%08i\n",0);
    fprintf(out,"@NEXT h%08i\n",1);
    fprintf(out,"@TOC main\n");
    fprintf(out,"This mail exists only in your imagination...\n");
    fprintf(out,"@ENDNODE\n");
/*- */
/*+ "empty header after last" */
    fprintf(out,"@NODE h%08i \"I'm serious! This mail does NOT exist!\"\n",j+1);
    fprintf(out,"@PREV h%08i\n",j);
    fprintf(out,"@NEXT h%08i\n",j+1);
    fprintf(out,"@TOC main\n");
    fprintf(out,"This mail exists only in your imagination...\n");
    fprintf(out,"@ENDNODE\n");
/*- */
/*+ "main NODE" */
    fsetpos(tmp,0);
    fprintf(out,"@NODE main \"Guide for %s\"\n",argv[1]);
    fprintf(out,"@PREV main\n");
    fprintf(out,"@NEXT main\n");
    fprintf(out,"This is an AmigaGuide® file made by Mail2guide\n\n");
    for(k=1; k<=j; k++)
    {  fgets(name,80,tmp);
       del_quote(name);
       name[strlen(name)-1]=0;
       strcat(name,space80);
       fprintf(out,"@{\" %-41.41s \" LINK m%08i } ",name,k);
       fgets(title,80,tmp);
       del_quote(title);
       title[strlen(title)-1]=0;
       strcat(title,space80);
       fprintf(out,"@{\" %-41.41s \" LINK h%08i }\n",title,k);
    }
    fprintf(out,"@ENDNODE\n");
/*- */
/*+ "files schliessen" */
    fclose(out);
    fclose(in);
    printf("\nReady.\n");
    exit(EXIT_SUCCESS);
/*- */
}
/*- */

