package form.run;

import java.io.*;

public class ReceiverMailer {

	/**
	* the name of the local host, used when connecting to
	* the mail server
	*/
	static final String localHost = "spelunker.sv.vt.edu";

	/**
	* the port of the smtp Should always be 25
	*/
	static final int mailPort = 25;

	static String readme = "README";
	/**
	* The variable in the mail tempelate file
	* which is replaced with the dirctory containg 
	* the outputted data
	*/
	static final String replaceMe = "$1";

	public ReceiverMailer(String sourceDir, String dir, String to,String from,String host){
		FileReader f;
		try {
		  f = new FileReader(sourceDir+"/"+readme);
		  BufferedReader r = new BufferedReader(f);
		  String dataIn = "";
		  String dataOut = "";
		  while ((dataIn = r.readLine())!=null){
	 	   dataOut+=dataIn+"\n";
		}	
		System.out.println("Directory: "+dir);
		dataOut = parse(dataOut,dir);
		System.out.println("Parsed: "+dataOut);
		mail(to,from,host,dataOut);
		System.out.println("Should have been sent");
		} catch (FileNotFoundException e){
            		e.printStackTrace();
        	}
		catch (IOException e){
            		e.printStackTrace();
        	}
	}

	private String parse(String data, String dir){
		int curr =0;
		int last =0;
		String newData = "";

		curr = data.indexOf(replaceMe);
		while (curr != -1){
		   newData += data.substring(last, curr);
		   newData+=dir;
		   last = curr+replaceMe.length();
		   curr = data.indexOf(replaceMe,last);
		}
		return newData+data.substring(last);
	}

    public void mail(String to,String from, String host, String msg){
      SMTPClient c = new SMTPClient(host,mailPort);
        if(c.helo())
          System.out.println("OK - HELO");
        if(c.sendFrom(from))
          System.out.println("OK - sendFrom");
        if(c.sendTo(to))
          System.out.println("OK - sendTo");
        if(c.startData())
          System.out.println("OK - startData");
        if(c.data(msg))
              System.out.println("OK - data 1");
        if(c.endData())
          System.out.println("OK - endData");
        if(c.quit())
          System.out.println("OK - quit");
    }

}	
