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

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=0,white,returnval;

  extra_path = getenv("PATH_INFO");
  parse_extra(extra_path,paramlist);

  /* use param list to generate possibly rotated piece and return */
  output_image = gdImageCreate(102,102);
  white = gdImageColorAllocate(output_image,255,255,255);
  gdImageFill(output_image,10,10,white);
  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,
              (output_image->sx-piece_image->sx)/2,
              (output_image->sy-piece_image->sy)/2,
              0,0,
              piece_image->sx,piece_image->sy);
  gdImageDestroy(piece_image);
  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);
}

