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


   int plugin_main( void *param){
   struct DISASM_TGT *target;
      struct function f;
      int cont;

      target = GetBastardTgt();

   
      /* PREDISASM phase: parse the file header */
      if ( target->status < DISASM_TGT_PREDISASM ) 
         if (! ApplyTargetFormat() ) return(SetLastError(4570));
      target->status = DISASM_TGT_PREDISASM;

      /* DISASM phase */
      /* Start by disassembling all functions recognized by the format parser */
      Message("Disassembling named symbols. Instruction stack:\n");

      cont = DBIndexFirst(FUNCTION_RVA, &f);
      while (cont) {
         DisassembleForward(f.rva);
         cont = DBIndexNext(FUNCTION_RVA, &f);
      }
   
      /* Then attack the entry point */
      Message("Disassembling forward from entry point. Instruction stack:\n");
      DisassembleForward(target->entry);
      target->status = DISASM_TGT_DISASM;


      /* POSTDISASM Phase */
      /* create an export for the entry point */
      DefineExport(target->entry, "_start");
      Message("Performing post-disassembly passes\n");
      /* EIC fucks up when moree than 3 of these are used, goddamn POS
      DisassembleAllPasses();
      */
      //DisassembleAllPasses();
      DisassemblePass(1, 0);
      DisassemblePass(2, 0);
      DisassemblePass(3, 0);
      target->status = DISASM_TGT_POSTDISASM;

      /* OK, done, return to base */
      Message("Done.\n");
      return(1);
   }

