/******************************************************************************
 * Steve Rider - stever@transmission23.com
 * QuickPics
 *   Image galleries for the web made easy
 *
 * main.c - main
 *
 *****************************************************************************/
/* 17 February 98 */

#include "qp.h"
#include "utility.h"
#include "image.h"
#include "html.h"
#include "dir.h"

/* PUBLIC MAIN OBJECTS */
QPDir_ob QPDir;
QP_ob QP;

/* Prototypes */
void	do_arg(char *arg);

void main(int argc, char **argv)
{
	char	cwd[MAXTEXTLEN], yn[1024];
	int	first_time = 0, i;

	/* Init objects (must do this first to setup default configs) */
	QPDir_Init();
	QP_Init();

	/* Process args */
	if(argc > 1)
	{
		for(i=1; i<argc; i++)
			do_arg(argv[i]);
	}
	else
		do_arg("-h");

	/* Check to make sure they entered a directory */
	if(!QP.rootdir[0])
		do_arg("-h");

	/* Title screen */
	printf("===========================================================================\n");
	printf("%s\n",QP.copyright);
	printf("\n");
	printf("Reading picture directory: %s\n",BaseFilename(QP.rootdir));

	/* Check validity of specified dir */
	if(!IsDirectory(QP.rootdir))
	{
		printf("*** ERROR: \"%s\" is not a directory.\n",QP.rootdir);
		return;
	}
	chmod(QP.rootdir,0755);

	/* Save cwd and change to specified dir. */
	getcwd(cwd,MAXTEXTLEN-1);
	if(chdir(QP.rootdir) != 0)
	{
		printf("*** ERROR: Unable to change to directory \"%s\".\n",QP.rootdir);
		return;
	}

	/* Read configfile from specified directory. */
	if(!QP_ReadConfig())
	{
		char	default_descriptions[25];
		char	thumbnails[25];

		default_descriptions[0] = 0;
		thumbnails[0] = 0;
		first_time = 1;

		if(!QP.write_thumbnails)
			strcpy(thumbnails," (off)");
		printf("\n");
		printf("  This looks like your first time running QuickPics against\n");
		printf("  the specified picture directory \"%s\".\n",BaseFilename(QP.rootdir));
		printf("\n");
		printf("  The following is a brief overview of what is about to be performed:\n");
		printf("\n");
		printf("      1) The picture directory will be explored and image dimensions retrieved,\n");
		printf("      2) Thumbnails will be created for all images%s,\n",thumbnails);
		printf("      3) HTML pages will be written using collected information.\n");
		printf("\n");
		printf("  These steps are dictated by the configuration file %s, which\n",QP.configfile);
		printf("  has just been created in the specified picture directory.\n");
		printf("\n");
		printf("  Stop execution so that you can edit the configuration file? (y/n) ");
		fflush(stdout);
		fflush(stdin);
		fgets(yn,1023,stdin);
		rmspace(yn);

		if(yn[0] == 'y')
		{
			printf("\n");
			printf("  Execution halted.\n");
			printf("  Config file is: %s/%s.\n",BaseFilename(QP.rootdir),QP.configfile);
			printf("\n");
			return;
		}

		printf("\n");
	}

	printf("Reading config file: %s/%s\n",
		BaseFilename(QP.rootdir),QP.configfile);
	printf("Thumbnail settings: (quality %d) (target size %dx%d)\n",QP.quality,QP.thumbnailsize,QP.thumbnailsize);
	printf("\n");

	/* Read directory and load images attributes and descriptions */
	printf("Exploring directory structure, reading image attributes\n");
	QPDir_ReadDirectoryTree(QP.rootdir);

	/* Create thumbnails */
	if(QP.override_thumbnail)
	{
		if(QP.write_thumbnails)
			strcpy(yn,"y");
		else
			strcpy(yn,"n");
	}
	else if(QP.prompt_for_thumbnail_creation && !first_time)
	{
		printf("Answer 'y' if images have changed on disk.\n");
		printf("Update thumbnails? [y]: ");
		fflush(stdout);
		fflush(stdin);
		fgets(yn,1023,stdin);
		rmspace(yn);
		printf("\n");
	}
	else
		strcpy(yn,"y");

	if(yn[0] == 'n')
	{
		printf("Reading thumbnail attributes\n");
		QPDir_MakeAllThumbnails(0);
	}
	else
	{
		printf("Creating thumbnails (target size is %dx%d)\n",QP.thumbnailsize,QP.thumbnailsize);
		QPDir_MakeAllThumbnails(1);
	}

	/* Create Gallery */
	printf("Compiling info and writing HTML pages\n");
	HTML_CreateGallery();

	/* Change back to original directory and we're done. */
	chdir(cwd);

	printf("\n");
	printf("Done!\n");
	printf("\n");
	printf("  Edit the following files to customize your gallery:\n");
	printf("\n");
	printf("  File                 Located in        Description\n");
	printf("  -------------------- ----------------- --------------------------\n");
	printf("  %-20s %-17s Information file\n",QP.folder_configfile,"(each folder)");
	printf("  %-20s %-17s Configuration file\n",QP.configfile,BaseFilename(QP.rootdir));
	printf("\n");
	printf("  Use the link \"%s/%s\" to view the gallery.\n",BaseFilename(QP.rootdir),QP.index_html);
	printf("  Have fun!\n");
	printf("\n");
	return;
}

void do_arg(char *arg)
{
	int	i;

	if(arg[0] == '-')
	{
		for(i=1; i<strlen(arg); i++)
		{
			if(arg[i] == 'h')
			{
				printf("%s\n",QP.copyright);
				printf("Usage: qp [switch] <picture directory>\n");
				printf("  -h   This help\n");
				printf("  -c   Write a new config file in picture directory\n");
				printf("  -C   Write a new config file and exit.\n");
#if 0
				printf("  -2   Quiet mode (no output)\n");
				printf("  -1   Semi-Quiet mode (small output)\n");
				printf("  -0   Verbose mode (normal output)\n");
#endif
				printf("  -p   Prompt for descriptions\n");
				printf("  -P   Don't prompt for descriptions\n");
				printf("  -t   Build thumbnails\n");
				printf("  -T   Don't build thumbnails\n");
				printf("\n");
				exit(0);
			}
			else if(arg[i] == 'c')
				QP.write_new_config = 1;
			else if(arg[i] == 'C')
				QP.write_new_config = 2;
			else if(arg[i] == '2')
			{
				QP.override_quiet = 1;
				QP.quiet_level = 2;
			}
			else if(arg[i] == '1')
			{
				QP.override_quiet = 1;
				QP.quiet_level = 1;
			}
			else if(arg[i] == '0')
			{
				QP.override_quiet = 1;
				QP.quiet_level = 0;
			}
			else if(arg[i] == 'p')
			{
				QP.override_description = 1;
				QP.override_date = 1;
				QP.prompt_for_description = 1;
				QP.prompt_for_date = 1;
			}
			else if(arg[i] == 'P')
			{
				QP.override_description = 1;
				QP.override_date = 1;
				QP.prompt_for_description = 0;
				QP.prompt_for_date = 0;
			}
			else if(arg[i] == 't')
			{
				QP.override_thumbnail = 1;
				QP.write_thumbnails = 1;
			}
			else if(arg[i] == 'T')
			{
				QP.override_thumbnail = 1;
				QP.write_thumbnails = 0;
			}
		}
	}
	else
		strcpy(QP.rootdir,arg);
}

