#include <stdio.h>
#include <stdlib.h>
#include <string.h>

unsigned char key[51] = "%$#(L;Psle-0+x~!#@$7pqkdek935..z,dlekfoL.Gs.Cyu.MB";

void decode(char *filename, int len) {
  FILE *filep = fopen(filename, "rb");
  int c, i = 0;

  if (filep == NULL) {
    fprintf(stderr, "I can't find file %s\n", filename);
    exit (2);
  }

  while ((c = fgetc(filep)) != -1) {
    c ^= key[i];

    if (c >= 'A' &&  c <= 'Z') { c ^= 32; } 

    if (c != 32) { fputc((unsigned char) c, stdout); }

    if (++i == len) {
      i = 0;
      fputc((unsigned char) 10, stdout);
    }
  }
}

void main (int argc, char ** argv) {
  int i, len = 50;

  for (i = 0; i < 50; i++) { key[i]; }

  if (argc > 1) {
    if (argv[1][0] != '-') { 
      char *filename = argv[1];
      char *end = argv[1];
      if (strlen(filename) > 8) {
	end = filename + strlen(filename) - 8;
	if (!strcmp(end,"ceml.exf")) { len = 30; }
	else if (!strcmp(end,"ceng.exf")) { len = 50; }
	else if (!strcmp(end,"cwrd.exf")) { len = 15; }
	else if (!strcmp(end,"csew.exf")) { len = 15; }
	else if (!strcmp(end,"csvr.exf")) { len = 50; }
	else {
	  fprintf(stderr, "I don't know how to decode file %s\n", filename);
	  exit (1);
	}
	decode(filename, len);
      }
    }
    else {
      char *filename = argv[2];
      int len = atoi(argv[1]+1);
      decode(filename, len);
    }
  }
}
