import java.io.*;
import java.nio.channels.FileChannel;
import java.util.Enumeration;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

public class TCInstallFilesServices {
	
	String m_strAgentDir ;
	String m_strSourceDir ;
	String strNotDeleted;
	String m_strServiceName;

	public void Init(String AgentDir, String SourceDir)
	{
		Init(AgentDir, SourceDir, "");
	}

	public void Init(String AgentDir, String SourceDir, String OEMServiceName)
	{
		m_strAgentDir = AgentDir;
		m_strSourceDir = SourceDir;
		m_strServiceName = OEMServiceName;
	}
	
	public void Copy() throws IOException
	{
		File destdir = new File(m_strAgentDir);
		File sourcedir = new File(m_strSourceDir);
		if (sourcedir.isFile())
			ExtractFiles(sourcedir.getAbsolutePath(),destdir.getAbsolutePath());
		else
			CopyFile(sourcedir.getAbsolutePath(),destdir.getAbsolutePath());
	}
	
	public String Remove()
	{
		File destdir = new File(m_strAgentDir);
		File parent = destdir.getParentFile();
		strNotDeleted = null;
		strNotDeleted= new String();
		Delete(destdir);
		destdir.delete();
		parent.delete();
		return strNotDeleted;
	}	
	
	String Delete(File dir) {
		File []DirContents = dir.listFiles();
		if (DirContents!=null)
		{
		   for (int i=0;i<DirContents.length;i++) { 
		     if (DirContents[i].isDirectory()) { 
		    	 Delete(DirContents[i]); 
		     } 
		     if (DirContents[i].delete()==false)
		    	 strNotDeleted += DirContents[i].getName()+"; ";
		   }
		}
		return strNotDeleted; 
	} 	
	
	public boolean AgentDirExists()
	{	
		return new File(m_strAgentDir).exists();
	}
	
	public void CopyFile(String strPath, String dstPath) throws IOException
	{
		File src = new File(strPath);
		File dest = new File(dstPath);
		
		if (src.isDirectory())
		{
			dest.mkdirs();
			String list[] = src.list();		
			
			for (int i = 0; i < list.length; i++)
			{
				String dest1 = dest.getAbsolutePath() + "/" + list[i];
				String src1 = src.getAbsolutePath() + "/" + list[i];
				CopyFile(src1 , dest1);
			}
		}
		else
		{
		     FileChannel sourceChannel = new FileInputStream(src).getChannel();
		     new File (dstPath).createNewFile();
		     FileChannel targetChannel = new FileOutputStream(dest).getChannel();
		      sourceChannel.transferTo(0, sourceChannel.size(), targetChannel);
		      sourceChannel.close();
		      targetChannel.close();
			
		}
	}
	
	void ExtractFiles(String strJarFile, String dstPath) throws IOException
	{
	    JarFile jf = new JarFile(strJarFile);
	    String FiledstPath = new String();
	
	    for(Enumeration e = jf.entries(); e.hasMoreElements(); )
	    {
	  	  ZipEntry ze = (ZipEntry) e.nextElement();	  
	  	  if (ze.isDirectory()==true)
	  	  {
		  	  String name = ze.getName();
		  	  //if ()
		  	  //name = name.replace("/","//");
		  	  FiledstPath = dstPath+"/"+name;	  	  		  
	  		  new File(FiledstPath).mkdirs();
	  	  }
	    }	
	    jf.close();
	    
 	     try {
  	    	 final int BUFFER = 8000;
  	         BufferedOutputStream dest = null;
  	         ZipFile srczipdata = new ZipFile(strJarFile);
  	         ZipEntry entry;
  	         Enumeration e = srczipdata.entries();

  	         while(e.hasMoreElements()) {
  	        	entry = (ZipEntry) e.nextElement();
  	            String name = entry.getName();
  		  	  	//name = name.replace("/","/");
  		  	  	FiledstPath = dstPath+"/"+name;	   	            
  	            int count;
 	            
  		  	  	if (entry.isDirectory()==false)
  		  	  	{
  		            System.out.println("<<Extracting: " +entry+"->"+FiledstPath+">>");

					if (name.equals("TCOracleAgent/conf/wrapper.conf") ||
						name.equals("TCOracleAgent/bin/TCOraAgent"))
					{
						BufferedReader br = new BufferedReader(
							new InputStreamReader(srczipdata.getInputStream(entry)));

						File dstfile = new File(FiledstPath);
						BufferedWriter bw = new BufferedWriter(
							new OutputStreamWriter(new FileOutputStream(dstfile)));

						String line;
						while((line = br.readLine()) != null)
						{
							// if OEM Agent name is not empty then update configs
							if (!m_strServiceName.equals(""))
								line = line.replaceAll("TaskCentre Oracle Agent", m_strServiceName);

							bw.write(line);
							bw.newLine();
						}

						bw.close();
						br.close();
					}
					else
					{

						byte data[] = new byte[BUFFER];
						// write the files to the disk
						File dstfile = new File(FiledstPath);
						dstfile.createNewFile();
						BufferedInputStream zis = new BufferedInputStream(srczipdata.getInputStream(entry));
						FileOutputStream fos = new FileOutputStream(dstfile);

						dest = new BufferedOutputStream(fos, BUFFER);
						while ((count = zis.read(data, 0, BUFFER)) != -1)
						{
							dest.write(data, 0, count);
						}
						zis.close();
						dest.flush();
						dest.close();
					}
  		  	  	}		  	  	
  	         }
  	      } catch(Exception e1) {
  	         e1.printStackTrace();
  	      }    
	}
}
