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

const char idx_data[] = "# VobSub index file, v7 (do not modify this line!)\npalette: 000000, ffffff, 000000, 000000, 828282, 828282, 828282, ffffff, 828282, bababa, 828282, 828282, 828282, 828282, 828282, 828282\n\nlangidx: 0\n\nid: en, index: 0\ntimestamp: 00:00:00:000, filepos: 000000000\n";

unsigned char sub_data[0x800] = {
/* 0x00 */  0x00, 0x00, 0x01, 0xba, 0x44, 0x02, 0xc4, 0x82,  0x04, 0xa9, 0x01, 0x89, 0xc3, 0xf8, 0x00, 0x00,
/*
 *          0x00, 0x00, 0x01 = "0x00 0x00 0x01"
 *                            0xba = packet start code
 *                                  0x44 = mpeg version 4
 *                                                                                               0x00, 0x00,
 */
/* 0x10 */  0x01, 0xbd, 0x00, 0x5c, 0x81, 0x80, 0x05, 0x21,  0x00, 0x01, 0x00, 0x01, 0x20, 0x00, 0x53, 0x00,
/*
 *          0x01 = "0x00 0x00 0x01"
 *                0xbd = packet
 *                      0x00, 0x5c = fragment size = 0x005c = 92 bytes
 *                                  0x81 = .VOB stream
 *                                        0x80 = pts_flags
 *                                              0x05 = header length = 5 bytes
 *                                                    0x21,  0x00, 0x01, 0x00, 0x01 = header
 *                                                                                   0x20 = aid
 *                                                                                         0x00, 0x53 = SPDSZ
 *                                                                                                     0x00,
 */
/* 0x20 */  0x35, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24,  0x24, 0x24, 0x24, 0x24, 0x24, 0x87, 0xd7, 0x80,
/*
 *          0x35 = SP_DCSQTA = 0x0035 SP_DCSQT is at 0x001d + 0x0035 = 0x0052
 */
/* 0x30 */  0x87, 0xd7, 0x80, 0x86, 0xf6, 0x80, 0x24, 0x24,  0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24, 0x24,
/* 0x40 */  0x24, 0x24, 0x24, 0x24, 0x87, 0xe7, 0x80, 0x87,  0xd7, 0x80, 0x87, 0xd7, 0x80, 0x24, 0x24, 0x24,
/* 0x50 */  0x00, 0x00, 0x00, 0x00, 0x00, 0x4d, 0x01, 0x03,  0x32, 0x10, 0x04, 0xff, 0xf0, 0x05, 0x1d, 0xb1,
/*
 *
 *                      0x00, 0x00 = SP_DCSQ_STM
 *                                  0x00, 0x4d = SP_NXT_DCSQ_SA = 0x004d next SP_DCSQT is at 0x1d + 0x4d = 0x6A
 *                                              0x01 = STA_DSP
 *                                                    0x03,  0x32, 0x10 = SET_COLOR
 *                                                                       0x04, 0xff, 0xf0 = SET_CONTR
 *                                                                                         0x05, 0x1d, 0x1b,
 */
/* 0x60 */  0xe3, 0x28, 0x92, 0xab, 0x06, 0x00, 0x04, 0x00,  0x1c, 0xff, 0x00, 0x57, 0x00, 0x4d, 0x02, 0xff,
/*
 *          0xe3, 0x28, 0x92, 0xab = SET_DAREA
 *                                  0x06, 0x00, 0x04, 0x00,  0x1c = SET_DSPXA
 *                                                                 0xff = CMD_END
 *                                                                       0x00, 0x57 = SP_DCSQ_STM
 *                                                                                   0x00, 0x4d = SP_NXT_DCSQ_SA
 *                                                                                               0x02 = STP_DSP
 *                                                                                                     0xff = CMD_END
 */
/* pad to size 0x800 with byte value 0xff */
};

#define FRAGMENT_SIZE        0x05c
#define SPDSZ                0x053
#define FRAGMENT_SIZE_OFFSET 0x012 /* 2 bytes, big-endian (most significant byte first) */
#define SPDSZ_OFFSET         0x01d /* 2 bytes, big-endian (most significant byte first) */
#define PAD_OFFSET           0x070
#define PAD_SIZE             0x790

int
write_file(
  const char* name,
  const void *data,
  size_t size) {

  int result = -1;
  FILE* f = NULL;

  f = fopen(name, "w");
  if (f == NULL) {
    fprintf(stderr, "could not open file '%s'\n", name);
    goto cleanup;
  }

  if (fwrite(data, size, 1, f) != 1) {
    fprintf(stderr, "could not write to file '%s'\n", name);
    goto cleanup;
  }
  
  if (fclose(f) != 0) {
    fprintf(stderr, "could not close file '%s'\n", name);
    goto cleanup;
  }
  f = NULL;

  result = 0;
cleanup:
  if (f) {
    fclose(f);
  }
  return result;
}

int
main(int argc, char**argv) {
  int result = 1;
  char* name = NULL;

  if (argc != 2) {
    fprintf(stderr, "Usage: %s NAME\n", argv[0]);
    return 1;
  }

  memset(sub_data + PAD_OFFSET, 0xff, PAD_SIZE);

  name = malloc(strlen(argv[1]) + 80);
  if (name == NULL) {
    fprintf(stderr, "could not allocate memory for file name\n");
    goto cleanup;
  }

  for (unsigned fragment_size = 0; fragment_size <= FRAGMENT_SIZE; fragment_size++) {
    const unsigned spdsz = (fragment_size >= (FRAGMENT_SIZE - SPDSZ)) ?
      (fragment_size - (FRAGMENT_SIZE - SPDSZ)) : 0;

    sub_data[FRAGMENT_SIZE_OFFSET + 0] = (unsigned char) ((fragment_size >> 8) & 0xFF);
    sub_data[FRAGMENT_SIZE_OFFSET + 1] = (unsigned char) (fragment_size & 0xFF);

    sub_data[SPDSZ_OFFSET + 0] = (unsigned char) ((spdsz >> 8) & 0xFF);
    sub_data[SPDSZ_OFFSET + 1] = (unsigned char) (spdsz & 0xFF);

    sprintf(name, "%s-%u%s", argv[1], fragment_size, ".idx");
    if (write_file(name, idx_data, sizeof(idx_data)) == -1) {
      goto cleanup;
    }
    sprintf(name, "%s-%u%s", argv[1], fragment_size, ".sub");
    if (write_file(name, sub_data, sizeof(sub_data)) == -1) {
      goto cleanup;
    }
  }
  result = 0;

cleanup:
  if (name) {
    free(name);
  }
  return result;
}

