/*
 * generate_express.cc
 *
 *
 */

/* Since generate_express doesn't use any schema-specific things except the
   init function, we don't need to include the schema's header file inside
   tests.h  */
#define DONT_NEED_HEADER
#include <tests.h>

/********************  main()  ****************************/

main() {
    // This has to be done before anything else.  This initializes
    // all of the registry information for the schema you are using.
    // The SchemaInit() function is generated by fedex_plus... see
    // extern statement above.

    Registry * registry = new Registry( SchemaInit );

    // "Reset" has tables for browsing
    registry->ResetSchemas();
//    registry->ResetEntities();
//    registry->ResetTypes();

    const SchemaDescriptor * schema = 0;

    SchemaDescriptor * schema2 = 0;
#if 0
    schema2 = ( SchemaDescriptor * )registry->FindSchema( "Group_Schema" );

    EntityDescriptor * ent = new EntityDescriptor(
        "New_Entity", schema2, SCLLOG( LFalse ), SCLLOG( LFalse ),
        ( Creator ) 0 );
    const EntityDescriptor * ed = registry->FindEntity( "Application_Context_Element" );

//    ent->AddSupertype(application_context_schemae_application_context_element);
    ent->AddSupertype( ( EntityDescriptor * )ed );
    Uniqueness_rule_ptr ur = new Uniqueness_rule;
    ur->comment_( "Hi Dave" );
    if( ent->_uniqueness_rules ) {
        ent->_uniqueness_rules->Append( ur );
    } else {
        ent->_uniqueness_rules = new Uniqueness_rule__set;
        ent->_uniqueness_rules->Append( ur );
    }
    schema2->AddEntity( ent );
    registry->AddEntity( *ent );
#endif
    schema = ( SchemaDescriptor * )registry->FindSchema( "Example_Schema" );
    EntityDescriptor * ed = ( EntityDescriptor * )registry->FindEntity( "Circle" );
    Uniqueness_rule_ptr ur = new Uniqueness_rule;
    ur->comment_( "(* Hi Dave *)\n" );
    if( ed->_uniqueness_rules ) {
        ed->_uniqueness_rules->Append( ur );
    } else {
//  Uniqueness_rule__set_var ursv = new Uniqueness_rule__set;
        ed->uniqueness_rules_( new Uniqueness_rule__set );
        ed->_uniqueness_rules->Append( ur );
    }

    registry->ResetSchemas();
    schema = registry->NextSchema();
    ofstream * efile;
    std::string str, tmp;

    while( schema != 0 ) {
//  str = "new";
        str = "";
        str.Append( StrToLower( schema->Name(), tmp ) );
        str.Append( ".exp" );
        efile = new ofstream( str.c_str() );
        cout << "Generating: " << str << endl;
        schema->GenerateExpress( *efile );
        efile->close();
        delete efile;

        schema = registry->NextSchema();
    }
    cout << "Done!" << endl;
}
