/* -*- indent-tabs-mode: t; tab-width: 8; c-basic-offset: 8; -*- */
/* vim: set ts=8 sts=8 sw=8 noet : */

#define _GNU_SOURCE

#include "libisofs.h"
#include "libburn/libburn.h"
#include <getopt.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <assert.h>
#include <sys/types.h>
#include <dirent.h>
#include <string.h>
#include <err.h>

#define SECSIZE 2048

const char * const optstring = "JRL:h";
extern char *optarg;
extern int optind;

void usage()
{
	printf("test [OPTIONS] LSS NWA DISC DIRECTORY OUTPUT\n");
}

void help()
{
	printf(
"Options:\n"
"  -J       Add Joliet support\n"
"  -R       Add Rock Ridge support\n"
"  -L <num> Set the ISO level (1 or 2)\n"
"  -h       Print this message\n"
);
}

int main(int argc, char **argv)
{
	struct ecma119_source_opts wopts;
	struct ecma119_read_opts ropts;
	struct data_source *rsrc;
	struct iso_volset *volset;
	struct iso_volume *volume;
	struct iso_tree_node_dir *root;
	struct burn_source *wsrc;
	unsigned char buf[2048];
	FILE *fd;
	int c;
	int constraints;
	struct iso_tree_radd_dir_behavior behav = {0,0,0};
	int level=1, flags=0;
	char *boot_img = NULL;

	while ((c = getopt(argc, argv, optstring)) != -1) {
		switch(c) {
		case 'h':
			usage();
			help();
			exit(0);
			break;
		case 'J':
			flags |= ECMA119_JOLIET;
			break;
		case 'R':
			flags |= ECMA119_ROCKRIDGE;
			break;
		case 'L':
			level = atoi(optarg);
			break;
		case '?':
			usage();
			exit(1);
			break;
		}
	}

	if (argc < optind + 2) {
		printf ("Please supply last_sess_start next_sess_start\n");
		usage();
		return 1;
	}
	if (argc < optind + 3) {
		printf ("Please supply device name\n");
		usage();
		return 1;
	}
	if (argc < optind + 4) {
		printf ("Please supply directory to add to disc\n");
		usage();
		return 1;
	}
	if (argc < optind + 5) {
		printf ("Please supply output file\n");
		usage();
		return 1;
	}
	
	if (!iso_init()) {
		err(1, "Can't init libisofs");
	}
	iso_msgs_set_severities("NEVER", "ALL", "");
	
	rsrc = data_source_from_file(argv[optind+2]);
	if (rsrc == NULL) {
		printf ("Can't open device\n");
		return 1;
	}
	
	fd = fopen(argv[optind+4], "w");
	if (!fd) {
		err(1, "error opening output file");
	}
	
	ropts.block = atoi(argv[optind]);
	ropts.norock = 0;
	ropts.nojoliet = 0;
	ropts.preferjoliet = 0;
	ropts.mode = 0555;
	ropts.uid = 0;
	ropts.gid = 0;
	volset = iso_volset_read(rsrc, &ropts);
	
	if (volset == NULL) {
		printf ("Error reading image\n");
		return 1;
	}

	root = iso_volume_get_root(iso_volset_get_volume(volset, 0));
	iso_tree_radd_dir(root, argv[optind+3], &behav);
	
	constraints = ECMA119_OMIT_VERSION_NUMBERS  |
		ECMA119_37_CHAR_FILENAMES | ECMA119_NO_DIR_REALOCATION | 
		ECMA119_RELAXED_FILENAMES;

	memset(&wopts, 0, sizeof(struct ecma119_source_opts));
	wopts.level = level;
	wopts.flags = flags;
	wopts.relaxed_constraints = 0;//constraints;
	wopts.input_charset = "UTF-8";
	wopts.ouput_charset = "UTF-8";
	wopts.ms_block = atoi(argv[optind+1]);
	
	wsrc = iso_source_new_ecma119(volset, &wopts);

	while (wsrc->read(wsrc, buf, 2048) == 2048) {
		fwrite(buf, 1, 2048, fd);
	}
	fclose(fd);

	iso_finish();

	return 0;
}