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

/*
 * This program writes the necessary bootblocks for the fastboot
 * mode on disks.
 *
 * 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	<stdio.h>

#include	<exec/types.h>
#include	<exec/memory.h>
#include	<devices/trackdisk.h>

#define  BOOTPFAD  "Devs:Omti/"
#define  BOOTBLOCKSIZE (TD_SECTOR * 2)

void Help(n)
STRPTR n;
{
	printf("Usage:\n");
	printf("%s {DF0|DF1|DF2|DF3} {0|1}\n",n);
}

main(argc,argv)
STRPTR	argv[];
COUNT	argc;
{
struct	MsgPort		*DI_Port;
struct	IOStdReq	*DI_IoReq;

struct	ParaBlock	*ParaStruct;
UBYTE				*BootBlockBuffer;
BYTE				 DI_Error;

SHORT				 DiskUnit;

TEXT				 FileName[30];
FILE				*fp;

	if(argc == 0)
		exit(20);				/* Sorry! No WorkBench! */
	if(argc != 3)
	{
		Help(argv[0]);
		exit(10);
	}
	if	(	(strncmp(argv[1],"df",2) && strncmp(argv[1],"DF",2)) ||
			(	(argv[1][2] & 0xfc)	!= '0') ||
				(argv[1][3]			!= ':') ||
			(	(*argv[2] & 0xfe)	!= '0')
		)
	{
		Help(argv[0]);
		exit(10);
	}

	sprintf(FileName,"%sOmtiBoot.%c",BOOTPFAD,*argv[2]);
	if(NULL == (fp = (FILE *)fopen(FileName,"r")))
	{
		printf("Kein File: %s\n",FileName);
		exit(20);
	}

	if(NULL == (BootBlockBuffer	 = (UBYTE *)AllocMem(BOOTBLOCKSIZE,MEMF_CLEAR | MEMF_CHIP | MEMF_PUBLIC)))
	{
		printf("Kein Speicher!\n");
		fclose(fp);
	}
/* Device oeffnen */

	if(NULL == (DI_Port = (struct MsgPort *)CreatePort("OmtiInstall-Port",NULL)))
	{
		fclose(fp);
		FreeMem(BootBlockBuffer,BOOTBLOCKSIZE);
		printf("Kein Message-Port!\n");
		exit(20);
	}
	if(NULL == (DI_IoReq = (struct IOStdReq *)CreateExtIO(DI_Port,sizeof(struct IOStdReq))))
	{
		fclose(fp);
		FreeMem(BootBlockBuffer,BOOTBLOCKSIZE);
		DeletePort(DI_Port);
		printf("Kein IORequest!\n");
		exit(20);
	}
	DiskUnit	 = argv[1][2] - '0';
	if(OpenDevice(TD_NAME,DiskUnit,DI_IoReq,NULL))
	{
		fclose(fp);
		FreeMem(BootBlockBuffer,BOOTBLOCKSIZE);
		DeleteExtIO(DI_IoReq);
		DeletePort(DI_Port);
		exit(20);
	}

	fread(BootBlockBuffer,BOOTBLOCKSIZE,1,fp);
	fclose(fp);
	if(ferror(fp))
	{
		clearerr(fp);
		printf("File-Error: %s\n");
		DeleteExtIO(DI_IoReq);
		DeletePort(DI_Port);
		exit(20);
	}

	DI_IoReq->io_Command	 = CMD_WRITE;
	DI_IoReq->io_Length		 = BOOTBLOCKSIZE;
	DI_IoReq->io_Offset		 = 0;			/* Parameterblock beginnt bei 0 */
	DI_IoReq->io_Data		 = BootBlockBuffer;
	if(DI_Error = DoIO(DI_IoReq))
		printf("IO-Fehler: %d\n",DI_Error);
	else
	{
		DI_IoReq->io_Command	= CMD_UPDATE;
		DoIO(DI_IoReq);

		DI_IoReq->io_Command	= TD_MOTOR;
		DI_IoReq->io_Length		= 0;
		DoIO(DI_IoReq);

		printf("Disk in Drive %s bootet jetzt von Unit %s\n",argv[1],argv[2]);
	}
	FreeMem(BootBlockBuffer,BOOTBLOCKSIZE);
	CloseDevice(DI_IoReq);
	DeleteExtIO(DI_IoReq);
	DeletePort(DI_Port);
	exit(0);
}
