/****************************************************************
  odindemo2.c
  Created 11-jul-90 by Peter Oerbak
  Shows off some features of the odin.library V1.11
  This is in the public domain.
*****************************************************************/

#include	"exec/types.h"
#include	"exec/semaphores.h"
#include	"exec/tasks.h"
#include	"stdio.h"
#include	"odin.h"

struct OdinBase	*OpenLibrary();

struct SignalSemaphore	ss;

#define CR(s) ObtainSemaphore(&ss); { s } ReleaseSemaphore(&ss);

typedef struct {
	Envelope	e;
	char		*windowname;
	int		num;
} Packet;

#define p_sizeof (long)sizeof(Packet)
#define PACKNAME "A packet"

struct OdinBase *OdinBase;

Envelope *new_process(p)
Packet *p;
{
	FILE	*f;

	CR(
		f = fopen(p->windowname,"w");
		fprintf(f,"Hello there.\n");
	)

	Delay(100L);
	CR(fprintf(f,"bye,bye\n");)
	CR(fclose(f);)
	return (Envelope *)p;
}

main()
{
	Packet		*p;
	struct Task	*t;
	Envelope	*env;

	if(!(OdinBase = OpenLibrary("ram:odin.library",0L))) {
		printf("No odin library!\n");
		exit(20);
	}
	printf("odin.library open\n");
	InitSemaphore(&ss);
	printf("semaphore inited\n");
	p = (Packet *)CreateEnvelope(PACKNAME,p_sizeof);
	if(!p) {
		printf("no memory for packet\n");
		CloseLibrary(OdinBase);
		exit(20);
	}
	printf("packet created\n");
	p->e.e_proc   = new_process;
	p->num        = 0;
	p->windowname = "CON:10/10/300/200/Process";
	t = Eval(p,0L,4000L,EVAL_PROCESS);
	if(!t) {
		printf("couldn't start new process with Eval()\n");
		DisposeEnvelope(p);
		CloseLibrary(OdinBase);
		exit(20);
	}
	CR(printf("new process started\n");)
	do {
		CR(printf("polling.\n");)
		env = PollNamedEnvelope(PACKNAME);
	} while(!env);
	printf("got envelope\n");
	DisposeEnvelope(env);
	CloseLibrary(OdinBase);
}

