/*
  Little test program for libisoburn.
  It grows an iso filesystem on a disc.

  Copyright 2007 Vreixo Formoso Lopes <metalpain2002@yahoo.es>
             and Thomas Schmitt <scdbackup@gmx.net>
*/


#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <getopt.h>
#include <err.h>
#include <string.h>
#include <errno.h>
#include <sys/stat.h>


#include <libisofs/libisofs.h>
#include <libburn/libburn.h>
#include "../src/libisoburn.h"

const char * const optstring = "JRh";
extern char *optarg;
extern int optind;


/** Activates the usage of function graft_point() rather than
    plain iso_tree_radd_dir() from libisofs
*/
#define With_graft_poinT 1


static int graft_point(struct iso_volume *volume, const char *disk_path,
                const char *img_path, struct iso_tree_radd_dir_behavior *behav)
{
 char path[4096], *apt, *npt;
 struct iso_tree_node_dir *dir;
 struct iso_tree_node *node;
 int done= 0, is_dir= 0;
 struct stat stbuf;

 strncpy(path, img_path, sizeof(path)-1);
 path[sizeof(path)-1]= 0;
 apt= npt= path;

 if(lstat(disk_path, &stbuf) == -1) {
   fprintf(stderr, "Cannot determine attributes of '%s' : %s (%d)\n",
           disk_path, (errno > 0 ? strerror(errno) : "unknown error"), errno);
   return(0);
 }
 if(S_ISDIR(stbuf.st_mode)) 
   is_dir= 1; 
 else if(!(S_ISREG(stbuf.st_mode) || S_ISLNK(stbuf.st_mode))) {
   fprintf(stderr, "File object '%s' is of non-supported file type\n",
           disk_path);
   return(0);
 }

 dir= iso_volume_get_root(volume);
 if(dir==NULL) {
   fprintf(stderr, "While grafting '%s' : no root node available\n", img_path);
   return(0);
 }
 for(npt= apt; !done; apt= npt+1) {
   npt= strchr(apt, '/');
   if(npt==NULL) {
     npt= apt+strlen(apt);
     done= 1;
   } else
     *npt= 0;
   if(*apt==0) {
     *apt= '/';
     apt++;
 continue;
   }
   node= iso_tree_volume_path_to_node(volume,path);
   if(node!=NULL) {
     if(iso_tree_node_get_type(node)!=LIBISO_NODE_DIR) {
       fprintf(stderr, "While grafting '%s' : '%s' is not a directory\n",
               img_path, path);
       return(0);
     }
     dir= (struct iso_tree_node_dir *) node;
   } else {
     dir= iso_tree_add_dir(dir, apt);
     if(dir==NULL) {
       fprintf(stderr, "While grafting '%s' : could not insert '%s'\n",
               img_path, path);
       return(0);
     }
   }
   if(done) {
     if(is_dir) {
       iso_tree_radd_dir(dir, disk_path, behav);
     } else {
       node= iso_tree_add_node(dir, disk_path);
       if(node == NULL) {
         fprintf(stderr, "While grafting '%s'='%s' : libisofs_errno = %d\n",
               img_path, disk_path, libisofs_errno);
       }
     }
   } else
     *npt= '/';
 }
 fprintf(stderr, "NOTE: added %s '%s'='%s'\n", (is_dir ? "directory" : "node"),
                 img_path, disk_path);
 return(1);
}


static
void usage()
{
	printf("test [OPTIONS] DRIVE DIRECTORY\n");
}

static
void help()
{
	printf(
"Options:\n"
"  -J       Add Joliet support\n"
"  -R       Add Rock Ridge support\n"
"  -h       Print this message\n"
);
}

int main(int argc, char **argv)
{
	struct burn_drive_info *drives;
	struct iso_volset *volset;
	struct burn_drive *drive;
	struct burn_disc *disc;
	enum burn_disc_status state;
	struct isoburn_read_opts ropts;
	struct isoburn_source_opts sopts;
	int c;
	struct iso_tree_radd_dir_behavior behav = {0,0,0};
	int flags=0;
	int ret=0, i;
	int size, free_bytes;
	char *status_text;

	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 '?':
			usage(); 
			exit(1);
			break;
		}
	}

	if (argc < optind + 1) {
		fprintf(stderr, "Please supply device name\n");
		usage();
		exit(1);
	}
	if (argc < optind + 2) {
		fprintf(stderr, "Please supply directory to add to disc\n");
		usage();
		exit(1);
	}
	
	
	if (!isoburn_initialize()) {
		fprintf(stderr, "Can't init libisoburn\n");
		exit(1);
	}
	
	/* TODO change this. maybe we can add wrapp in libisoburn */
	iso_msgs_set_severities("NEVER", "DEBUG", "libisofs : ");
	burn_msgs_set_severities("NEVER", "DEBUG", "libburn : ");
	burn_set_signal_handling("libisoburn/test/test : ", NULL, 0);
	
	printf("Growing drive %s\n", argv[optind]);
	
	if (isoburn_drive_scan_and_grab(&drives, argv[optind], 1) <= 0) {
		fprintf(stderr,
		"Can't open device. Are you sure it is a valid drive?\n");
		exit(1);
	}
	drive = drives[0].drive;
	
	/* check for invalid state */
	state = isoburn_disc_get_status(drive);
	if (state != BURN_DISC_BLANK && state != BURN_DISC_APPENDABLE) {
		fprintf(stderr, "Unsuitable disc status\n");
		goto exit_cleanup;
	}
	
	/* fill read opts */
        memset(&ropts, sizeof(ropts), 0);
	ropts.norock = 0;
	ropts.nojoliet = 0;
	ropts.preferjoliet = 0;
	ropts.uid = 0;
	ropts.gid = 0;
	ropts.mode = 0555;
        ropts.pretend_blank= 0;
	
	if (isoburn_read_volset(drive, &ropts, &volset) <= 0) {
		fprintf(stderr, "Can't read volset\n");
		goto exit_cleanup;
	}


#ifdef With_graft_poinT
	for (i = optind + 1; i < argc; i++) {
		if (graft_point(iso_volset_get_volume(volset, 0),
				argv[i], argv[i], &behav) <= 0) {
			fprintf(stderr, "Canot graft '%s'\n", argv[optind+1]);
			goto exit_cleanup;
		}
	}
	
#else
	struct iso_tree_node_dir *root;
	root = iso_volume_get_root(iso_volset_get_volume(volset, 0));
	/* add a new dir */
	iso_tree_radd_dir(root, argv[optind+1], &behav);
#endif /* ! With_graft_poinT */

	
	sopts.level = 2;
	sopts.flags = flags;
	sopts.relaxed_constraints = 0;
	sopts.copy_eltorito = 1;
	sopts.no_cache_inodes = 0;
	sopts.sort_files = 1;
	sopts.default_mode = 0;
	sopts.replace_dir_mode = 0;
	sopts.replace_file_mode = 0;
	sopts.replace_uid = 0;
	sopts.replace_gid = 0;
	sopts.dir_mode = 0555;
	sopts.file_mode = 0444;
	sopts.gid = 0;
	sopts.uid = 0;
	sopts.input_charset = NULL;
	sopts.ouput_charset = NULL;

	if (isoburn_prepare_disc(drive, &disc, &sopts) <= 0) {
		fprintf(stderr, "Can't prepare disc\n");
		goto volset_cleanup;
	}
	
	/* a. write the new image */
	printf("Adding new data...\n");
	{
		struct burn_write_opts *burn_options;
		struct burn_progress progress;
		
		burn_options = burn_write_opts_new(drive);
		burn_drive_set_speed(drive, 0, 0);
		burn_write_opts_set_underrun_proof(burn_options, 1);
	
		/* ok, write the new track */
		isoburn_disc_write(burn_options, disc);
		burn_write_opts_free(burn_options);
		
		while (burn_drive_get_status(drive, NULL) == BURN_DRIVE_SPAWNING)
			usleep(100002);
		
		while (burn_drive_get_status(drive, &progress)
							 != BURN_DRIVE_IDLE) {

			printf("Writing: sector %d of %d",
				 progress.sector, progress.sectors);
			ret = isoburn_get_fifo_status(drive, &size,
					 	&free_bytes, &status_text);
			if (ret > 0 )
				printf("  [fifo %s, %2d%% fill]", status_text,
					(int) (100.0 - 100.0 *
						((double) free_bytes) /
						(double) size));
			printf("\n");
			sleep(1);
		}
	}
	
	/* b. write the new vol desc */
	printf("Writing the new vol desc...\n");
	if (isoburn_activate_session(drive) <= 0) {
 		fprintf(stderr, "Ups, new vol desc write failed\n");
 	}

	ret= 0;
volset_cleanup:;
/*
	iso_volset_free(volset);
*/       
	
exit_cleanup:;
	isoburn_drive_release(drive, 0);
	isoburn_finish();
	
	exit(ret);
}

