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

const char VERSION_STRING[]="GIFconv v1.0.12  14/05/1997";
FILE *f1,*f2;
char c,s[36];
const char iden[]="!þkFILE IDENTITY";
long l,f;
int LEN;

void main(int argc, char *argv[])
{
  if (argc<3 || !strcmp(argv[1],argv[2])) {
    fprintf(stderr,"GIFCONV <infile> <outfile>\n");
    return;
  }
  LEN=strlen(iden);
  f1=fopen(argv[1],"rb");
  fseek(f1,0,SEEK_END);
  l=ftell(f1)-350;
  if (l<0) {
    printf("This file is not a GIFCON file. Nothing to do.\n");
    fclose(f1);
  }
  fseek(f1,l,SEEK_SET);
  fgets(s,LEN+1,f1);
  s[LEN]='\0';
  if (!strcmp(s,iden)) {
    printf("Hmm. It seems to be a transparent background .GIF file.\n");
    l--;
  }
  else {
    rewind(f1);
    for (f=0; f<LEN; f++) s[f]=0;
    for (; f<l; f++) {
      if (!strncmp(s+f%LEN,iden,LEN)) break;
      s[f%LEN]=getc(f1);
      s[f%LEN+LEN]=s[f%LEN];
    }
    if (f>=l) {
      printf("This file does not seem to be a GIFCON file. Nothing to do.\n");
      fclose(f1);
      return;
    }
    l=f-2*LEN-1;
    printf("Yep. It seems to be an animated .GIF file.\n");
  }
  rewind(f1);
  f2=fopen(argv[2],"wb");
  f=l;
  for (; l>=0; l--) putc(getc(f1),f2);
  fseek(f1,0,SEEK_END);
  l=ftell(f1)-f;
  printf("Gain: %ld bytes, %d%%.\n",l,100*l/(f+l));
  fclose(f1);
  fclose(f2);
}