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

#define HEADSIZE 200

FILE *fin, *fout_h;
FILE *fout_def;
long ofs = HEADSIZE * sizeof(long);
int help_cnt = 0;
 
char* upper(char* s)
{
char * ret = s;

 while (*s)
 {
  if (islower(*s)) *s = toupper(*s);
  s++;
 }
 return ret;
}

void write_incl(int index, char* str)
{
char *p_sp, *p_tab, *p_name;

 str[strlen(str)-1] = 0;
 p_sp = strrchr(str, ' ');
 p_tab = strrchr(str, '\t');
 if (!p_sp && !p_tab) p_name = str;
 else p_name = ((p_sp > p_tab) ? p_sp : p_tab)+1;
 fprintf(fout_h,"#define HELP_%s %d\n", upper(p_name), index);
}

void trans(void)
{
char inpstr[BUFSIZ];
size_t len;
long pos;

 while( fgets( inpstr, BUFSIZ, fin ) !=NULL )
 {
  if (help_cnt == HEADSIZE)
  {
   fprintf(stderr, "header too small\n");
   exit(1);
  } 
  pos = help_cnt * sizeof(long);
  if (fseek(fout_def, pos, SEEK_SET) != 0)
  {
   fprintf(stderr, "seek to offset failed\n");
   exit(1);
  }
  if (fwrite( &ofs, sizeof(long),1,fout_def) != 1)
  {
   fprintf(stderr, "error writing offset\n");
   exit(1);
  }
  write_incl(help_cnt, inpstr);
  while( inpstr[0] != '$' )
  {
   fgets( inpstr, BUFSIZ, fin );
   if (fseek(fout_def, ofs, SEEK_SET ) != 0)
   {
    fprintf(stderr, "lseek to start string failed\n");
    exit(1);
   }
   len = strlen(inpstr);
   if (fwrite( inpstr, len,1,fout_def) != 1)
   {
    fprintf(stderr, "error writing string\n");
    exit(1);
   }
   ofs += len;
  }
  printf(".");
  fflush(stdout);
  help_cnt++;
 }
}

main( argc, argv )
int argc;
char *argv[];
{
int i = 0, outnumber;
long zero=0;
void trans();
char *p_sl, *p_dot, *filename;

 outnumber = argc-1;

 if ( argc < 3 )
 {
  fprintf(stderr, "usage: %s input1 input2 ... outputfile\n",
   argv[0]);
  exit(1);
 }

/* if((fout_def = creat( argv[outnumber],0666)) == -1)*/
 if((fout_def=fopen(argv[outnumber],"wb+"))==NULL)
 {                         
   fprintf(stderr, "%s could not open %s\n", argv[0],
   argv[outnumber] );
  exit( 1 );
 }

 if ((filename = (char *)malloc(strlen(argv[outnumber])+3)) == NULL)
 {
  fprintf(stderr, "%s could not allocate memory\n", argv[0]);
  exit(1);
 }
 strcpy(filename, argv[outnumber]);
 if ((p_dot = strrchr(filename,'.')) != NULL)
 {
  p_sl = strrchr(filename,'\\');
  if (p_sl)
  {
   if (p_dot - p_sl > 0) *p_dot = 0; 
  }
  else *p_dot = 0;
 }
 strcat(filename, "_h");

 if( (fout_h = fopen(filename,"w+")) == NULL )
 {
  fprintf(stderr, "%s could not open %s\n", argv[0],
   filename );
  exit( 1 );
 }

 for ( i = 0; i < HEADSIZE; i++ )
  fwrite( &zero, sizeof(long),1,fout_def);

 fprintf(fout_h, "#define HEADSIZE %ld\n", (long)HEADSIZE);

 for (argc = 1; argc < outnumber; argc++)
 {
  if( (fin = fopen(argv[argc],"r")) == NULL )
  {
   fprintf(stderr,"%s could not open %s\n", argv[0],
    argv[argc] );
   exit( 1 );
  }
  else
  {
   printf("transforming %s to %s", argv[argc],
    argv[outnumber] );
   fflush(stdout);
  }
  trans();
  printf("\n");
  fclose( fin );
 }
 fclose (fout_def);
 fclose (fout_h);
 printf("done\n");
 return 0;
}
