#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

#define WEBROOT "/usr/local/apache2/htdocs/www.gtoal.com"
#define BINDIR "/usr/local/bin/"

#define TRUE (0==0)
#define FALSE (0!=0)

/* test url: http://www.gtoal.com/cgi-bin/image/athome/edinburgh/scans/skimp_ii/split/skimp047-left.png */

static char *pnmto[] = { /* name of conversion program and output mime type */
  "jpg/jpeg", "jpeg/jpeg", "gif/gif", "tiff/tiff", "png/png",
  NULL
};

static char *topnm[] = { /* acceptible extensions for files on disk */
  "djvu", "png", "jpg", "jpeg", "gif", "tiff", "pnm",
  NULL /* they are listed in preferred order of size in case > 1 on disk */
};

void nonmac_htmlassert(int test, char *desc) {
  if (test) return;

  fprintf(stdout, "Content-Type: text/html; charset=ISO-8859-1\r\n\r\n");
  fprintf(stdout, "<HTML>\n");
  fprintf(stdout, "<BODY>\n");
  fprintf(stdout, "Assertion failed: %s\n", desc);
  fprintf(stdout, "</BODY>\n");
  fprintf(stdout, "</HTML>\n");
  fflush(stdout);
  exit(0);
}
#define htmlassert(s) nonmac_htmlassert(s, #s)

int main(int argc, char **argv)
{
  int i;
  FILE *f;
  char *orig_path, *s, *ext, *basename, *command, *diskfile, *diskext, *mime;

  /* Extract URL from environment, find base file, convert on the fly */
  s = getenv("PATH_INFO");

  orig_path = strdup(s);
  htmlassert(orig_path != NULL);
  ext = strrchr(s, '.');
  if (ext == NULL || strchr(ext, '/') != NULL) {
    /* any error punts to the non-filtered URL which should then
       generate the appropriate error message */
    fprintf(stdout, "%s%s\r\n\r\n","Refresh: 0;url=", orig_path);exit(0);
  }
  basename = malloc((ext-s)+32);
  command = malloc((ext-s)+512);
  diskfile = malloc((ext-s)+512);
  htmlassert(basename != NULL && command != NULL && diskfile != NULL);
  strncpy(basename, s, ext-s); basename[ext-s] = '\0';
  ext += 1; /* drop the "." */

  s = basename;
  strsep(&s, "\\'\"`$.<>&;%~- "); /* eliminate obvious shell escapes. */
  if (s != NULL) {             /* (did I miss any?) */
    fprintf(stdout, "%s%s\r\n\r\n","Refresh: 0;url=", orig_path);exit(0);
  }

  i = 0;
  while ((s = topnm[i++]) != NULL) {
    sprintf(diskfile, "%s%s.%s", WEBROOT, basename, s);
    if ((f=fopen(diskfile, "r")) != NULL) {fclose(f); break;}
    *diskfile = '\0';
  }
  if (*diskfile == '\0') {
    fprintf(stdout, "%s%s\r\n\r\n","Refresh: 0;url=", orig_path);exit(0);
  }
  i = 0;
  while ((s = pnmto[i++]) != NULL) {
    mime = strchr(s, '/');
    htmlassert(mime != NULL);
    *mime++ = '\0'; /* writable strings */
    diskext = strrchr(diskfile, '.')+1;
    if (strcmp(ext, s) == 0) {
      fprintf(stdout,
        "Content-Type: image/%s\r\n\r\n", mime);
      fflush(stdout);
      sprintf(command,
        "/bin/sh -c \"%s%stopnm < %s 2> /dev/null|%spnmto%s  2>/dev/null\"",
            BINDIR,diskext,       diskfile,       BINDIR,ext);
      system(command); fflush(stdout);
      exit(0);
    }
  }

  fprintf(stdout, "%s%s\r\n\r\n","Refresh: 0;url=", orig_path);
  exit(0);
}