#include <stdlib.h>
#include <stdio.h>

#define BUFSIZE 100000

char x[BUFSIZE];

int main(int argc, char **argv) {
 
  FILE *infile,*outfile;
  int fixups=0;
  
  if (argc!=3) { fprintf(stderr,"Syntax: moz2imap <infile> <outfile>\n");exit(2);}
  infile=fopen(argv[1],"r");
  if (infile==NULL) { fprintf(stderr,"Cannot find input file %s\n",argv[0]);exit(1);}
  outfile=fopen(argv[2],"w");
  if (outfile==NULL) { fprintf(stderr,"Cannot open output file %s\n",argv[1]);exit(1);}
  
  while(fgets(x,BUFSIZE,infile)!=NULL) {
    /* We are looking for lines like From - DDD MMM D   
     * which need to be formatted like From - DDD MMM 0D
     * for UW IMAP stuff
     */
    if (x[0]=='F' && x[1]=='r' && x[2]=='o' && x[3]=='m' && x[4]==' ' && x[5]=='-' && x[6]==' ' && x[10]==' ' && x[14]==' '&& x[16]==' ') {
      fixups++;
      printf("%s\n",x);
      fwrite(x,1,15,outfile);
      fputc(' ',outfile);
      fputs(x+15,outfile);
    } else fputs(x,outfile);
  }
  fclose(infile);
  fclose(outfile);
  printf("%d fixups performed.\n",fixups);
  exit(0);
}

