#include <string.h>

#include "extern.h"

filwrite(mail)
     int mail;
     {   char ftitle[31],fjunle[9],fcall[9],fdate[9],ftime[6];
         char fsize[5],fkanflg[2],frep[4],frcall[9];
         char ffcall[9],ffdate[9],fftime[6],fftitle[31],rexx[2],fboard[9];
         int i;
         
         fp2 = fopen(temp_fil,"r");
         if ( fgets(fbuff,255,fp2) == NULL 
              ||  strlen(fbuff) < 90 )     { fclose(fp2); return(0); }

         substr(fbuff,97,8,fjunle);
         substr(fbuff,13,2,frep);
         substr(fbuff,15,30,ftitle);
         substr(fbuff,47,8,fcall);
         substr(fbuff,68,5,fsize);
         substr(fbuff,74,1,fkanflg);
         substr(fbuff,76,8,fdate);
         substr(fbuff,84,5,ftime);
         substr(fbuff,89,8,frcall);
         
         if ( strcmp(frcall,"        ") )
            { if ( mail ) { fp3 = fopen(MAIL_HDR,"r");
                            fseek(fp3,mptr,0); }
                   else   { fp3 = fopen(HDR_FIL,"r");
                            fseek(fp3,fptr,0); }
              while(1)
                 {  if ( fgets(fbuff,255,fp3) == NULL ) break;
                    substr(fbuff,47,8,ffcall);
                    if ( strcmp(ffcall,fcall) != NULL ) continue;
                    substr(fbuff,76,8,ffdate);
                    if ( strcmp(ffdate,fdate) != NULL ) continue;
                    substr(fbuff,84,5,fftime);
                    if ( strcmp(fftime,ftime) != NULL ) continue;
 			        substr(fbuff,15,30,fftitle);
					if ( strcmp(fftitle,ftitle) != NULL ) continue;
                    fclose(fp2); fclose(fp3); return(0);
                 }
              fclose(fp3);
            }
        
		strcpy(fboard,fjunle);
 
        if ( ! mail )
          { for ( i = 0 ; i <= JUNLE_MAX ; i++ )
             { if ( jtbl[i][0] == NULL ) 
					{ 
						if ( ! strncmp(fjunle,"PDS",3) )
								{ strcpy(fjunle,"PDS     "); break; } 
						if ( ! strncmp(fjunle,"DAT",3) )
								{ strcpy(fjunle,"DATALIB "); break; } 
						strcpy(fjunle,"FREE    "); break;
					}
               if ( strcmp(fjunle,jtbl[i]) == NULL )  break;
             }
          }

		i = makfno();        
                  
        strcpy(rexx," ");
        if ( ! strcmp(frep,"Re") ) strcpy(rexx,">");
        if ( mail ) *(fjunle + 6) = '#';
        
        nwdate();
		makfname("MSG",i,fname);
        fp3 = fopen(fname,"w");
        fprintf(fp3,"%s%s%s%s",fjunle,frep,rexx,ftitle);
        fprintf(fp3,"  From %s  %s %s %s\n",fcall,nowdate,nowtime,fsize);
        while(1)
        { if ( fgets(fbuff,255,fp2) == NULL ) break;
          if ( strncmp(fjunle,"PDS",3) == NULL 
			&& strncmp(fbuff,pdsstr,34) == NULL )
               break;
          if ( strncmp(fjunle,"DAT",3) == NULL 
			&& strncmp(fbuff,pdsstr,34) == NULL )
               break;
          fputs(fbuff,fp3);
        }
        fclose(fp3);
        
        if ( strncmp(fjunle,"PDS",3) == NULL )
           { makfname("PDS",i,fname);
             fp3 = fopen(fname,"w");
        fprintf(fp3,"%s%s%s%s",fjunle,frep,rexx,ftitle);
        fprintf(fp3,"  From %s  %s %s %s\n",fcall,nowdate,nowtime,fsize);
             while(1)
             {  if ( fgets(fbuff,255,fp2) == NULL ) break;
                fputs(fbuff,fp3);
             }
             fclose(fp3);
           }
        if ( strncmp(fjunle,"DAT",3) == NULL )
           { makfname("DAT",i,fname);
             fp3 = fopen(fname,"w");
        fprintf(fp3,"%s%s%s%s",fjunle,frep,rexx,ftitle);
        fprintf(fp3,"  From %s   %s %s %s\n",fcall,nowdate,nowtime,fsize);
             while(1)
             {  if ( fgets(fbuff,255,fp2) == NULL ) break;
                fputs(fbuff,fp3);
             }
             fclose(fp3);
           }
        fclose(fp2);
        
        fp2 = fopen(temp_fil,"r+");
        if ( fgets(fbuff,255,fp2) == NULL ) { fclose(fp2); return(0); }
        rewind(fp2);
        fprintf(fp2,"%04d",i);
        if ( ! mail ) { fseek(fp2,5L,0);
                        fprintf(fp2,"%s",fjunle);
                      }
        fseek(fp2,55L,0);
        fprintf(fp2,"%s%s",nowdate,nowtime);
        fseek(fp2,89L,0);
        fprintf(fp2,"%s",hiscall);
		if ( mail ) { fprintf(fp2,"\n"); }
			else	{ fprintf(fp2,"%s\n",fboard); }
        fclose(fp2);
        fp2 = fopen(temp_fil,"r");
        if ( mail ) fp3 = fopen(MAIL_HDR,"a");
             else   fp3 = fopen(HDR_FIL,"a");
        fgets(fbuff,255,fp2);
        fputs(fbuff,fp3);
        fclose(fp2);fclose(fp3);
        rcvcnt++;
        
        return(0);
       }
