/* MKSYS 1.0 by Eli0t/cONTORTIOn (C) 1996 - FREEWARE */

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

/*

  ERROR CODES:  (stupid tactic, but works!)
  =========================================

  00 - SYSEX data O.K. (probably)
  01 - SYSEX data bad! (bad checksum)
  02 - no parameters?! (i.e. help lines)
  03 - I/O error! (disk full? write protected files?)
  04 - bad length?! (no sense to compute a checksum!)

  =========================================

*/


main(int argc, char **argv)
{
  FILE *vstup, *vystup;
  unsigned char pole[16384];
  unsigned int length, suma, i, oldsuma;

  suma=0;
  oldsuma=0;

       if(argc<2)
  {
     printf("\n \233\061\155Usage:\n         \233\062\155mksys filename\233\060\155\n\n");
     printf(" \233\061\155Eli0t Studios Project - \233\060\155\233\063\155eliot@sci.muni.cz \233\060\155\233\061\155- (C) 1996 cONTORTIOn\n");
     printf(" \233\063\063\155===============================================================\233\060\155\n");
     printf("  \233\061\155Converts DX 4096,4097,4224,5232 to 4104 and K4 15114 to 15123\233\060\155\n\n");
     return(2); /* bad number of parameters */
  }

  vstup=fopen(argv[1],"r");
       if (vstup==NULL)
  {
     printf("\233\061\155%s\233\060\155 - I/O error!\n",argv[1]);
     return(3);
  }

  length=fread(pole,(size_t) 1, (size_t) 16384, vstup);
  fclose(vstup);

       if (length==4104)
  {
       for(i=6;i<4103;i++) suma+=pole[i];
       suma&=(int) 127;
       if (suma!=0)
       {
          printf("\233\061\155%s\233\060\155 - bad \233\062\155CHECKSUM\233\060\155\007!\n",argv[1]);
          return(1);
       }
       else
       {
          printf("\233\062\155%s\233\060\155 - already converted, checksum O.K.\n",argv[1]);
          return(0);
       }
  }

       if (length==4097) /* the checksum is IN! */
  {
       oldsuma=pole[4096]+128;length=4096;
  }

       if (length==5232)  /* the checksum is IN! */
  {
       for(i=0;i<4097;i++) pole[i]=pole[i+6];
       oldsuma=pole[4096]+128;length=4096;
  }

       if (length==4224) length=4096; /* weirdo?! */

       if ((length==15114) || (length==4096)) {} else
  {
       printf("\233\061\155%s\233\060\155 - bad \233\062\155LENGTH\233\060\155!\n",argv[1]);
       return(4); /* bad length! */
  }

       if (length==4096)
{
  for(i=0;i<length;i++) suma+=pole[i];
  suma&=(int) 127; suma=128-suma;
  if (oldsuma!=0)
  {
     if (oldsuma!=(suma + 128))
     {
          printf("\233\061\155%s\233\060\155 - bad \233\062\155CHECKSUM\233\060\155\007!\n",argv[1]);
          return(1);
     }
  }
}

  vystup=fopen(argv[1],"w");
       if (vystup==NULL)
  {
     printf("\233\061\155%s\233\060\155 - I/O error!\n",argv[1]);
     return(3);
  }

     if (length==4096)
     {
       printf("\233\062\155%s\233\060\155\n",argv[1]);
       fprintf(vystup,"\360\103%c\011\040%c",0,0);
       fwrite(pole,(size_t) 1, (size_t) 4096, vystup);
       fprintf(vystup,"%c\367", suma);
     }
     else
     {
       printf("\233\062\155%s\233\060\155\n",argv[1]);
       fprintf(vystup,"\xf0\x40%c\x22%c\x04%c%c",0,0,0,0);
       fwrite(pole,(size_t) 1, (size_t) 15114, vystup);
       fprintf(vystup,"\367");
     }
  fclose(vystup);
  return(0);
}
