#include <stdio.h>
#include <stdlib.h>
#include "gd.h"

FILE *logfile;

struct parameter 
  {
  char piece[256];
  char rotate[256];
  char x[256];
  char y[256];
  }

html_error()
  {
  printf("Content-type: text/html\n\n");
  printf("File open error");
  exit(1);
  }

void parse_extra(char *extra_path, struct parameter paramlist[])
  {
  char *charp,*slash1,*slash2,*slash3,*slash4;
  int i=0,j,k;

  for(j=1;j<strlen(extra_path);j++)
    {
    if((extra_path[j-1] == '/')&&(extra_path[j] == '/'))
      {
      for(k=j;k<strlen(extra_path);k++)
        {
        extra_path[k] = extra_path[k+1];
        }
      }
    }
  charp = extra_path;
  charp++;

  while(i<255)
    {
    slash1 = strchr(charp,'/');
    if(slash1)
      slash2 = strchr(slash1+1,'/');
    else
      html_error();
    if(slash2)
      slash3 = strchr(slash2+1,'/');
    else
      {
      strcpy(paramlist[i].piece,charp);
      strcpy(paramlist[i].rotate,"ISMAP");
      strcpy(paramlist[i].x,"ISMAP");
      strcpy(paramlist[i].y,"ISMAP");
      strcpy(paramlist[i+1].piece,"LAST");
      break;
      }
    if(slash3)
      slash4 = strchr(slash3+1,'/');
    else
      html_error();

    *slash1=0;
    *slash2=0;
    *slash3=0;

    strcpy(paramlist[i].piece,charp);
    strcpy(paramlist[i].rotate,slash1+1);
    strcpy(paramlist[i].x,slash2+1);

    if(!slash4)
      {
      strcpy(paramlist[i].y,slash3+1);
      strcpy(paramlist[i+1].piece,"LAST");
      break;
      }

    if(strchr(slash4+1,'/'))
      {
      *slash4=0;
      strcpy(paramlist[i].y,slash3+1);
      charp=slash4+1;
      i++;
      }
    else
      {
      *slash4=0;
      strcpy(paramlist[i].y,slash3+1);
      strcpy(paramlist[i+1].piece,slash4+1);
      strcpy(paramlist[i+1].rotate,"ISMAP");
      strcpy(paramlist[i+1].x,"ISMAP");
      strcpy(paramlist[i+1].y,"ISMAP");
      strcpy(paramlist[i+2].piece,"LAST");
      break;
      }
    }
  }

main(int argc, char *argv[])
  {
  char graphic_char, *extra_path, piece_file_name[256], *comma, 
       unique_filename[256], command[256];
  FILE *blank_field_fp, *piece_fp;
  struct parameter paramlist[256];
  gdImagePtr output_image, input_image, piece_image;
  int j,white,returnval;

  if(argc != 2) /* not called as imagemap */
    {
    extra_path = getenv("PATH_INFO");
    if(extra_path)                       /* If called with params, parse them.*/
      parse_extra(extra_path,paramlist);
    else                                 /* If not, return blank field.       */
      {
      output_image = gdImageCreate(288,288);
      white = gdImageColorAllocate(output_image,255,255,255);
      gdImageFill(output_image,0,0,white);
      gdImageColorTransparent(output_image,white);

      printf("Content-type: multipart/x-mixed-replace;boundary=ThisRandomString\n");
      printf("\n--ThisRandomString\n");
      printf("Content-type: image/gif\n\n");
      gdImageGif(output_image,stdout);
      printf("\n--ThisRandomString\n");
      gdImageDestroy(output_image);
      exit(1);
      }
    /* use param list to generate Tangoes field and return */
    output_image = gdImageCreate(288,288);
    white = gdImageColorAllocate(output_image,255,255,255);
    gdImageFill(output_image,0,0,white);
    gdImageColorTransparent(output_image,white);

    j=0;
    while((strcmp(paramlist[j].piece,"LAST"))&&(j<255))
      {
      sprintf(piece_file_name,"/home/users/c/crywalt/public_html/tangoes/tang%ss.gif",paramlist[j].piece);
      if(!(piece_fp = fopen(piece_file_name,"r")))
        html_error();

      if(strcmp(paramlist[j].rotate,"0"))
        {
        srand(atoi(paramlist[j].rotate)+
              atoi(paramlist[j].x)+
              atoi(paramlist[j].y));
        sprintf(unique_filename,
                "%d",
                rand());
        sprintf(command,
                "/usr/local/bin/convert -bordercolor white -rotate %s -map /home/users/c/crywalt/public_html/tangoes/tang%ss.gif /home/users/c/crywalt/public_html/tangoes/tang%ss.gif %s",
                paramlist[j].rotate,
                paramlist[j].piece,
                paramlist[j].piece,
                unique_filename);
        system(command);
        sprintf(command,
                "/usr/local/bin/giftool -B -rgb white %s",
                unique_filename);
        system(command);
        fclose(piece_fp);
        if(!(piece_fp = fopen(unique_filename,"r")))
          html_error();
        }

      piece_image = gdImageCreateFromGif(piece_fp);
      fclose(piece_fp);
      if(strcmp(paramlist[j].rotate,"0"))
        {
        sprintf(command,
                "rm %s",
                unique_filename);
        system(command);
        }
      gdImageCopy(output_image,
                  piece_image,
                  atoi(paramlist[j].x)-(piece_image->sx/2),
                  atoi(paramlist[j].y)-(piece_image->sy/2),
                  0,0,
                  piece_image->sx,piece_image->sy);
      gdImageDestroy(piece_image);
      j++;
      }
    printf("Content-type: multipart/x-mixed-replace;boundary=ThisRandomString\n");
    printf("\n--ThisRandomString\n");
    printf("Content-type: image/gif\n\n");
    gdImageGif(output_image,stdout);
    printf("\n--ThisRandomString\n");
    gdImageDestroy(output_image);
    }
  else          /* called as imagemap */
    {
    extra_path = getenv("PATH_INFO");
    comma = strchr(argv[1],',');
    if(!comma)
      html_error();
    *comma = 0;
    printf("Content-type: text/html\n\n");
    printf("<BODY BGCOLOR=\"#FFFFFF\" TEXT=\"#000000\">\n");
    printf("<CENTER>\n");
    printf("<TABLE BORDER=1>\n");
    printf("<TR>\n");
    printf("<TD COLSPAN=2 ALIGN=CENTER><IMG SRC=\"http://www.westnet.com/~crywalt/tangoes/card0001s.gif\"></TD>\n");
    printf("</TR>\n");
    printf("<TR>\n");
    printf("<TD WIDTH=72 VALIGN=BOTTOM>Choose a piece</TD>\n");
    printf("<TD WIDTH=288 ROWSPAN=2><IMG SRC=\"http://www.westnet.com/cgi-bin/crywalt/tangoes.cgi%s/%s/%s\"></TD>\n",
           extra_path,
           argv[1],
           comma+1);
    printf("</TR>\n");
    printf("<TR>\n");
    printf("<TD WIDTH=72 VALIGN=CENTER><A HREF=\"http://www.westnet.com/cgi-bin/crywalt/imagemap/~crywalt/tangoes/tangoes.map%s/%s/%s\"><IMG BORDER=0 SRC=\"http://www.westnet.com/cgi-bin/crywalt/tangmap.cgi%s/%s/%s\" ISMAP></A></TD>\n",
           extra_path,
           argv[1],
           comma+1,
           extra_path,
           argv[1],
           comma+1);
    printf("</TR>\n");
    printf("<TR>\n");
    printf("<TD COLSPAN=2 ALIGN=CENTER><IMG SRC=\"http://www.westnet.com/~crywalt/tangoes/sltn0001s.gif\"></TD>\n");
    printf("</TR>\n");
    printf("</TABLE>\n");
    printf("</CENTER>\n");
/*
    printf("<INPUT TYPE=\"hidden\" NAME=\"tangoes\" VALUE=\"tangoes.cgi%s/%s/%s\">\n",
           extra_path,
           argv[1],
           comma+1);
*/
    }
  }

