/*

themeindex.c


*/

#include <stdio.h>

#define YES 1
#define NO  0
#define URL "http://themes.netfx-2000.com/"

/* Grobal */
FILE *out;
int n, sum;
char dirname[40], fname[40];

main()
{
FILE *inp;
char command[120], filename[40], buf[40];
int  i, j;

system("pwd>list_dir");
sprintf(fname, "%s", "list_dir");    
if((inp=fopen(fname, "r"))==NULL){
    printf("Can't open <%s>.\n", fname);
    exit(0);
    }
fgets(buf, 80, inp);
for(i=strlen(buf); buf[i]!='/'; i--);
i=i+1;
for(j=0; buf[i]!='\n'; i++,j++)
    dirname[j]=buf[i];
dirname[j]='\0';
fclose(inp);

system("ls *.jpg>list_jpg");

sprintf(filename, "list_jpg");
if((inp=fopen(filename, "r"))==NULL){
    printf("Can't open <%s>.\n", filename);
    exit(0);
    }
    
sum=0;
while(fgets(buf,40,inp)!=NULL)
    sum++;
fclose(inp);

if((inp=fopen(filename, "r"))==NULL){
    printf("Can't open <%s>.\n", filename);
    exit(0);
    }
n=1;
while(fgets(buf,40,inp)!=NULL){
    buf[strlen(buf)-1]='\0';
    get_fname(buf);
    if(n==1 || n%6==1)
        html_open();
    pictable();
    if(n%6==0)
        html_close();
    n++;
    }
if(sum%6!=0)
    html_close();
}

html_open()
{
char filename[40];
int page, lastpage;

page=n/6+(n%6>0); lastpage=sum/6+(sum%6>0);

if(page==1)
    sprintf(filename, "index.html");
else
    sprintf(filename, "index_%d.html", page);
if((out=fopen(filename, "w"))==NULL){
    printf("Can't open <%s>.\n", filename);
    exit(0);
    }
fprintf(out, "<html>\n");
fprintf(out, "<table width=\"620\"><tr><td align=\"center\" bgcolor=\"#DDDDDD\"><h3>");
fprintf(out, "%s - %d of %d page", dirname, page, lastpage);
fprintf(out, "</h3></td></tr></table>\n");

fprintf(out, "<html><table>\n");
fprintf(out, "<tr>\n");
}

html_close()
{
int page, lastpage;

page=n/6+(n%6>0); lastpage=sum/6+(sum%6>0);

fprintf(out, "</tr>\n");
fprintf(out, "</table><br>\n");
fprintf(out, "<table width=\"620\"><tr><td align=\"center\" bgcolor=\"#DDDDDD\"><h3>");
fprintf(out, "<a href=\"../index.html\">Home</a>");
if(page==lastpage && page>1){
    fprintf(out, "  |  ");
    if(page==2)
        fprintf(out, "<a href=\"index.html\">Prev</a>");
    else
    fprintf(out, "<a href=\"index_%d.html\">Prev</a>", page - 1);
    fprintf(out, "  |  ");
    fprintf(out, "<a href=\"index.html\">Top</a>");
    }
else if(page==1 && lastpage>1){
    fprintf(out, "  |  ");
    fprintf(out, "<a href=\"index_%d.html\">Last</a>", lastpage);
    fprintf(out, "  |  ");
    fprintf(out, "<a href=\"index_%d.html\">Next</a>", page + 1);
    }
else if(page>1){
    fprintf(out, "  |  ");
    if(page==2)
        fprintf(out, "<a href=\"index.html\">Prev</a>");
    else
        fprintf(out, "<a href=\"index_%d.html\">Prev</a>", page - 1);
    fprintf(out, "  |  ");
    fprintf(out, "<a href=\"index_%d.html\">Next</a>", page + 1);
    }
    
fprintf(out, "</h3></td></tr></table>\n");
fprintf(out, "</p>\n</html>\n");
fclose(out);
}

pictable()
{
fprintf(out, "<td align=\"center\">");
fprintf(out, "<a href=\"%s", URL);
fprintf(out, "wmaker/files/%s.tar.gz\">", fname);
fprintf(out, "<img src=\"%s.jpg\" ", fname);
fprintf(out, "width=\"200\" height=\"150\" border=\"0\"></a><br>", fname);
fprintf(out, "[%d] %s</td>\n", n, fname);
if(n%3==0 && n>0)
    fprintf(out, "</tr>\n<tr>\n");
}

get_fname(char *buf)
{
int i, j, last;

for(i=0, j=0; buf[i]!='.'; i++, j++)
    fname[j]=buf[i];
fname[j]='\0';
}