/*
**      $Filename: GenOberon.c $
**      $Release: 1.0 $
**      $Revision: 37.1 $
**
**      GenOberon main program.
**
**      (C) Copyright 1993 Kai Bolay.
**          Written by Kai Bolay & Jan van den Baard
**/

#include "GenOberon.h"

/*
**      Open up and allocate the necessary resources.
**/
Local BOOL GetResources( void )
{
    /* Open up the libraries */
    if ( ! ( IntuitionBase = ( struct IntuitionBase * )OpenLibrary( "intuition.library", 37L ))) {
        Print( "Error -: Unable to open the intuition.library V37+\n" );
        return( FALSE );
    }

    if ( ! ( DiskfontBase = ( struct Library * )OpenLibrary( "diskfont.library", 37L ))) {
        Print( "Error -: Unable to open the diskfont.library V37+\n" );
        return( FALSE );
    }

    if ( ! ( GTXBase = ( struct GTXBase * )OpenDiskLibrary( GTXNAME, GTXVERSION ))) {
        Print( "Error -: Unable to open the gadtoolsbox.library V39+\n" );
        return( FALSE );
    }

    GfxBase         = ( struct Library * )GTXBase->GfxBase;
    NoFragBase      = GTXBase->NoFragBase;
    GadToolsBase    = GTXBase->GadToolsBase;
    UtilityBase     = GTXBase->UtilityBase;

    /* Get us a MemoryChain */
    if ( ! ( Chain = GetMemoryChain( CHAINSIZE ))) {
        Print( "Error -: Out of memory\n" );
        return( FALSE );
    }
}

/*
**      Close and de-allocate the resources.
**/
Local VOID FreeResources( void )
{
    /* Close the libraries and de-allocate the MemoryChain. */
    if ( GTXBase ) {
        if ( Chain )
            FreeMemoryChain( Chain, TRUE );
        CloseLibrary(( struct Library * )GTXBase );
    }

    if ( DiskfontBase )
        CloseLibrary(( struct Library * )DiskfontBase );

    if ( IntuitionBase )
        CloseLibrary(( struct Library * )IntuitionBase );
}

/*
**      Main entry point.
**/
LONG _main( void )
{
    LONG        error = 0L;

    stdOut = Output();

    if ( GetResources()) {
        if ( SArgs = ReadArgs( "NAME/A,TO=AS/A,QUIET/S", ( LONG * )&Arguments, NULL )) {
            Print( "GenOberon 1.0 - (C) Copyright 1993 Kai Bolay.\n\tWritten by Kai Bolay & Jan van den Baard\n" );

            if ( ! ( error = GTX_LoadGUI( Chain, Arguments.Name, RG_GUI,            &GuiData,
                                                                 RG_Config,         &MainConfig,
                                                                 RG_WindowList,     &Projects,
                                                                 RG_Valid,          &ValidBits,
                                                                 TAG_END ))) {
                WriteOberonSource();
            } else {
                switch ( error ) {
                    case    ERROR_NOMEM:
                        Print( "Error -: out of memory\n" );
                        break;
                    case    ERROR_OPEN:
                        Print( "Error -: unable to open the GUI file\n" );
                        break;
                    case    ERROR_READ:
                        Print( "Error -: read error\n" );
                        break;
                    case    ERROR_WRITE:
                        Print( "Error -: write error\n" );
                        break;
                    case    ERROR_PARSE:
                        Print( "Error -: iffparse.library error\n" );
                        break;
                    case    ERROR_PACKER:
                        Print( "Error -: unable to decrunch the file\n" );
                        break;
                    case    ERROR_PPLIB:
                        Print( "Error -: the file is crunched and the powerpacker.library is not available\n" );
                        break;
                    case    ERROR_NOTGUIFILE:
                        Print( "Error -: not a GUI file\n" );
                        break;
                }
                error = 0;
            }

            GTX_FreeWindows( Chain, &Projects );
            FreeArgs( SArgs );
        } else
            PrintFault( error = IoErr(), "Error -" );
    }

    FreeResources();

    _exit( error );
}
