
/*************************************************************************
**
** o5_File.c
** Copyright (c) 1995,1996 Daniel Kahlin <tlr@stacken.kth.se>
**
** Commands: WRITEFILE, READFILE, COPY, DIR, STATUS, COMMAND
**
******/

#include <dos.h>
#include <stdio.h>
#include <stdlib.h>
#include <error.h>
#include <string.h>
#include <ctype.h>
#include <exec/types.h>
#include <exec/ports.h>
#include <exec/memory.h>
#include <dos/dos.h>
#include <dos/dosextens.h>
#include <dos/doshunks.h>
#include <proto/dos.h>
#include <proto/exec.h>

#include "Main_rev.h"

#include "Main.h"
#include "Block.h"
#include "Protocol.h"
#include "Main.h"
#include "Convert.h"

#include "mach.h"

#define WF_TEMPLATE "FROM/A,TO/A,DEVICE/N,DEBUG/S"

struct wf_args {
	UBYTE *as_from;
	UBYTE *as_to;
	ULONG *as_device;
	ULONG as_debug;
};

#define RF_TEMPLATE "FROM/A,TO/A,DEVICE/N,DEBUG/S"

struct rf_args {
	UBYTE *as_from;
	UBYTE *as_to;
	ULONG *as_device;
	ULONG as_debug;
};

#define CP_TEMPLATE "FROM/M,TO/A,DEBUG/S"

struct cp_args {
	UBYTE **as_from;
	UBYTE *as_to;
	ULONG as_debug;
};

#define DR_TEMPLATE "DEVICE/N,DEBUG/S"

struct dr_args {
	ULONG *as_device;
	ULONG as_debug;
};

#define ST_TEMPLATE "DEVICE/N,DEBUG/S"

struct st_args {
	ULONG *as_device;
	ULONG as_debug;
};

#define CM_TEMPLATE "COMMAND/A,DEVICE/N,DEBUG/S"

struct cm_args {
	UBYTE *as_command;
	ULONG *as_device;
	ULONG as_debug;
};

struct rf_args rf_argarray;
struct wf_args wf_argarray;
struct cp_args cp_argarray;
struct dr_args dr_argarray;
struct st_args st_argarray;
struct cm_args cm_argarray;

void copyfile(char *sourcename, char *destname);
void copymatch(char *sourcename, char *destname);
int getdevice(char *name);


/*************************************************************************
**
** WRITEFILE
**
******/
void o5_WriteFile(char *myparams)
{
	UBYTE *filename=NULL;
	UBYTE *inbuffer=NULL;
	UBYTE *toname=NULL;
	ULONG len=NULL;
	char name[64];
	int device;

	if (!(mach_rdargs(WF_TEMPLATE,(LONG *)&wf_argarray,myparams)))
		panic("error in args");
	if (wf_argarray.as_debug)
		debug=DBG_FULL;

	filename=wf_argarray.as_from;
	toname=wf_argarray.as_to;

	device=o5config.device;
	if (wf_argarray.as_device)
		device=*(wf_argarray.as_device);

	inbuffer=LoadFile(filename);
	len=jsize(inbuffer);
	str2petscii(toname,&name[0],sizeof(name));
	bl_sendfile(name,inbuffer,len,device);
}




/*************************************************************************
**
** READFILE
**
******/
void o5_ReadFile(char *myparams)
{
	UBYTE *filename=NULL;
	UBYTE *inbuffer=NULL;
	UBYTE *toname=NULL;
	ULONG len=NULL;
	char name[64];
	int device;

	if (!(mach_rdargs(RF_TEMPLATE,(LONG *)&rf_argarray,myparams)))
		panic("error in args");
	if (rf_argarray.as_debug)
		debug=DBG_FULL;

	device=o5config.device;
	if (rf_argarray.as_device)
		device=*(rf_argarray.as_device);

	filename=rf_argarray.as_from;
	toname=rf_argarray.as_to;
	str2petscii(filename,&name[0],sizeof(name));
	bl_recvfile(name,&inbuffer,&len,device);
	SaveFile(toname,inbuffer,len);

}


/*************************************************************************
**
** COPY
**
******/
void o5_Copy(char *myparams)
{
	UBYTE *filename=NULL;
	UBYTE *destname=NULL;
	UBYTE **froms;
	int	numin=0;


	if (!(mach_rdargs(CP_TEMPLATE,(LONG *)&cp_argarray,myparams)))
		panic("error in args");
	if (cp_argarray.as_debug)
		debug=DBG_FULL;

	froms=cp_argarray.as_from;
/*** check in:s ***/
	while (filename=*froms++)
		numin++;

/*** check dest ***/
	destname=cp_argarray.as_to;
	if (numin>1 && strlen(FilePart(destname))!=0)
		panic("multiple sources to one destination");

	froms=cp_argarray.as_from;
/*** Do copy ***/
	while (filename=*froms++) {
		copymatch(filename,destname);
	}

}






/*************************************************************************
**
** DIRECTORY
**
******/
void o5_Dir(char *myparams)
{
	UBYTE *filename=NULL;
	UBYTE *inbuffer=NULL;
	UBYTE *ptr,*text;
	ULONG len=NULL;
	ULONG line=0;
	int device;

	if (!(mach_rdargs(DR_TEMPLATE,(LONG *)&dr_argarray,myparams)))
		panic("error in args");
	if (dr_argarray.as_debug)
		debug=DBG_FULL;

	device=o5config.device;
	if (dr_argarray.as_device)
		device=*(dr_argarray.as_device);

	filename="$";
	bl_recvdir(filename,&inbuffer,&len,device);

	ptr=inbuffer+2;

/*** show directory ***/
	while(*ptr|*(ptr+1)) {
		line=*(ptr+2)+*(ptr+3)*256;
		text=ptr+4;
		printf("%d %s\n",line,text);
		ptr=ptr+(strlen(text)+5);
	};

}

/*************************************************************************
**
** STATUS
**
******/
void o5_Status(char *myparams)
{
	UBYTE *inbuffer=NULL;
	ULONG len=NULL;
	char name[64];
	int device;

	if (!(mach_rdargs(ST_TEMPLATE,(LONG *)&st_argarray,myparams)))
		panic("error in args");
	if (st_argarray.as_debug)
		debug=DBG_FULL;

	device=o5config.device;
	if (st_argarray.as_device)
		device=*(st_argarray.as_device);

	bl_recvstatus(&inbuffer,&len,device);
	petscii2str(inbuffer,&name[0],sizeof(name));
	printf("diskstatus: %s\n",&name[0]);

}


/*************************************************************************
**
** COMMAND
**
******/
void o5_Command(char *myparams)
{
	UBYTE *toname=NULL;
	char name[64];
	int device;

	if (!(mach_rdargs(CM_TEMPLATE,(LONG *)&cm_argarray,myparams)))
		panic("error in args");
	if (cm_argarray.as_debug)
		debug=DBG_FULL;

	device=o5config.device;
	if (cm_argarray.as_device)
		device=*(cm_argarray.as_device);

	toname=cm_argarray.as_command;
	str2petscii(toname,&name[0],sizeof(name));
	bl_sendcommand(&name[0],strlen(&name[0]),device);

}



/*************************************************************************
**
** copyfile
**
******/
void copyfile(char *sourcename, char *destname)
{
	UBYTE *inbuffer=NULL,*stbuf;
	ULONG len=NULL,stlen;
	char namebuffer[256];
	char name[64];
	int srcdevice,destdevice;

	srcdevice=destdevice=o5config.device;

/*** fix destination ***/
	if (strlen(FilePart(destname))==0) {
		namebuffer[255]=0;
		strncpy(&namebuffer[0],destname,255);
		AddPart(&namebuffer[0],FilePart(sourcename),255);
		destname=&namebuffer[0];
	}
	printf("%s -> %s\n",sourcename,destname);


/*** Read source ***/

	if (srcdevice=getdevice(sourcename)) {
		str2petscii(FilePart(sourcename),name,sizeof(name));
		bl_recvfile(name,&inbuffer,&len,srcdevice);
	}	else {
		inbuffer=LoadFile(sourcename);
		len=jsize(inbuffer);
	}

/* status */
	if (srcdevice) {
		bl_recvstatus(&stbuf,&stlen,srcdevice);
		petscii2str(stbuf,&name[0],sizeof(name));
		printf("diskstatus: %s\n",&name[0]);
	}


/*** Write destination ***/

	if (destdevice=getdevice(destname)) {
		str2petscii(FilePart(destname),name,sizeof(name));
		bl_sendfile(name,inbuffer,len,destdevice);
	}	else {
		SaveFile(destname,inbuffer,len);
	}

/* status */
	if (destdevice) {
		bl_recvstatus(&stbuf,&stlen,destdevice);
		petscii2str(stbuf,&name[0],sizeof(name));
		printf("diskstatus: %s\n",&name[0]);
	}
	jfree(inbuffer);
}



/*************************************************************************
**
** copymatch
**
******/
void copymatch(char *sourcename, char *destname)
{
	UBYTE *matchname;
	UBYTE *inbuffer=NULL;
	UBYTE *ptr,*text,*namestr,*tmp;
	ULONG len=NULL;
	char name[64];
	char newname[64];
	char sname[64];
	int	srcdevice,destdevice;

	srcdevice=destdevice=o5config.device;

	if (srcdevice=getdevice(sourcename)) {
		if (!(strchr(sourcename,'*'))) {
			copyfile(sourcename,destname);
			return;
		}
	} else {
		copyfile(sourcename,destname);
		return;
	}


	if (strlen(FilePart(destname))!=0)
		panic("multiple sources to one destination");

	ptr=strchr(sourcename,'*');
	*ptr=0;
	matchname=FilePart(sourcename);
	str2petscii(matchname,name,sizeof(name));
	matchname=name;

/*** match IT ***/
	bl_recvdir("$",&inbuffer,&len,srcdevice);
	ptr=inbuffer+2;

/*** skip diskname ***/
	text=ptr+4;
	ptr+=(strlen(text)+5);

/*** parse directory ***/
	while(*ptr|*(ptr+1)) {

		/* skip link+linenum */
		text=ptr+4; 
		/* set ptr to next link */
		ptr+=(strlen(text)+5);

		/* find start of filename */
		if (!(text=strchr(text,'\"')))
			break;

		/* skip quote */
		namestr=text+1;

		/* find endquote */
		if (!(text=strchr(namestr,'\"')))
			panic("oooaaaa!");

		/* terminate filename */
		*text=0;

		/* extract extension */
		text+=strlen(text+1)-1;
		while(!isalpha(*text))
			text--;
		*(text+1)=0;
		text-=2;

		/* match filename */
		if (strncmp(matchname,namestr,strlen(matchname))) 
			continue;
		if (strcmp("PRG",text)) 
			continue;

		strcpy(sname,sourcename);
		tmp=PathPart(sname);
		*tmp=0;

		petscii2str(namestr,newname,sizeof(newname));
		AddPart(sname,newname,sizeof(sname));
		//printf("'%s' '%s'\n",sname,destname);
		copyfile(sname,destname);
	}
}

/*************************************************************************
**
** getdevice
**
******/
int getdevice(char *name)
{
	char *ptr;
	char str[16];

	strncpy(str,name,15);
	str[15]=0;
	if (!(ptr=strchr(str,':')))
		return(0);

	*ptr=0;

	return(atoi(str));
}

