/* Simple appicon that displays the size of a file */

#include <stdio.h>
#include <proto/wb.h>

extern near long __stack=16384;

extern char *id(char *buf,char *fname);

int filesize(char *name);
char *build_name(char *buf,char *name, BPTR dir);
void msg(char *msg);
BOOL quit(void);
char *getversion(char *buf, char *name);
	
struct IntuiText msg_text={
	1,0,JAM1,16,9,NULL,NULL,NULL };

struct IntuiText ok_text={
	1,0,JAM1,16,9,NULL,NULL,NULL };

struct IntuiText die_text={
	1,0,JAM1,16,9,NULL,NULL,NULL };

void main(void) {
	struct MsgPort *mp;
	struct DiskObject *dob;
	struct AppIcon *ap;
	struct AppMessage *am;

	dob=GetDiskObject("filesize_appicon");
	
	if(!dob) {
		dob=GetDefDiskObject(WBDISK);
		if(!dob) {
			printf("Serious probs with icons!\n");
			return;
		}
	}
	
	
	if(!(mp=CreateMsgPort())) {
		printf("Unable to open a msg port!\n");
		FreeDiskObject(dob);
		return;
	}

	ap=AddAppIcon(0x1,0,"FileSize",mp,0,dob,TAG_END);
	
	if(!ap) {
		printf("App Icon failed!\n");
		DeleteMsgPort(mp);
		FreeDiskObject(dob);
		return;
	} 

	while(1) {
		if(Wait((ULONG)(SIGBREAKF_CTRL_C|(1<<mp->mp_SigBit)))==SIGBREAKF_CTRL_C) 
			goto end;
		else {
			char buf[1000];
			char buf2[1000];
			char buf3[500];
			
			while(am=(struct AppMessage *)GetMsg(mp)) {
				char str[2000];
				if(am->am_NumArgs==0) {
					/* Double clicked */
					if(quit()) { ReplyMsg((struct Message *)am); goto end; }
				} else {

					build_name(buf,am->am_ArgList->wa_Name,am->am_ArgList->wa_Lock);
					sprintf(str,"FileSize v2.1 © 1997 by Ben Matthew\n===================================\n\nFile: %s\nSize: %d bytes\n\nType: %s\nVersion: %s",buf,filesize(buf),id(buf2,buf),getversion(buf3,buf));
					msg(str);
				}
				ReplyMsg((struct Message *)am);
			}
		}	
	}

	end:
	while(am=(struct AppMessage *)GetMsg(mp))
		ReplyMsg((struct Message *)am);
	DeleteMsgPort(mp);

	FreeDiskObject(dob);	
	RemoveAppIcon(ap);
	
}

int filesize(char *name) {
	FILE *fp=0;
	int size= -1;

	if(fp=fopen(name,"r")) {
		fseek(fp,0,SEEK_END);
		size=ftell(fp);
		fclose(fp);
	}

	return(size);
}

char *build_name(char *buf,char *name, BPTR dir) {

	char path[1024];

	strcpy(path,"");

	NameFromLock(dir,path,1023);
	
	strcpy(buf,path);
	if(strcmp(buf,"")) {
		if(buf[strlen(buf)-1]!=':') {
			strcat(buf,"/");
		}
	}
	
	strcat(buf,name);
		
	return(buf);

}

void msg(char *msg) {

	struct EasyStruct easypeasy;

	easypeasy.es_StructSize=sizeof(struct EasyStruct);
	easypeasy.es_Flags=0;
	easypeasy.es_Title="FileSize v2.1 © 1997 by Ben Matthew";
	easypeasy.es_TextFormat=msg;
	easypeasy.es_GadgetFormat="Continue";

	EasyRequest(NULL,&easypeasy,NULL,TAG_DONE);

}

BOOL quit(void) {

	struct EasyStruct easypeasy;

	easypeasy.es_StructSize=sizeof(struct EasyStruct);
	easypeasy.es_Flags=0;
	easypeasy.es_Title="FileSize v2.1 © 1997 by Ben Matthew";
	easypeasy.es_TextFormat="FileSize © 1997 v2.1 by Ben Matthew\n===================================\n\nReally Quit?\n";
	easypeasy.es_GadgetFormat="Yep|Nahh Maybe Not";

	return(EasyRequest(NULL,&easypeasy,NULL,TAG_DONE));
	
}

char *getversion(char *buf, char *name) {
	char command[500];
	FILE *fp;
	UBYTE c=1;
	char dud;
	
	sprintf(command,"version %s >ram:filesize_tmp",name);
	system(command);
	
	strcpy(buf,"");
	
	if(fp=fopen("ram:filesize_tmp","r")) {
		while(c!=EOF) {
			char ch[5];
			c=fgetc(fp);
			sprintf(ch,"%c",c);
			strcat(buf,ch);
		}
		buf[strlen(buf)-1]=0;
		fclose(fp);
		DeleteFile("ram:filesize_tmp");
	}
	
	if(strlen(buf)>10) {
		dud=buf[5];
		buf[5]=0;
		if(!strcmp(buf,"Could")) 
			strcpy(buf,"Not available\n");
		else
			buf[5]=dud;
	}
	

	return(buf);
}