#include <bastard.h>
#include <bdb.h>
#include <extension.h>



int plugin_main( void *param){
      struct DISASM_TGT *target;
      char str[256];
      struct section s;
      int cont;

      target = GetBastardTgt();

      /* PREDISASM phase: parse the file header */
      if ( target->status < DISASM_TGT_PREDISASM ) 
         if (! ApplyTargetFormat() ) return(SetLastError(4570));
      /* Calling EiC from withing EiC really fucks the stack over */
      target->status = DISASM_TGT_PREDISASM;


      /* DISASM phase */
      cont = DBIndexFirst(SECTION_RVA, &s);
      while(cont) {
         if ( (s.flags & SECTION_TYPE_MASK == SECTION_CODE) || 
               s.flags & SECTION_EXECUTE) {
            sprintf(str, "Disassembling Section %s . Instruction stack:\n", 
                         s.name);
            Message(str);
            DisassembleSection( s.name );
         }
         cont = DBIndexNext(SECTION_RVA, &s);
      }
      target->status = DISASM_TGT_DISASM;
         
      /* POSTDISASM Phase */
      /* create an export for the entry point */
      DefineExport(target->entry, "_start");
      target->status = DISASM_TGT_POSTDISASM;

      Message("Done.\n");
      return(1);
}
