/*
Dexter Travis
Bradley University
2001/2002 Senior Project Botdock
http://pioneer1.bradley.edu
http://cegt201.bradley.edu/projects/proj2002/botdock/
*/

import java.io.*;
import java.net.*;
import java.lang.*;
import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*; //possibly redundant

//will creat log file and keep track of server and user events.
	class log
	{	//declared public static for use in multiple methods
		public static File logfile;
		public static FileWriter logfilewriter;
		public static void logstart(String filename)
		{
			try
			{
				
				logfile = new File(filename);
				
				logfilewriter = new FileWriter(filename, true);//second parameter tells FileWriter to append not overwrite
				log("server and Log started on");
			}
			catch(Exception e)
			{
				System.out.println("E error writing to or opening logfile " +e);
			}
				
		}
		
		public static void log(String logmsg)
		{
		
		
		/*
		legend of error log first letter of string
		E = various errors
		S = server messages
		M = movement messages 
		W = wait messages
		U = user messages
		F = find messages
		C = charge messages
		*/
			try
			{
				Date msgtime = new Date();
				logfilewriter.write(msgtime+" "+logmsg +"\n");
				logfilewriter.flush();
			}
			catch(Exception e)
			{
			}
			
		}
		
		public static void email(String emailmsg)
		{
			//to be used to email system admin problems
			
			String to = new String("dtravis@bradley.edu");
			String from = new String("server@pioneer1.bradley.edu");
			String host = new String("cegt201.bradley.edu");
			String filename = new String("logfile.txt");
			
			String msgText1 = emailmsg;
			String subject = new String("Pioneer1 logfile");
	
			// create some properties and get the default Session
			Properties props = System.getProperties();
			props.put("mail.smtp.host", host);
	
			Session session = Session.getDefaultInstance(props, null);
			
	
			try 
			{
	    			// create a message
	    			MimeMessage msg = new MimeMessage(session);
	    			msg.setFrom(new InternetAddress(from));
	    			InternetAddress[] address = {new InternetAddress(to)};
	    			msg.setRecipients(Message.RecipientType.TO, address);
	    			msg.setSubject(subject);

	    			// create and fill the first message part
	    			MimeBodyPart mbp1 = new MimeBodyPart();
	    			mbp1.setText(msgText1);

	    			// create the second message part
	    			MimeBodyPart mbp2 = new MimeBodyPart();

            			// attach the file to the message
   	    			FileDataSource fds = new FileDataSource(filename);
	    			mbp2.setDataHandler(new DataHandler(fds));
	    			mbp2.setFileName(fds.getName());

	    			// create the Multipart and its parts to it
	    			Multipart mp = new MimeMultipart();
	    			mp.addBodyPart(mbp1);
	    			mp.addBodyPart(mbp2);

	    			// add the Multipart to the message
	    			msg.setContent(mp);

	    			// set the Date: header
	    			msg.setSentDate(new Date());
	    
	    			// send the message
	    			Transport.send(msg);
	    
			}
		 	catch (MessagingException mex) 
		 	{
	    			mex.printStackTrace();
	    			Exception ex = null;
	    			if ((ex = mex.getNextException()) != null)
	    			{
					ex.printStackTrace();
	    			}
			}
		//end of method email	
		}
		
		public static boolean reboot(boolean reboot)
		{
			//to determine in a file if the robot was rebooted and encounted same error again.
			//for much later implementation.	
			return(false);
		}
		public static void logclose()
		{
			try
			{
				
				log("Server and log closed");
				email("ADMIN alert server is down");
				logfilewriter.flush();
				logfilewriter.close();
			}
			catch(Exception e)
			{
			}
			
		}
		
	
	//end of class log
	}