package wa.server.plugin.apache;

import java.io.File;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import wa.server.WAConstants;
import wa.server.plugin.WAConfigurator;
import wa.server.util.ConfigFileUtil;
import zutil.db.DBConnection;
import zutil.io.file.FileUtil;

public class ApacheConfigurator implements WAConfigurator {
	private static final String APACHE_CONF_FILE = "wa_apache_vhost.conf";
	private static final String APACHE_MAIN_CONFIG_FILE = "/etc/apache2/apache2.conf";
	private static final String STATIC_PRE_CONF = "wa/server/plugin/apache/apache_default.config";
	
	// Configuration data
	List<ApacheConfigVirtualHost> vhosts;
	
	
	public ApacheConfigurator(){
		vhosts = new LinkedList<ApacheConfigVirtualHost>();
	}


	@Override
	public void read(DBConnection db) throws SQLException {
		vhosts = ApacheConfigVirtualHost.load(db, ApacheConfigVirtualHost.class);
	}

	@Override
	public void save(DBConnection db) throws IOException {
		File file = WAConstants.getConfigFile(APACHE_CONF_FILE);
		// Update Man configuration file
		ConfigFileUtil.writeBetweenBoundary(
				new File(APACHE_MAIN_CONFIG_FILE), 
				"#",
				"Include "+file.getAbsolutePath());
		
		// Write Vhost configuration
		PrintStream out = new PrintStream(file);

		out.println(FileUtil.getContent(new File(STATIC_PRE_CONF)));
		out.println("######################################");
		out.println("#	vhost.php");
		for(ApacheConfigVirtualHost vhost : vhosts){
			if(vhost.isTomcatApp())
				writeTomcatVhost(out, vhost);
			else if(vhost.isSSL())
				writeSSLVhost(out, vhost);
			else
				writeVhost(out, vhost);
		}

		out.close();
	}

	private void writeVhost(PrintStream out, ApacheConfigVirtualHost conf) throws IOException{
		out.println("<VirtualHost *:80>");
		out.println("	ServerName "+conf.getDomain()+":80");
		out.println("	DocumentRoot "+conf.getPath());
		out.println("</VirtualHost>");
		out.println("");
	}

	private void writeSSLVhost(PrintStream out, ApacheConfigVirtualHost conf) throws IOException{
		out.println("<VirtualHost *:80>");
		out.println("	ServerName "+conf.getDomain()+":80");
		out.println("	RewriteEngine On");
		out.println("	RewriteCond  %{SERVER_PORT} !^443$");
		out.println("	RewriteRule  ^(.*)$ https://server$1 [L,R]");
		out.println("</VirtualHost>");
		out.println("<VirtualHost *:443>");
		out.println("	ServerName "+conf.getDomain()+":443");
		out.println("	DocumentRoot  "+conf.getPath());
		out.println("");
		out.println("	SSLEngine on");
		out.println("	SSLCertificateFile     "+WAConstants.getConfigFile(WAConstants.WA_SSL_CERT));
		out.println("	SSLCertificateKeyFile  "+WAConstants.getConfigFile(WAConstants.WA_SSL_KEY));
		out.println("</VirtualHost>");
		out.println("");
	}

	private void writeTomcatVhost(PrintStream out, ApacheConfigVirtualHost conf) throws IOException{
		out.println("<VirtualHost *:80>");
		out.println("	ServerName "+conf.getDomain()+":80");
		out.println("	");
		out.println("	RewriteEngine   On");
		out.println("	RewriteRule     ^/$     /"+conf.getPath()+"    [R]");
		out.println("	ProxyPreserveHost on");
		out.println("	<Proxy *>");
		out.println("		Order deny,allow");
		out.println("		Allow from all");
		out.println("	</Proxy>");
		out.println("	ProxyPass        /  ajp://localhost:8009/");
		out.println("	ProxyPassReverse /  http://localhost:8080/");
		out.println("</VirtualHost>");
		out.println("");
	}
}
