// Package
package zeroslackr.installer;

//Imports
import java.io.*;
import java.util.zip.*;
import com.ice.tar.*;
import org.w3c.tools.crypt.*;


/**
 * Abstract Tools class
 * <p>
 * Collection of useful methods.
 * <p>
 * Licensed under <A HREF="http://www.gnu.org/licenses/gpl.txt">GNU GPL (Version 3)</A>. 
 * <p>
 * @author	Keripo
 * @version	1.0 Dec 21, 2007
 */

public abstract class Tools {
	
	
	/**
	 * Converts processed path to native equivalent.
	 * @param	path	String of path to be converted.
	 * @return	String of converted path.
	 */
	public static String convertPath (String path) {
		return path.replaceAll("/", Character.toString(File.separatorChar));
	}
	
	/**
	 * Extracts an archive to a specified location. <br>
	 * Supported file extensions: .gz, .tar.gz <br>
	 * ie extract("linux.bin.gz", "/bin"); <br>
	 * or extract ("pz2modules.tar.gz", "/usr/lib/");
	 * @param	file	String of file location. <br>
	 * 			dest	String of destination folder/file.
	 * @return	True if successful, false otherwise.
	 */
	public static boolean extract (String file, String dest) {
		
		// Get extension
		String fileLower = file.toLowerCase();
		
		// Extract
		try {
			if (fileLower.endsWith(".tar.gz")) {
				return extractTarGz(file, dest);
			} else if (fileLower.endsWith(".gz")) {
				return extractGz(file, dest);
			} else {
				System.err.println("Error: Unsupported archive file extension.");
				return false;
			}
		// Extraction problem
		} catch (IOException e) {
			System.err.println("Error: Unable to extract file " + file);
			System.err.println(e.getMessage());
			return false;
		}
		
	} // extract
	
	private static boolean extractGz (String file, String dest) throws IOException {
		
		// Create input stream
		GZIPInputStream in = new GZIPInputStream ( new FileInputStream ( new File (file) ) );
		
		// Create output stream
		FileOutputStream out = new FileOutputStream (dest);
		
		// Extract
        byte[] buffer = new byte[256]; // Arbitrary length
        int len;
        while ( ( len = in.read(buffer) ) != -1 ) {
        	out.write(buffer, 0, len);
        }
        
        // Cleanup
        in.close();
        out.close();
        return true;
		
	} // extractGz
	
	private static boolean extractTarGz (String file, String dest) throws IOException {
		
		// Create directories
		File destFolder = new File(dest);
		destFolder.mkdirs();
		
		// Create input stream
		TarInputStream in = new TarInputStream ( new GZIPInputStream ( new FileInputStream( new File (file) ) ) );
		
		// Extract
		TarEntry e;
		while ( ( e = in.getNextEntry() ) != null ) {
			// Extraction file path
			File destFile = new File ( destFolder.getPath() + e.getName() );
			if (e.isDirectory()) { // Directory
				// Make directory
				destFile.mkdirs();
			} else { // File
				// Extract to location
				FileOutputStream out = new FileOutputStream(destFile);
				in.copyEntryContents(out);
				out.close();
			}
		}
		
		// Cleanup
		in.close();
		return true;
		
	} // extractTarGz
	
	public static String getMd5 (String file) {
		
		return "foo";
	}
	
	
	
}
