/*
 * NewImg.c
 * for Bioinfo Animal Pictures Archive
 * jskim@bulls.kordic.re.kr
 * prints image info in ascending order of date
 */

#define KOREAN 1

#include "version3/web.h"
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <time.h>
#include <dirent.h>

#define NEW_IMG_DIR "New"
#define HTML_ENG "HTMLSrc/New_eng.html"
#define HTML_HAN "HTMLSrc/New_han.html"


#define CGI_PREVIEW "preview.cgi"
#define CGI_VIEW "ViewImg.cgi"
#define CGI_MAIL "uu2mail.cgi"

typedef struct {
    char	name[256];
    int	size;	/* file size in bytes */
    time_t	time;	/* when added */
}IMAGE;

int int_comp(IMAGE *, IMAGE *);

static int lang = 0;

main()
{
    FILE	*fp;
    
    DIR *dirp;
    struct dirent *direntp;
    struct stat Stat;
    
    IMAGE img[1024];
    char *ptr;
    char line[256];
    char path[256];	/* file path */
    
    int cnt;
    time_t CurTime;

    getentry entries[5];
    int i, x;	/* general purposes */
    int start = 0, end;
    int dspNum = 10;

    
    i = ProcessGetMethod(entries);
    for (x = 0; x <= i; x++) {
	if (!strcasecmp(entries[x].name, "start")) {
	    start = atoi(entries[x].val);
	    continue;
	}
	if (!strcasecmp(entries[x].name, "dspNum")) {
	    dspNum = atoi(entries[x].val);
	    if (dspNum <= 0) dspNum = 10;
	    continue;
	}
	if (!strcasecmp(entries[x].name, "Lang")) {
	    if ( !strcasecmp(entries[x].val, "Korean") ) {
		lang = KOREAN;
	    }
	    continue;
	}
    }
    
    printf("content-type: text/html\n\n");	/* HTML Header */
    
    if (lang == KOREAN)
    	fp = fopen(HTML_HAN, "r");
    else
    	fp = fopen(HTML_ENG, "r");

    if (fp == NULL) { printf("HTML template error\n"); exit(0); }
    
    cnt = 0;
    dirp = opendir( NEW_IMG_DIR );
    while ( (direntp = readdir( dirp )) != NULL ) {
	ptr = (char *) strstr(direntp->d_name, ".");
	if (!strcasecmp(ptr+1, "jpg") || !strcasecmp(ptr+1, "gif")) {
	    sprintf(path, "%s/%s", NEW_IMG_DIR, direntp->d_name);
	    lstat(path, &Stat);
	    strcpy(img[cnt].name, direntp->d_name);
	    img[cnt].size = Stat.st_size;
	    img[cnt].time = Stat.st_ctime;
	    cnt++;
	}
    }
    
    closedir( dirp );
    
    qsort(img, cnt, sizeof(IMAGE), (int *)int_comp);
    time(&CurTime);
    
    /* limit the range */
    if (start < 0 || start > cnt) start = 0;
    if (start + dspNum >= cnt) end = cnt;
    else end = start+dspNum;
    
    /* print the HTML */
    while (fgets(line, 256, fp)) {
	if (!strncasecmp(line, "___IMAGE_LIST___", 16)) {
	    printf("<table width=100% border=0 cellpadding=5>\n");
	    for (i=start; i < end; i++) {
		PrintImgInfo(i, img[i], CurTime);
	    }
	    printf("</table>\n");
	    continue;
	}
	if (!strncasecmp(line, "___PAGE_LIST___", 15)) {
	    printPageList(start, dspNum, cnt);
	}
	else printf("%s", line);
    }
    
    fclose(fp);
    exit(0);
}

int PrintImgInfo(int num, IMAGE img, time_t CurTime)
{
    char path[256];	/* path */
    int days;
    int hours;
    int i, len;
    
    days = (CurTime - img.time)/(24*3600);
    hours = ( (CurTime - img.time)%(24*3600) )/3600;

    /* print thumbnail image name */
    sprintf(path, "PREVIEW/%s", img.name);
    for (i=strlen(path); i > 0; i--) {
	if (path[i] == '.' && !strcasecmp(path+i, ".jpg"))
	    strcpy(path+i, "-s160.jpg");
	else if (path[i] == '.' && !strcasecmp(path+i, ".gif"))
	    strcpy(path+i, "-s160.gif");
    }
    makethumbnail(path, img.name);

    printf( "<tr><td width=162 valign=top>\n");
    if (lang == KOREAN)
    	printf( "<a href=ViewImg.cgi?img=%s/%s&Lang=Korean>",NEW_IMG_DIR, img.name);
    else
    	printf( "<a href=ViewImg.cgi?img=%s/%s>",NEW_IMG_DIR, img.name);

    printf( "<img width=160 border=2 src=%s></a></td>\n", path);
    
    sprintf(path, "%s/%s", NEW_IMG_DIR, img.name);
    printf( "<td valign=top>&nbsp;&nbsp;<font color=brown><b>");
    len = strlen(img.name);
    for (i=0; i<len; i++) {
	if(img.name[i] == '_') printf(" ");
	else if(img.name[i] == '-') printf(" - ");
	else printf("%c", img.name[i]);
    }
    printf( "</b></font>\n<ul>");

    if (lang == KOREAN)
   	 printf( "<li>ÆÄÀÏ Å©±â : %d<i>kb</i>\n", (int)img.size/1024 );
    else
   	 printf( "<li>Size : %d<i>kb</i>\n", (int)img.size/1024 );

    if (lang == KOREAN)
    	printf( "<li>%dÀÏ %d½Ã°£Àü¿¡ Ãß°¡.\n", days, hours );
    else
    	printf( "<li>Added %d days and %d hours ago.\n", days, hours );

/*
    if (lang == KOREAN)
    	printf( "<li><a href=%s?email=&srcFile=%s>ÀüÀÚ¿ìÆíÀ¸·Î ÀÌ¹ÌÁö º¸³»±â(<i>uuencode</i> ÀÌ¿ë)</a>\n", CGI_MAIL, path);
    else
    	printf( "<li><a href=%s?email=&srcFile=%s>Download the full image by e-mail in uuencoded form</a>\n", CGI_MAIL, path);
*/

    if ( (int)img.size/1024 >= 40 ) {
    	if (lang == KOREAN)
    	    printf( "<li><a href=preview.cgi?img=%s&Lang=Korean>³ôÀÌ ¶Ç´Â ³Êºñ°¡ 400ÀÎ ÀÛÀº ±×¸² º¸±â</a>\n", path);
    	else
    	    printf( "<li><a href=preview.cgi?img=%s>Preview the double-sized thumbnail image</a>\n", path);
    }

    if (lang == KOREAN)
    	printf( "<li><a href=ViewImg.cgi?img=%s/%s&Lang=Korean>ÀÌ¹ÌÁö ¿øº» º¸±â</a>",NEW_IMG_DIR, img.name);
    else
    	printf( "<li><a href=ViewImg.cgi?img=%s/%s>View the full image</a>",NEW_IMG_DIR, img.name);

    printf( "</ul></td></tr>\n");

    return 0;
}

int int_comp(IMAGE *i, IMAGE *j)
{
        if ( i->time < j->time)
                return (1);
        if (i->time > j->time)
                return (-1);
        return (0);
}

int makethumbnail(char *thumbnail, char *srcImg)
{
    FILE *fp;
    char buf[512];
    
    fp = fopen(thumbnail, "r");
    if (fp != NULL) return 0;

    sprintf(buf, "/usr/X11R6/bin/convert -frame 5 -geometry 160x160 \"%s/%s\" \"%s\"", NEW_IMG_DIR, srcImg, thumbnail);
    system(buf);
    
    return 0;
}

int printPageList(int start, int dspNum, int cnt)
{
    int i;
    
    if (start > 0) {
    	if (lang == KOREAN)
	    printf("[<a href=New.cgi?start=%d&dspNum=%d&Lang=Korean>¹Ù·ÎÀü</a>]\n", start-dspNum, dspNum);
    	else
	    printf("[<a href=New.cgi?start=%d&dspNum=%d>Prev</a>]\n", start-dspNum, dspNum);
    }
    for (i=0; i < cnt; i+= dspNum) {
	if (start == i) printf(" <font color=red>%d</font>\n", i/dspNum+1);
	else {
	    printf("<a href=New.cgi?start=%d", i);
	    if (lang == KOREAN) {
	   	printf("&dspNum=%d&Lang=Korean>%d</a>\n", dspNum, i/dspNum+1);
	    }
	    else {
	   	printf("&dspNum=%d>%d</a>\n", dspNum, i/dspNum+1);
	    }
	}
    }
    if (start+dspNum < cnt) {
    	if (lang == KOREAN)
	    printf("[<a href=New.cgi?start=%d&dspNum=%d&Lang=Korean>´ÙÀ½</a>]\n", start+dspNum, dspNum);
    	else
	    printf("[<a href=New.cgi?start=%d&dspNum=%d>Next</a>]\n", start+dspNum, dspNum);
    }
}
