/************* 
customizer for KioskCD 
v2.6 for Windows
March 2005

Visit kioskcd.com

Seeks to known point in ISO file and fwrites new config file,
then fills up the rest of the reserved file space with empty lines.

Source code licensed under Gnu General Public License.
(gnu.org).

*****************/

#define CFG_OFFSET 0214000L

#include <stdio.h>

main(int argc, char *argv[])
{
	int i, c, first;
	long rc;
	char cfgname[200], isoname[200], *cmd, str[500];
	FILE *cfgfp, *isofp;

	i = 0;
	cmd = strrchr(argv[i++], '\\') + 1;
	if (! cmd)
		cmd = argv[0];
	if (argc < 3)  {
		printf("Usage: %s config-file iso-file\n", cmd);
		printf("press Enter to exit program\n");
		getchar();
		return 1;
		}

	strcpy(cfgname, argv[i++]);
	strcpy(isoname, argv[i++]);

	printf("Apply config file %s\n\tto ISO file %s ([y]/n)? ",
							cfgname, isoname);
	c = getchar();
	if (c == '\n')
		c = 'y';
	if (c != 'y')  {
		printf("No action.\n");
		printf("Usage: %s config-file iso-file\n", cmd);
		printf("press Enter to exit program\n");
		getchar();
		return 1;
		}

	cfgfp = fopen(cfgname, "r");
	if (! cfgfp)  {
		printf("Error: can't open %s for reading\n", cfgname);
		printf("Usage: %s config-file iso-file\n", cmd);
		printf("press Enter to exit program\n");
		getchar();
		return 1;
		}
	isofp = fopen(isoname, "rb+");
	if (! isofp)  {
		printf("Error: can't open %s for update\n", isoname);
		printf("Usage: %s config-file iso-file\n", cmd);
		printf("press Enter to exit program\n");
		getchar();
		return 1;
		}
	fseek(isofp, CFG_OFFSET, SEEK_SET);
	rc = 0L;
	while (fgets(str, 500, cfgfp))  
		rc += fwrite(str, 1, strlen(str), isofp);
	if (! rc)  {
		printf("Error: write failed to %s\n", isoname);
		printf("Usage: %s config-file iso-file\n", cmd);
		}
	else  {		// filler 
		printf("Wrote %ld bytes to %s\n", rc, isoname);
		rc += 71;
		*str = 0;
		for (i = 0; i < 70; i++)   
			strcat(str, " ");
		strcat(str, "\n");
		for (i = rc; i < 9980; i += 71)
			fwrite(str, 1, strlen(str), isofp);
		fwrite("# end\n", 1, 6, isofp);
		}
	fclose(isofp);
	fclose(cfgfp);
	printf("press Enter to exit program\n");
	getchar();
}
