<%@ page import="javax.servlet.jsp.PageContext,javax.servlet.http.*, java.io.File" %><%!
 boolean debug = false;
 
 boolean tryRedir( String base, HttpServletRequest request,PageContext pc, String lang) throws java.io.IOException {
    String uri = request.getServletPath();
    int dex = uri.indexOf("/en/");
    String nuri = uri.substring(0,dex+1)+lang+uri.substring(dex+3);
    nuri = nuri.substring(0,nuri.length()-3)+"htm";
    File file = new File( pc.getServletContext().getRealPath( nuri ) );
    if ( file.exists() ) {
        nuri = request.getContextPath() + nuri;
        String redir = base+nuri+"?"+request.getQueryString();
        if ( debug ) {
            pc.getOut().println("<p>Found : "+file );
        	pc.getOut().println("<p>Would redirect to <a href="+redir+">"+redir+"</a>");
        } else
	        ((HttpServletResponse)pc.getResponse()).sendRedirect(  redir );       
        return true;
    }
     if (debug)
     	  pc.getOut().println("<p>Did not find : "+file );
    
    return false;
}
%><%

     // please don't cache this page.
        response.setHeader("Pragma", "No-cache");
        response.setDateHeader("Expires", 0);
        response.setHeader("Cache-Control", "no-cache");

	if ( debug ){
    	out.println("locale: "+request.getLocale()+"<p>");
     }    
     
	String base="";
//	String ua = request.getHeader("user-agent"); 
//	if ( ua != null && ua.indexOf( "MSIE 6.0" ) != -1 ){
//	    String root = getServletContext().getRealPath( "/" ) ;
//		int port = com.sun.netstorage.array.mgmt.cfg.bui.utilities.HttpHelpServer.getPort(root);
//		base = "http://"+request.getServerName()+":"+port ;
//	}

    // lockhart doesnt like our filenames which are really URLs,
    // so all requests for help hit this page and sent to the
    // corect page for the lanauage

    // Try full locale
    if ( tryRedir( base, request, pageContext, request.getLocale().toString() ) )
      return;

    // try just language
    if ( tryRedir( base, request, pageContext, request.getLocale().getLanguage() ) )
      return;

    // fall back to english (can't ever fail.)
    if ( ! tryRedir( base, request, pageContext, "en" ) )
    	out.println("Sorry, no help was found.  ");
%>

