#include <gui/image.h>
#include <storage/file.h>
#include <util/application.h>
#include <iostream.h>

using namespace os;

#define TRY( stmt, msg )	try { stmt; } catch(errno_exception &e) { cout << msg << endl << e.error_str() << endl; return; }

class MyApp : public Application
{
	public:
	MyApp( char *src, char *dst ) : Application( "application/x-vnd.digitaliz-IconConvert" )
	{
		TRY( pcSource = new File( src ), "Can't open source file!" );
		TRY( pcDest = new File( dst, O_CREAT ), "Can't create destination file!" );
		TRY( pcIcon = new BitmapImage( pcSource ), "Can't load source file!" );
		TRY( pcIcon->Save( pcDest, "image/atheos-icon" ), "Failed to save icon!" );
		cout << src << " => " << dst << endl;
	}

	MyApp::~MyApp()
	{
		if(pcSource) delete pcSource;
		if(pcDest) delete pcDest;
		if(pcIcon) delete pcIcon;
	}

	private:
	File*		pcSource;
	File*		pcDest;
	Image*		pcIcon;
};

int main( int ac, char **av )
{
	cout << "IConvert V1.0" << endl;
	if( ac != 3 ) {
		cout << "USAGE: " << av[0] << " <image file> <icon file>.icon" << endl;
		return 0;
	}

	MyApp ThisApp( av[1], av[2] );

	// Note:
	// We need an Application object to access the library classes safely.
	// Since the Run() method is never called, we have to delete the app
	// object, and this is why it is allocated on the stack here.
	// When Run() is used, the object must be allocated with new!
}

