/*
 *
 * $Header: DH0:src/omti/dist/src/RCS/OmtiWrite.c,v 1.1 92/11/25 02:07:02 Barnard Exp $
 *
 */

/*
 * This program writes the 52 sectors from disk, which are
 * used as booting track for AmigaOS 2.04. If you want to use it for
 * AmigaOS 1.3, you should use (5*26) instead of (2*26) below.
 *
 * Sorry, I just inserted this comment-lines, the rest is commented in
 * german.
 *
 * This program was written under AmigaOS 1.3. I don't know, whether it will
 * run under AmigaOS 2.04 and above.
 *
 * The TAB-Size use is 4.
 */


#include <exec/types.h>
#include <exec/io.h>

#include <stdio.h>

#include "include/omti.h"
#include "include/omtistruct.h"

main(argc,argv)
STRPTR	argv[];
COUNT	argc;
{
SHORT				 OmtiUnit;
struct	MsgPort		*DI_Port;
struct	IOStdReq	*DI_IoReq;
UBYTE				 BootTrackBuffer[512*26*2];
BYTE				 DI_Error;
FILE				*outfile;

	if(argc == 0)
		exit(20);				/* Sorry! No WorkBench! */
	if((argc != 3) || ((argc == 3) && ((*argv[1] == '?') || ((*argv[1] & 0xfe) != '0'))))
	{
		printf("\n%s 0|1 <filename>\n",argv[0]);
		exit(10);
	}

/* Device öffnen */

	if(NULL == (DI_Port = (struct MsgPort *)CreatePort("OmtiRead-Port",NULL)))
	{
		printf("Kein Message-Port!\n");
		exit(20);
	}
	if(NULL == (DI_IoReq = (struct IOStdReq *)CreateExtIO(DI_Port,sizeof(struct IOStdReq))))
	{
		DeletePort(DI_Port);
		printf("Kein IORequest!\n");
		exit(20);
	}
	OmtiUnit	 = *argv[1] - '0';

	if((outfile = (FILE *)fopen(argv[2],"r")))
	{
		printf("%d\n",fread(BootTrackBuffer, 512, 26*2, outfile));
		fclose(outfile);
	}
	else
	{
		printf("File geht nicht auf\n");
		exit(20);
	}

	if(OpenDevice(OD_NAME,OmtiUnit,DI_IoReq,NULL))
	{
		DeleteExtIO(DI_IoReq);
		DeletePort(DI_Port);
		printf("Device busy!\n");
		exit(20);
	}

	DI_IoReq->io_Command	 = CMD_WRITE;
	DI_IoReq->io_Length		 = 512*26*2;		/* zwei Spuren lesen */
	DI_IoReq->io_Offset		 = 512*26;			/* Spuren eins und zwei lesen */
	DI_IoReq->io_Data		 = BootTrackBuffer;

	if(DI_Error = DoIO(DI_IoReq))
		printf("IO-Fehler: %d\n",DI_Error);

	CloseDevice(DI_IoReq);
	DeleteExtIO(DI_IoReq);
	DeletePort(DI_Port);
	exit(0);
}
