/*
 * Little program that reads an existing ISO image and prints its
 * contents to stdout.
 */

#include "libisofs.h"
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

static void
print_permissions(mode_t mode)
{
	char perm[10];
	
	//TODO suid, sticky...
	
	perm[9] = '\0';
	perm[8] = mode & S_IXOTH ? 'x' : '-';
	perm[7] = mode & S_IWOTH ? 'w' : '-';
	perm[6] = mode & S_IROTH ? 'r' : '-';
	perm[5] = mode & S_IXGRP ? 'x' : '-';
	perm[4] = mode & S_IWGRP ? 'w' : '-';
	perm[3] = mode & S_IRGRP ? 'r' : '-';
	perm[2] = mode & S_IXUSR ? 'x' : '-';
	perm[1] = mode & S_IWUSR ? 'w' : '-';
	perm[0] = mode & S_IRUSR ? 'r' : '-';
	printf("[%s]",perm);
}

static void 
print_dir(struct iso_tree_node_dir *dir, int level) 
{
	int i;
	struct iso_tree_iter *iter;
	struct iso_tree_node *node;
	char sp[level * 2 + 1];
	
	for (i = 0; i < level * 2; i += 2) {
		sp[i] = '|';
		sp[i+1] = ' ';
	}
	
	sp[level * 2-1] = '-';
	sp[level * 2] = '\0';
	
	iter = iso_tree_node_children(dir);
	while ( (node = iso_tree_iter_next(iter)) != NULL ) {
	
		
		if (LIBISO_ISDIR(node)) {
			printf("%s+[D] ", sp);
			print_permissions(iso_tree_node_get_permissions(node));
			printf(" %s\n", iso_tree_node_get_name(node) );
			print_dir((struct iso_tree_node_dir*)node, level+1);
		} else if (LIBISO_ISREG(node)) {
			printf("%s-[F] ", sp);
			print_permissions(iso_tree_node_get_permissions(node));
			printf(" %s\n", iso_tree_node_get_name(node) );
		} else if (LIBISO_ISLNK(node)) {
			printf("%s-[L] ", sp);
			print_permissions(iso_tree_node_get_permissions(node));
			printf(" %s -> %s \n", iso_tree_node_get_name(node),
			       iso_tree_node_symlink_get_dest((struct iso_tree_node_symlink*)node) );
		} else {
			/* boot catalog */
			printf("%s-[C] ", sp);
			print_permissions(iso_tree_node_get_permissions(node));
			printf(" %s\n", iso_tree_node_get_name(node) );
		} 
	}
	iso_tree_iter_free(iter);
}

static void
check_el_torito(struct iso_volume *volume)
{
	struct iso_tree_node *cat, *img;
	
	if (iso_volume_get_boot_image(volume, &img, &cat)) {
		
		printf("\nEL-TORITO INFORMATION\n");
		printf("=====================\n\n");
		printf("Catalog: %s\n", iso_tree_node_get_name(cat) );
		printf("Image: %s\n", iso_tree_node_get_name(img) );
	}
}

int main(int argc, char **argv)
{
	struct ecma119_read_opts opts;
	struct data_source *src;
	struct iso_volset *volset;
	struct iso_volume *volume;
	
	if (argc != 2) {
		printf ("You need to specify a valid ISO image\n");
		return 1;
	}
	
	if (!iso_init()) {
		err(1, "Can't init libisofs");
	}
	iso_msgs_set_severities("NEVER", "ALL", "");
	
	src = data_source_from_file(argv[1]);
	if (src == NULL) {
		printf ("Can't open image\n");
		return 1;
	}
	
	opts.block = 0;
	opts.norock = 0;
	opts.nojoliet = 0;
	opts.preferjoliet = 1;
	opts.mode = 0555;
	opts.uid = 0;
	opts.gid = 0;
	
	volset = iso_volset_read(src, &opts);
	
	if (volset == NULL) {
		printf ("Error reading image\n");
		return 1;
	}
	
	volume = iso_volset_get_volume(volset, 0);
	
	printf("\nVOLUME INFORMATION\n");
	printf("==================\n\n");
	
	printf("Vol. id: %s\n", iso_volume_get_volume_id(volume));
	printf("Publisher: %s\n", iso_volume_get_publisher_id(volume));
	printf("Data preparer: %s\n", iso_volume_get_data_preparer_id(volume));
	printf("System: %s\n", iso_volume_get_system_id(volume));
	printf("Application: %s\n", iso_volume_get_application_id(volume));
	printf("Copyright: %s\n", iso_volume_get_copyright_file_id(volume));
	printf("Abstract: %s\n", iso_volume_get_abstract_file_id(volume));
	printf("Biblio: %s\n", iso_volume_get_biblio_file_id(volume));
	
	if (opts.hasRR)
		printf("Rock Ridge Extensions are available.\n");
	if (opts.hasJoliet)
		printf("Joliet Extensions are available.\n");
	
	printf("\nDIRECTORY TREE\n");
	printf("==============\n");
	
	print_dir(iso_volume_get_root(volume), 0);
	
	check_el_torito(volume);
	
	printf("\n\n");
	
	data_source_free(src);
	iso_volset_free(volset);

	iso_finish();

	return 0;
}
