/*
 * %W% %E% Sun Microsystems, Inc.
 *
 * Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
 *
 * Permission to use, copy, modify, and distribute this software
 * and its documentation for NON-COMMERCIAL purposes and without
 * fee is hereby granted provided that this copyright notice
 * appears in all copies. Please refer to the file "copyright.html"
 * for further important copyright and licensing information.
 *
 * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 */

import java.io.*;
import java.lang.*;

import net.jini.lookup.*;
import net.jini.discovery.*;
import net.jini.lease.*;
import net.jini.event.*;

import java.rmi.*;
import java.rmi.server.UnicastRemoteObject;

import com.sun.jini.system.FileSystem;
import com.sun.jini.thread.*;


public class PrintService extends UnicastRemoteObject 
	implements PrintRemote, Serializable {

    public PrintService() throws RemoteException {
	super();
    }
    	
    public String print(String printerName, String fileName) {
	String retStr = null;
	String inStr = null;
	BufferedReader buffRead = null;

	System.out.println("printerName: "+printerName+" fileName: "+fileName);
	String runCmd = "lp -d "+printerName+" "+fileName;
	try {

	    // Set security manager if there isn't one
	    if (System.getSecurityManager() == null) {
		System.setSecurityManager(new RMISecurityManager());
	    }

	    // Call runtime exec to run printer command
	    Runtime rt = Runtime.getRuntime();
	    Process process = rt.exec(runCmd);

	    // Get return information from process
	    buffRead = new BufferedReader
		(new InputStreamReader(process.getInputStream()));
            while((inStr = buffRead.readLine()) != null) {
		if (retStr == null) {
		    retStr = inStr+"\n";
		} else {
		    retStr += inStr+"\n";
		}
	    }

	    // Get error stream messages
	    buffRead = new BufferedReader
		(new InputStreamReader(process.getErrorStream()));
            while((inStr = buffRead.readLine()) != null) {
		if (retStr == null) {
		    retStr = inStr+"\n";
		} else {
		    retStr += inStr+"\n";
		}
	    }

//	    StreamPlugThread.plugTogether(process.getErrorStream(),
//					  System.err);
		
	} 
	catch (IOException e) {
	    System.err.println("lp exec error:" + e);
	}

	return(retStr);
    }

    public String checkQueue(String printerName) {
	String retStr = null;

	System.out.println("printerName: "+printerName);
	String runCmd = "lpq -P"+printerName;
	try {

	    // Set security manager if there isn't one
	    if (System.getSecurityManager() == null) {
		System.setSecurityManager(new RMISecurityManager());
	    }

	    // Call runtime exec to run printer command
	    Runtime rt = Runtime.getRuntime();
	    Process process = rt.exec(runCmd);

	    // Get return information from process
	    String inStr;
	    BufferedReader buffRead = 
		new BufferedReader(
		    new InputStreamReader(process.getInputStream()));
            while((inStr = buffRead.readLine()) != null) {
		if (retStr == null) {
		    retStr = inStr+"\n";
		} else {
		    retStr += inStr+"\n";
		}
	    }

	    // Plug together error streams
	    StreamPlugThread.plugTogether(process.getErrorStream(),
					  System.err);
	} 
	catch (IOException e) {
	    System.err.println("lpq exec error:" + e);
	}

	return(retStr);
    }

}


