/* Program to communicate with Fuji MX-500 & MX-700 cameras */
#include <stdio.h>
#include <exec/types.h>

int debug =  ( 64 | 128);

#include "serialport.c"

#include "cameracommands.c"

#include "primitives.c"

#include "debugging.c"

/************ main functions ************/

void main(int argc, char *argv[])
{
	int i;

    	if (argc == 1 || (argc==2 && *argv[1]=='?'))
	{
		parseargs(argc,argv);
	}
	else
	{
		setup_comm();

		if (attention())
			parseargs(argc,argv);
		else
	          printf("!! Camera is not responding.\n");
	}

	finish();
}


finish()
{
	closeserial();
	return(1);
}


parseargs(int argc, char *argv[])
{
	int i,frame;
	char *filename;
	int understood = 0;

	if (argc == 1 || (argc==2 && *argv[1]=='?'))
	{
		printf("Usage:\n %8s ",argv[0]);
	     printf("{  DOWNLOAD  { NAME picture-name | FRAME frameno | ALL } [ filename ] }  \n");
		printf("        | {    UPLOAD  filename imagename }                                         \n");
		printf("        | {    DELETE  { NAME name } | { FRAME frameno } | ALL  }                   \n");
		printf("        | {    LIST }                                                               \n");
		printf("        | {    SHOOT [ filename [ DELETE ] ] }                                      \n");
		printf("        | {    SET [ FLASH { OFF | FORCE | REDEYE | AUTO } ]                        \n");
		printf("                   [ RESOLUTION 1280 | 640 | HI | LO ] }                            \n");
		printf("                   [ COMPRESSION BASIC | FINE | NORMAL ]                            \n");
		printf("                   [ ZOOM | NOZOOM ]                                                \n");
		printf("                   [ MACRO | NOMACRO ]                                              \n");
          printf("           }\n");
		printf("\n");
		understood=1;
	}
	else
	{
		uppercase(argv[1]);

		if (!strncmp(argv[1],"DOWNLOAD",8))
		{
			understood=1;
			uppercase(argv[2]);
			if (!strncmp(argv[2],"ALL",3)  || argv[2]==NULL)	return(download_all(argv[3]));
			if (!strncmp(argv[2],"NAME",5))
			{
				printf("!! Downloading by picture name not yet implemented\n");
				return(0);
			}
			if (!strncmp(argv[2],"FRAME",5))
			{
				if (!sscanf(argv[3],"%d",&frame)) return(0*printf("!! Bad frame number\n"));
				return(download_byframe(frame,argv[4]));
			}
			understood=0;
		}

		if (!strncmp(argv[1],"UPLOAD",6))
		{
			if (argc==4)
			{
				understood=1;
				uppercase(argv[3]);
				return(send_image(argv[2],argv[3]));
			}
		}

		if (!strncmp(argv[1],"DELETE"  ,6))
		{
               understood=1;
			uppercase(argv[2]);
			if (!strncmp(argv[2],"ALL",3)  || argv[2]==NULL)	return(delete_all());
			if (!strncmp(argv[2],"NAME",5))
			{
				printf("!! Deleting by picture name not yet implemented\n");
				return(0);
			}
			if (!strncmp(argv[2],"FRAME",5))
			{
				if (!sscanf(argv[3],"%d",&frame)) return(0*printf("!! Bad frame number\n"));
				return(delete_byframe(frame));
			}
               understood=0;
		}

		if (!strncmp(argv[1],"LIST"    ,4))
		{
			understood=1;
			return(list_all(argv[2]));
		}

		if (!strncmp(argv[1],"SHOOT"   ,5))
		{
			understood=1;
			uppercase(argv[3]);
			return(shoot_picture(argv[2],!strncmp(argv[3],"DELETE",6)));
		}

		if (!strncmp(argv[1],"SET"     ,3))
		{
			understood=0;
			for( i=2; i<argc; i++ ) uppercase(argv[i]);
			for( i=2; i<argc; i++ )
			{
				if (!strncmp(argv[i],"FLASH",5)) understood = set_flash(argv[++i]);
				if (!strncmp(argv[i],"RESOL",5)) understood = set_resolution(argv[++i]);
				if (!strncmp(argv[i],"COMPR",5)) understood = set_compression(argv[++i]);
				if (!strncmp(argv[i],"ZOOM" ,4)) understood = set_zoom(1);
				if (!strncmp(argv[i],"NOZOO",5)) understood = set_zoom(0);
				if (!strncmp(argv[i],"MACRO",5)) understood = set_macro(1);
				if (!strncmp(argv[i],"NOMAC",5)) understood = set_macro(0);
			}
			return(1);
		}

		if (!strncmp(argv[1],"TEST",4))
		{
			understood=1;
	
			send_image("ram:dsc00224.jpg","dsc00999.jpg");	

			understood=0;
		}


	}


	if (understood==0) 
	{
		printf("!! I couldn't understand the command line \n");
		return(0);
	}

	return(0);
}



uppercase(char *string)
{
	int i;
	if (string==NULL) return(0); 
	for(i=0; i<strlen(string); i++)
	{
		if (string[i]>='a' && string[i]<='z') string[i] -= 32;
	}
	return(strlen(string));
}

/****** 'High level' commands ******/

download_all(char * filename)
{
	int images,i,chars_received,startnum=1;
	char imagename[80],diskfilename[80];
	
	if (filename)
	{
		printf("@ Download all pictures to files with template %snnnn.jpeg\n",filename);
		images = get_num_pics();
		if (images==0) return(0*printf("!! No images in camera\n"));
		for(i=1; i<=images && attention(); i++)
		{	
			sprintf(diskfilename,"%s%04d.jpeg",filename,startnum++);
			chars_received = get_image(i,diskfilename);
			printf("  Frame %03d to filename %s",i,diskfilename);
			if (chars_received) printf(" successful\n"); else printf(" FAILED\n");
		}
	}
	else
	{
		printf("@ Download all pictures to files with same name as in camera\n",filename);
		images = get_num_pics();
		if (images==0) return(0*printf("!! No images in camera\n"));
		for(i=1; i<=images && attention(); i++)
		{	
			get_image_name(i,imagename);
			sprintf(diskfilename,"%s",imagename,startnum);
			chars_received = get_image(i,diskfilename);
			printf("  Frame %03d to filename %s",i,diskfilename);
			if (chars_received) printf(" successful\n"); else printf(" FAILED\n");
		}
	}	
	return(1);
}

download_byframe(int frame,char *filename)
{
	char imagename[80];
	int chars_received;

	if (filename)
	{
		printf("@ Download frame %d to file %s\n",frame,filename);
		chars_received = get_image(frame,filename);
		if (chars_received) return(1|printf(" successful\n"));  else return(0*printf(" FAILED\n"));
	}
	else
	{
		printf("@ Download frame %d to file with same name as in camera\n",frame);
		get_image_name(frame,imagename);
		chars_received = get_image(frame,imagename);
		if (chars_received) return(1|printf(" successful\n"));  else return(0*printf(" FAILED\n"));
	}
	return(1);
}


delete_all()
{
	int images,i;

	printf("@ Delete all pictures in camera\n");
	images = get_num_pics();
	if (images==0) return(0*printf("!! No images in camera\n"));
	for(i=images; i>=1 && attention(); i++)
	{	
		printf("  Deleting %04d of %04d - ",i,images);
		if (delete_image(i)) 
			printf(" successful\n\x0b"); 
		else 
			printf(" FAILED\n\x0b");
	}
	printf("\n");
	
	return(1);
}

delete_byframe(int frame)
{
	printf("@ Delete frame  %d\n",frame);
	if (delete_image(frame)) 
		printf(" successful\n"); 
	else 
		printf(" failed\n");

	return(1);
}

list_all(char *filename)
{
	int size,images,i,totsize=0;
	char name[80];
	FILE *fp=NULL;

	printf("@ List info about pictures in camera\n");
	images=get_num_pics();	
	
	if (filename) fp=fopen(filename,"w");

	for (i=1;i<=images; i++)
	{
		size=get_image_size(i);
		get_image_name(i,name);
		if (fp) fprintf(fp,"%4d  %0.20s %8d\n",i,name,size);
		printf("%4d  %0.20s %8d\n",i,name,size);
		totsize+=size;
	}
	printf("      %0.20s %8d\n","Total:",totsize);
	if (fp) fprintf(fp,"      %0.20s %8d\n","Total:",totsize);
	if (fp) fclose(fp);
	return(1);
}

shoot_picture(char *filename,int delete)
{
	int frame=0,status=0;
	
	charge_flash(200);
	shoot(&frame,&status);

	if (filename==NULL) 
	{
		printf("@ Shoot a picture and leave it in the camera\n");
	}
	else
	{
		printf("@ Shoot a picture and download it to file %s",filename);
		get_image(frame,filename);
	}
	if (delete)	
	{
		printf(" and then delete it from the camera");
		delete_image(frame);
	}
	printf("\n");
	return(1);
}

set_flash(char *flashmode)
{
	int mode=-1;
	if (!strncmp(flashmode,"OFF"   ,3)) mode = 0;
	if (!strncmp(flashmode,"FOR"   ,3)) mode = 1;
	if (!strncmp(flashmode,"RED"   ,3)) mode = 2;
	if (!strncmp(flashmode,"AUT"   ,3)) mode = 3;

	if (mode==-1) return(0*printf("!! The flashmode you chose is not supported\n"));
	
	printf("@ Flash mode set to %d - %s\n",mode,flashmode);
	set_flashmode(mode);
	return(1);
}

set_resolution(char *resol)
{
	int mode=-1;
	if (!strncmp(resol,"1280"  ,4)) mode = 1;
	if (!strncmp(resol,"HI"    ,2)) mode = 1;
	if (!strncmp(resol,"640"   ,3)) mode = 0;
	if (!strncmp(resol,"LO"    ,2)) mode = 0;
	printf("!! Not supported yet\n");
	if (mode==-1) return(0*printf("!! The resolution you chose is not supported\n"));
	printf("@ Resolution set to %d - %s\n",mode,resol);
	return(1);
}

set_compression(char *compressmode)
{
	int mode=-1;
	if (!strncmp(compressmode,"BASI"  ,4)) mode = 0;
	if (!strncmp(compressmode,"FINE"  ,4)) mode = 1;
	if (!strncmp(compressmode,"NORM"  ,4)) mode = 2;
	printf("!! Not supported yet\n");
	if (mode==-1) return(0*printf("!! The compressmodeution you chose is not supported\n"));

	printf("@ Compression mode set to %d - %s\n",mode,compressmode);
	return(1);
}

set_zoom(int zoom_mode)
{
	printf("!! Not supported yet\n");
	if (zoom_mode) printf("@ Zoom enabled\n");
	if (!zoom_mode) printf("@ Zoom disabled\n");
	return(1);
}

set_macro(int macro_mode)
{
	printf("!! Not supported yet\n");
	if (macro_mode) printf("@ Macro enabled\n");
	if (!macro_mode) printf("@ Macro disabled\n");
	return(1);
}

