//
//	ctrltmr.m, Kommandozeilen-Utility zum Senden zweier Remote Messages
//		an eine Timer-Applikation
//

#include "TimerSpeaker.h"
#include <libc.h>

void usage(const char *name) {

	fprintf(stderr,"%s: invoke either with option -r or -x\n",name);
	exit(1);
}

void performmsg(const char *name,SEL aSel) {

	int     msgDelivered, fileOpened; 
	id      mySpeaker = [[TimerSpeaker alloc] init]; 
	port_t  thePort = NXPortFromName("Timer",NULL);

	if( thePort!=PORT_NULL ) {
    		[mySpeaker setSendPort:thePort]; 
    		msgDelivered = (int)[mySpeaker perform:aSel]; 
    		if( msgDelivered!=0 ) {
			fprintf(stderr,"%s: sorry can't deliver "
				"method\n",name);
			exit(2);
    		}
	}
	[mySpeaker free];
	port_deallocate(task_self(),thePort);
}

int main(int argc,char **argv) {

	const char *name = strrchr(argv[0],'/');

	if( name )
		name += 1;
	else
		name = argv[0];

	if( argc!=2 ) usage(name);

	if( strcmp(argv[1],"-r")==0 )
		performmsg(name,@selector(resetUnits));
	else if( strcmp(argv[1],"-x")==0 )
		performmsg(name,@selector(exit));
	else
		usage(name);

	return 0;
}
