#include <stdio.h>
#include <string.h>
#include "ctypes/Plumber.h"
#include "tnef/Attachment.h"
#include "tnef/Stream.h"
#include "tnef/TNEFError.h"

static char usage[] =
 "Usage: %s [-dhv] file\n"
 "  This program extracts any attachments in an application/ms-tnef MIME.\n"
 "  For each attachment, the user is prompted to specify the filename in\n"
 "  which the attachment's data will be stored.\n\n"
 "  Below is a list of optional parameters, and how they modify the behaviour\n"
 "  of the program.\n\n"
 "  -d  Dump a textual description of the TNEF stream\n"
 "      This parameter causes the program to dump a description of each item\n"
 "      found in the TNEF stream.  This behaviour is useful for debugging\n"
 "      and learning more about the inner workings of TNEF.\n\n"
 "  -h  Display help for this program\n"
 "      This parameter causes the program to display this information.\n\n"
 "  -v  Verbosely dump a textual description of the TNEF stream\n"
 "      This parameter causes the program to dump a description of each item\n"
 "      found in the TNEF stream, including a hexadecimal dump of each\n"
 "      binary data item.  Warning: this output can be very large.\n\n"
 "  MIME: Multipart Internet Mail Extension.\n"
 "  TNEF: Transport Neutral Encapsulation Format.\n\n";

int main(int argc, char **argv)
{
    Attachment *attachment;
    CList       attachments;
    TNEFError   error;
    char        filename[256];
    FILE       *in;
    Stream      tnefStream;
    uint32      i;
    char       *argfile;
    bool        dataDump = FALSE;
    bool        dumpBinaries = FALSE;

 // Print a usage statement if the number of arguments is wrong or it looks
 // like the user wants help.
 if((argc < 2) || !strcmp(argv[1], "-?") || !strcmp(argv[1], "-h") ||
    !strcmp(argv[1], "-help"))
   {
   printf(usage, argv[0]);
   return 0;
   }

 argfile = argv[1];
 if((argc == 3) && !strcmp(argv[1], "-d"))
   {
   argfile  = argv[2];
   dataDump = TRUE;
   }
 else if((argc == 3) && !strcmp(argv[1], "-v"))
   {
   argfile      = argv[2];
   dataDump     = TRUE;
   dumpBinaries = TRUE;
   }

 // Open the TNEF file.
 if(!(in = fopen(argfile, "rb")))
   {
   fprintf(stderr, "Couldn't open file `%s'.\n\n", argfile);
   return 0;
   }

 // If the user indicated to on the command line, dump the data in this stream.
 if(dataDump)
   {
   tnefStream.dump(dumpBinaries, in);
   fclose(in);
   return 0;
   }

 // Get the list of attachments.
 if((error = tnefStream.getAttachments(in, attachments)) != TNEFSuccess)
   {
   fprintf(stderr, "Error (%ld) reading TNEF stream `%s'.\n\n", (uint32)error,
           argfile);
   fclose(in);
   return 0;
   }
 fclose(in);
 printf("Found %ld attachments.\n", attachments.GetLength());

 // For each attachment, get a filename from the user.  Then save the
 // attachment's data in the specified file.
 attachment = (Attachment *)attachments.After(NULL);
 i          = 1;
 while(attachment)
   {
   printf("Save attachment %ld, length %ld, as (%s): ", i,
          attachment->getDataLength(), attachment->getFilename());
   gets(filename);
   if(strlen(filename) > 0)
     attachment->setFilename(filename);

   if(!attachment->save())
     {
     fprintf(stderr, "Warning: failed to save attachment %ld as `%s'.\n",
             i, attachment->getFilename());
     }

   attachment = (Attachment *)attachments.After(attachment);
   i++;
   }

 return 0;
}