#include <ccmio.h>
#include "MeshNGEOM.h"
#include "PostNPOST.h"

#include <iostream>
using namespace std;

const char kDefaultState[] = "default";

int main( int argc, char *argv[] )
{
    if (argc < 3 || argc > 4)
    {
	cout << "Usage: " << argv[0] << " file.ccm mesh.ngeom [post.npost]"
	     << endl;
	return(1);
    }

    MeshNGEOM mesh(argv[2]);

    int i = 0;
    CCMIOID root, state, processor;
    CCMIOError err;

    err = CCMIOOpenFile(NULL, argv[1], kCCMIOWrite, &root);
    CCMIONewState(&err, root, kDefaultState, NULL, NULL, &state);
    if (CCMIONextEntity(&err, state, kCCMIOProcessor, &i, &processor) != kCCMIONoErr)
	err = CCMIONewEntity(NULL, state, kCCMIOProcessor, NULL, &processor);

    try {
	mesh.WriteCCMIO(state, processor);
	if (mesh.GetError() != kCCMIONoErr)
	    cout << "Error " << mesh.GetError()
		 << " occurred while writing mesh." << endl;
	if (argc == 3)
	{
	    CCMIOCloseFile(&err, root);
	    return(0);
	}

	PostNPOST post(argv[3]);
	post.WriteCCMIO(processor);
    }
    catch(IOException& e)
    {
	cerr << e.what() << endl;
    }
    CCMIOCloseFile(&err, root);
    return(0);
}
