import java.io.* ;
import java.util.* ;

/** Classe de cr&eacute;ation d'un processus <I>wget</I>.<BR>
 * La m&eacute;thode <i>getProcess()</i> permet de r&eacute;cup&eacute;rer un objet de type Process.<BR>
 * On peut ensuite r&eacute;cup&eacute;rer les Input/Output Streams associ&eacute;s aux flots
 * stdin/stdout/stderr du processus pour pouvoir afficher ce que le processus envoie sur sa sortie standard
 * dans une fen&ecirc;tre Java.
 */
public class WgetProcess {
    private Process wget ;

    /** Cr&eqcute;ation du processus "wget", lanc&eacute; avec la ligne de commande qui convient.
     * @param cmdline Les param&egrave;tres &agrave; passer &agrave; l'&eacute;xecutable.
     * @exception IOException si l'&eacute; new peut &ecirc;tre trouv&eacute;.
     */
    public WgetProcess(String cmdline) throws IOException {
	String executable = loadWgetPath() ;
	wget = Runtime.getRuntime().exec(executable + " " + cmdline) ;
	System.out.println("Started : " + executable + " " + cmdline);
    }

    /** Permet de r&eacute;cup&eacute;rer l'objet processus associ&eacute; &agrave; la commande <i>rpm</i>.
     * @return L'objet Process associ&eacute; au sous-processus <i>rpm</i>
     */
    public Process getProcess() {
	return wget ;
    }
    
    private String loadWgetPath() {
	try {
	    String home = System.getProperty("user.home") ;
	    String os = System.getProperty("os.name") ;
	    
	    if(home != null) {
		FileReader file ;
		if(os.equals("Windows 95"))
		    file = new FileReader( "" + home + System.getProperty("file.separator") + "kawaget.ini") ;
		else
		    file = new FileReader( "" + home + System.getProperty("file.separator") + ".kawaget") ;
		
		BufferedReader br = new BufferedReader(file) ;
		String wgetpath = br.readLine() ;
		
		return wgetpath ;
	    }
	}
	catch (FileNotFoundException excep) {
	    System.out.println("*** ERROR ***");
	    System.out.println("Sorry, i can't find the resource file in your home directory.") ;
	}
	catch (IOException excep2) {
	    System.out.println("*** ERROR ***");
	    System.out.println("Parse error in the '.kawaget' file.");
	}
	// QUESTION : pourquoi si on met un bloc finally ici (avec tous les System.out... dedans,
	// le finally est traite AVANT le traitement de l'exception ?!?!? (ou est la logique du bidule ???)
	System.out.println("Please make sure the file '.kawaget' exists in your home directory");
	System.out.println("or that c:\\windows\\kawaget.ini exists, and contains the absolute");
	System.out.println("path to the 'wget' executable.");
	System.out.println("It should contain a line looking like : '/usr/local/bin/wget'");
	System.out.println("or c:\\utils\\wget-1.54\\wget.exe");
	
	
	return "" ;
    }
}
