/*
 *
 *  AM --- AmigaMail
 *  (C) 1991, 1992 by Christian Riede
 *
 *  AM is distributed in the hope that it will be useful, but WITHOUT ANY
 *  WARRANTY.  No author or distributor accepts responsibility to anyone
 *  for the consequences of using it or for whether it serves any
 *  particular purpose or works at all, unless he says so in writing.
 *  Refer to the GNU General Public License, Version 1, for full details.
 *  
 *  Everyone is granted permission to copy, modify and redistribute AM,
 *  but only under the conditions described in the GNU General Public
 *  License, Version 1.  A copy of this license is supposed to have been 
 *  given to you along with AM so you can know your rights and responsi-
 *  bilities.  It should be in a file named COPYING.  Among other things,
 *  the copyright notice and this notice must be preserved on all copies.
 *
 *  
 *
 */


#include "am.h"

UBYTE *vers = (UBYTE *)("\0$VER: savefile:"AMIGAMAIL_VERSION);

struct IntuitionBase *IntuitionBase;
struct Library *AslBase;
struct Screen *AMScreen;
struct Window *Window;
struct FileRequester *FReq;

UBYTE Filename[100];

#define BUFLEN 4096
UBYTE Buffer[BUFLEN];

int main(int argc, char *argv[])
{
	BPTR in,out,lock;
	int i;
	
	if (argc!=3 && argc!=4)
	{
		printf("Usage: %s <requesttext> <filename> [defaultname]\n",argv[0]);
		goto abort;
	}

	if (lock = Lock((UBYTE *)"t:",ACCESS_READ))
		CurrentDir(lock);

	if (!(IntuitionBase = (struct IntuitionBase *)
		OpenLibrary((UBYTE *)"intuition.library",AM_LIBRARY_VERSION)))
	{
		puts("Can't open asl.library");
		goto abort;
	}

	if (!(AslBase = 
		OpenLibrary((UBYTE *)"asl.library",AM_LIBRARY_VERSION)))
	{
		SimpleRequest(IntuitionBase->ActiveWindow,"Can't open asl.library");
		goto abort;
	}

	if (!(FReq = (struct FileRequester *)
		AllocAslRequestTags(ASL_FileRequest,
			TAG_END,0)))
	{
		SimpleRequest(IntuitionBase->ActiveWindow,"Can't get filerequester");
		goto abort;
	}

	Window = IntuitionBase->ActiveWindow;

	/* look for other AM's public screen */
	if (AMScreen = LockPubScreen((UBYTE *)PUBSCREENNAME))
		Window = AMScreen->FirstWindow;

	/* get filename */
	if (AslRequestTags(FReq,
		ASL_Hail,argv[1],
		ASL_Window,Window,
		ASL_File,FilePart((argc==4)?((UBYTE *)argv[3]):((UBYTE *)argv[2])),
		ASL_Dir,(argc==4)?(strncpy((char *)Buffer,argv[3],
			(char *)PathPart((UBYTE *)argv[3])-argv[3])):("t:"),
		TAG_END,0)) 
	{
		strncpy((char *)Filename,(char *)FReq->rf_Dir,100);
		AddPart((UBYTE *)Filename,(UBYTE *)FReq->rf_File,100);

		if (!(in = Open((UBYTE *)argv[2],MODE_OLDFILE)))
		{
			SimpleRequest(IntuitionBase->ActiveWindow,"can't open input file");
			goto abort;
		}

		if (!(out = Open(Filename,MODE_NEWFILE)))
		{
			Close(in);
			SimpleRequest(IntuitionBase->ActiveWindow,"can't open output file");
			goto abort;
		}

		while (i=Read(in,Buffer,BUFLEN))
			Write(out,Buffer,i);
			
		Close(in);
		Close(out);
	}

abort:
	if (FReq) FreeFileRequest(FReq);
	if (AslBase) CloseLibrary(AslBase);
	if (AMScreen) UnlockPubScreen(0,AMScreen);
	if (lock) UnLock(lock);
	if (IntuitionBase) CloseLibrary((struct Library *)IntuitionBase);
}
