<%!
//functions
String strip(String in) {
        return in.substring(1, in.length()-1);
}
%>
<c:if test="${!empty param.printers}"><c:set var="printersCSV" value="${param.printers}" scope="session"/></c:if>
<c:if test="${!empty param.alias}"><c:set var="aliasCSV" value="${param.alias}" scope="session"/></c:if>
<c:if test="${!empty param.tftpParam}"><c:set var="tftpParam" value="${param.tftpParam}" scope="session"/></c:if>
<c:if test="${!empty param.printerDir}"><c:set var="printerDir" value="${param.printerDir}" scope="session"/></c:if>
<FORM ACTION="<c:out value='${alias}'/>/selector.jsp?view=listing" METHOD=POST>
    <select name="type">
        <option value="javascript" <c:if test="${'javascript' == param.type}">SELECTED</c:if>>JavaScript</option>
        <option value="tftpPrinterCfg" <c:if test="${'tftpPrinterCfg' == param.type}">SELECTED</c:if>>TFTP printer configs</option>
        <option value="tftpPrinterLn" <c:if test="${'tftpPrinterLn' == param.type}">SELECTED</c:if>>TFTP printer links</option>
        <option value="printcap" <c:if test="${'printcap' == param.type}">SELECTED</c:if>>printcap</option>
    </select><br>
        <table>
                <tr>
                        <td>from HP jet admin</td>
                        <td>group renaming</td>
                        <td></td>
                </tr>
                <tr>
                        <td><textarea rows=10 cols=15 name="printers"><c:out value="${printersCSV}"/></TEXTAREA></td>
                        <td><textarea rows=10 cols=30 name="alias"><c:out value="${aliasCSV}"/></TEXTAREA></td>
                        <td><table>
<tr><td>additional tftp params</td></tr>
<tr><td><textarea rows=10 cols=40 name="tftpParam"><c:out value='${tftpParam}'/></textarea></td></tr>
<tr><td>printer directory</td></tr>
<tr><td><input type=text name="printerDir" value="<c:out value='${printerDir}'/>"></td></tr>
</table></td>
                </tr>
        </table>
    <input type="submit" value="generate"><br>
</form>
<hr>
<%
TreeSet uris = new TreeSet();
pageContext.setAttribute("uris", uris);
TreeMap aliases = new TreeMap();
pageContext.setAttribute("aliases", aliases);
TreeMap printers = new TreeMap();
pageContext.setAttribute("printers", printers);
BufferedReader reader;
TreeMap locations = new TreeMap();
pageContext.setAttribute("locations", locations);
try {
        reader = new BufferedReader(new StringReader(request.getParameter("alias")));
        for(String line=null;(line=reader.readLine())!=null;) {
                StringTokenizer t = new StringTokenizer(line,",");
                aliases.put(t.nextToken(), line);
        }
}
catch(Exception e) {}
try {
        reader = new BufferedReader(new StringReader(request.getParameter("printers")));
        for(String line=null;(line=reader.readLine())!=null;) {
                StringTokenizer t = new StringTokenizer(line,",");
                printers.put(t.nextToken(), line);
        }
}
catch(Exception e) {}
StringTokenizer tokenizer = new StringTokenizer(serviceBean.getList(), ",");
for(;tokenizer.hasMoreTokens();) {
    URI uri = new java.net.URI(tokenizer.nextToken());
    uris.add(uri);
    StringTokenizer t2 = new StringTokenizer(uri.getHost(),"-.0123456789");
    String loc = t2.nextToken();
    if(loc!=null && loc.length()>0) {
        loc = loc.substring(0,1).toUpperCase()+loc.substring(1);
    }
    String alias = (String )aliases.get(loc);
    if(alias!=null) {
        StringTokenizer t3 = new StringTokenizer(alias, ",");
        t3.nextToken();
        loc = t3.nextToken();
    }
    Map map = (Map )locations.get(loc);
    if(map==null) map = new TreeMap();
    String queue = uri.getHost().substring(0, uri.getHost().indexOf("-ninja"));
    map.put(uri, new Object[] {uri, queue});
    locations.put(loc, map);
}
%>
<h2>Dist=<%=serviceBean.getOrganization()%></h2>
<c:choose>
    <c:when test="${param.type == 'javascript'}">
<ol>
    <li>make sure your javascript source includes the line <code>var printers;</code></li>
    <li>copy over to printers.js</li>
</ol>

<textarea rows=10 cols=60>
//<%=serviceBean.getOrganization()%>
if(printers==null) printers=new Array();
<c:forEach var="location" items="${locations}">
    <c:forEach var="vector" items="${location.value}">
        <c:set var="uri" value="${vector.value[0]}"/>
        <%
        String printerName = ((URI )pageContext.getAttribute("uri")).getHost().replaceFirst("-ninja","-printer");
        String printerModel = (String )printers.get(printerName);
        String duplex = "No";
        if(printerModel==null) printerModel="";
        else {
                StringTokenizer t = new StringTokenizer(printerModel, ",");
                if(t.hasMoreTokens()) t.nextToken(); //host
                if(t.hasMoreTokens()) printerModel = t.nextToken();
                if(t.hasMoreTokens()) t.nextToken(); //hw address
                if(t.hasMoreTokens()) t.nextToken(); //ip address
                if(t.hasMoreTokens()) duplex = t.nextToken(); //duplex
                if(printerModel.length()>0) printerModel+=" Series PS";
                printerModel = printerModel.replaceFirst("5Si Series", "5Si Mopier");
        }
        %>
printers[printers.length] = ["<c:out value="${vector.value[1]}"/>","<c:out value="${uri.host}"/>","<%=printerModel%>","<c:out value="${location.key}"/>","<%=duplex%>"];
    </c:forEach>
</c:forEach>
</textarea>
    </c:when>
    <c:when test="${param.type == 'tftpPrinterCfg'}">
<%
    int paramCount = 2;
    String tftpParam = "";
    reader = new BufferedReader(new StringReader(request.getParameter("tftpParam")));
    for(String line; (line=reader.readLine())!=null;) {
        tftpParam += line+"\n";
        paramCount++;
    }
    reader.close();
%>
                <ol>
                        <li><code>$ split -l <%=paramCount%> [INPUT] printerCFG</code></li>
                        <li>
                                <code>$ for i in printerCFG*;do mv $i `head -1 $i | awk -F\&nbsp;&nbsp;'{print $2}'`.cfg;done</code><br>
                        </li>
                        <li>
                                make sure the permissions are correct for downloading tftp files
                        </li>
                <ol><br>
<textarea rows=10 cols=60>
<c:forEach var="uri" items="${uris}"><%
        String ninjaHost = ((URI )pageContext.getAttribute("uri")).getHost();
        String printerHost = ninjaHost.replaceFirst("-ninja","-printer");
        String ninjaIP = "";
        try {
            ninjaIP = InetAddress.getByName(ninjaHost).getHostAddress();
        }
        catch(UnknownHostException e) {
            ninjaIP = "Unknown Host";
        }
        printerHost = new StringTokenizer(printerHost, ".").nextToken();
%>name: <%=printerHost%>
allow: <%=ninjaIP%>
<%=tftpParam%></c:forEach>
</textarea>
    </c:when>
    <c:when test="${param.type == 'tftpPrinterLn'}">
                <ol>
                        <li><code>tftp$ cd /var/tftp</code></li>
                <ol><br>
<textarea rows=10 cols=60>
<c:forEach var="uri" items="${uris}"><%
        String ninjaHost = ((URI )pageContext.getAttribute("uri")).getHost();
        String printerHost = ninjaHost.replaceFirst("-ninja","-printer");
        String printerIP = "";
        try {
            printerIP = InetAddress.getByName(printerHost).getHostAddress();
        }
        catch(UnknownHostException e) {
            printerIP = "Unknown Host";
        }
%>rm <%=printerIP%>
ln -s <c:out value="${printerDir}"/> <%=printerIP%>
</c:forEach></textarea>
    </c:when>
    <c:when test="${param.type == 'printcap'}">
                copy to printcap<br>
<textarea rows=10 cols=60>
<c:forEach var="uri" items="${uris}"><%
        String ninjaName = ((URI )pageContext.getAttribute("uri")).getHost();
        ninjaName = new StringTokenizer(ninjaName, "-.").nextToken();
        String queue = ((URI )pageContext.getAttribute("uri")).getPath();
        queue = queue.replaceAll("/", "");
%><%=ninjaName%>:lp=:rm=<c:out value="${uri.host}"/>:rp=<%=queue%>:sd=/usr/spool/lpd/<%=ninjaName%>:mx#0:
</c:forEach>
</textarea>
    </c:when>
    <c:otherwise>
        <ol>
                        <li>Select The type of output that you want.
                                <ol>
                                        <li>JavaScript - html pages that list printer information</li>
                                        <li>TFTP printer configs - printer access control files</li>
                                        <li>TFTP printer links - commands to enable printer access control</li>
                                        <li>printcap - unix /etc/printcap data</li>
                                </ol>
                        </li>
                        <li>Copy over the export data generated by HP Jet Admin.  This can be done by through HP Web Jet Admin &gt;
Device Management &gt; Device Cache &gt; Device Cache Export.  The view that you are exporting should have the following columns in the following order:
IP Hostname, Device Model, Hardware Address, IP Address.</li>
                        <li>Edit any names that need to be fixed in the rename box</li>
                        <li>enter the get community name</li>
                        <li>enter the set community name</li>
                        <li>enter the tftp printer directory</li>
                </ol>
    </c:otherwise>
</c:choose>
