/*
 * Copyright (c) 1993 Michael D. Bayne.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are permitted provided that
 * the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the
 *    following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
 *    following disclaimer in the documentation and/or other materials provided with the distribution.
 *
 * 3. All advertising materials mentioning features or use of this software must display the following
 *    acknowledgement:
 *
 *       This product includes software developed by Michael D. Bayne.
 *
 * 4. My name may not be used to endorse or promote products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY MICHAEL D. BAYNE ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL MICHAEL D. BAYNE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <exec/types.h>
#include <exec/memory.h>
#include <exec/ports.h>

#include <dos/dos.h>
#include <dos/dostags.h>

#include <intuition/intuition.h>

#include <clib/exec_protos.h>
#include <clib/dos_protos.h>
#include <clib/alib_protos.h>
#include <clib/utility_protos.h>
#include <clib/reqtools_protos.h>

#include "Blanker.h"
#include "defs.h"

VOID setPrefs( struct bPrefObject *, UBYTE );
UBYTE *doPath( UBYTE *, UBYTE * );

extern	ULONG			tags[];
extern	UBYTE			modInfo[];
	struct Task		*bTask = 0L, *tmpTask = 0L;
extern	struct MsgPort		*blankerPort, *modulePort;
extern	struct bPrefObject	bPrefs;

VOID loadModule( ULONG loadPrefs )
{
	BPTR	checkExists;
	struct bMessage *initMsg;

	if( checkExists = Open( doPath( bPrefs.modName, bPrefs.modDir ), MODE_OLDFILE ) ) {
		Close( checkExists );
		SystemTags( doPath( bPrefs.modName, bPrefs.modDir ), NP_FreeSeglist, TRUE, SYS_Asynch, TRUE,
			SYS_Input, Open( "NIL:", MODE_OLDFILE ), SYS_Output, Open( "NIL:", MODE_OLDFILE ),
			TAG_END );

		WaitPort( blankerPort );
		if( initMsg = ( struct bMessage * )GetMsg( blankerPort )) {
			modulePort = ( struct MsgPort * )initMsg->bm_Mess.mn_ReplyPort;
			tmpTask = initMsg->bm_Task;

			if( initMsg->bm_Dep ) {
				bPrefs.sDep = initMsg->bm_Dep;
				bPrefs.Flags &= ~BS_ASKDEP;
			} else bPrefs.Flags |= BS_ASKDEP;

			if( initMsg->bm_Mod != INVALID_ID ) {
				bPrefs.sMod = initMsg->bm_Mod;
	      			bPrefs.Flags &= ~BS_ASKSCR;
			} else bPrefs.Flags |= BS_ASKSCR;

			if( loadPrefs ) CopyMem( initMsg->bm_Data, bPrefs.prefData, 512 );
			CopyMem( initMsg->bm_Info, modInfo, 64 );
			bPrefs.Flags |= BS_VALMOD;

			ReplyMsg(( struct Message * )initMsg );
		}
	} else {
		CopyMem( "No module loaded.", modInfo, 18 );
		bPrefs.Flags |= BS_ASKDEP;
		bPrefs.Flags |= BS_ASKSCR;
		bPrefs.Flags &= ~BS_VALMOD;
	}

	if( BlankerWnd && loadPrefs ) {
		setPrefs( &bPrefs, GD_MODINFO );
		setPrefs( &bPrefs, GD_MDISPLAY );
	}
}

LONG sendModMsg( ULONG Type )
{
	struct bMessage *bMsg;

	if(!( bMsg = AllocMem( sizeof( struct bMessage ), MEMF_CLEAR|MEMF_PUBLIC ))) return( 1 );

	bMsg->bm_Mess.mn_ReplyPort = blankerPort;
	bMsg->bm_Mess.mn_Length = sizeof( struct bMessage );
	bMsg->bm_Task = ( struct Task * )( &bTask );
	bMsg->bm_Type = Type;
	bMsg->bm_Mod  = bPrefs.sMod;
	bMsg->bm_Dep  = bPrefs.sDep;
	bMsg->bm_Data = bPrefs.prefData;

	if( modulePort ) {
		PutMsg( modulePort, ( struct Message * )( bMsg ));
		switch( Type ) {
		case BM_DOBLANK:
			bTask = tmpTask;
			break;
		case BM_DOQUIT:
			modulePort = 0L;
			break;
		default:
			break;
		}
		WaitPort( blankerPort );
		FreeMem( GetMsg( blankerPort ), sizeof( struct bMessage ));
	} else { 
		EZRQ( "Can't send message. No module loaded." );
		FreeMem( bMsg, sizeof( struct bMessage ));
		return( 2 );
	}

	return( 0 );
}
