/* archive.vala - Example for using the libarchive bindings in Vala.
 *
 * Copyright (C) 2009 Julian Andres Klode <jak debian org>
 *
 * Copying and distribution of this file, with or without modification,
 * are permitted in any medium without royalty provided the copyright
 * notice and this notice are preserved.  This file is offered as-is,
 * without any warranty.
 */
using Archive;
using Posix;


void main(string[] args) {

    // Create a new archive object for reading
	Read archive = new Read();
    // A buffer which will hold read data
	char buf[4096];
    // The entry which will be read from the archive.
	weak Entry e;

    // First argument is the archive file.
    if (args.length != 2)
        error("Usage: %s <archive>", args[0]);

    // Enable all supported compression formats
	archive.support_compression_all();
    // Enable all supported archive formats.
	archive.support_format_all();

    // Open the file, if it fails exit
	if (archive.open_filename(args[1], 4096) != Result.OK) 
		error("%s", archive.error_string());

	
	while(archive.next_header(out e) == Result.OK) {		
		if (S_ISDIR(e.mode()))
			print("DIR:  %s\n", e.pathname());
		else {
			print("FILE: %s\n", e.pathname());
        }

        // Read the times of the file
        print("MTIME: %li\n", e.mtime());
		print("CTIME: %li\n", e.ctime());
		print("ATIME: %li\n", e.atime());

        // Read out files ending in .txt and README files, into the buffer buf.
        if (e.pathname().has_suffix(".txt") || e.pathname().has_suffix("README"))
            while(archive.read_data(buf, 4096) != 0)
                // Print the contents of buf, indented with a tab.
                print("\t%s", ((string)buf).replace("\n", "\n\t"));
        print("\n");
	}
    
}