#!{{ python_executable }}

import cgi
import cgitb
import json
import os
import subprocess
import logTools
import codecs

cgitb.enable(display=0, logdir="/tmp/cgi.log")
form = cgi.FieldStorage()
base_folder_list = {{ base_folder_list }}
script_path = "{{ script_path }}"
logpath = form.getvalue("path", "")
action = form.getvalue("action", "tail")
size = int(form.getvalue("size", "200"))


print """<html><head>
<link rel="stylesheet" href="static/pure-min.css">
<link rel="stylesheet" href="static/style.css">
<script src="static/jquery-1.10.2.min.js"></script>
<style>
.head{
	background-color:#0078e7;
	border:0px solid #ffffff;
	text-align:left;
	border-width:0px 0px 1px 1px;
	font-size:18px;
	font-family:Helvetica;
	font-weight:normal;
	color:#ffffff;
	display: block;
	padding: 10px;
	margin: 0;
}
.box{
  border: 1px solid #e8eaed;
  padding: 5px;
}
textarea {width: 100%; height: 470px; margin-top: 10px;}
ul.file-list {margin:0px; padding: 0px; list-style: none; height:400px; overflow: auto;}
.button {margin-top: 5px;}
.button div {margin: 0; margin-right: 10px; float: left; }
.grep {float: left; margin: 5px 0;}
#pattern {width:300px; padding:5px;}
</style>
<script language="javascript" type="text/javascript">
$(document).ready(function () {
  $(".file").click(function() {
    var child = $(this).children('input[type=checkbox]');
    if (! $(child).is(':checked') ) {
        $(child).prop( "checked", true );
    }
    else {$(child).prop( "checked", false );}
  });
  $( "#open" ).click(function() {
    var file_list = "";
    $(".file").each(function () {
      if ($(this).children('input[type=checkbox]').is(':checked') ) {
        file_list += $(this).attr("rel") + "#";
      }
    });
    if (file_list == "") { return false;}
    $("#path").val(file_list);
    $( "#log_form" ).submit();
    return false;
  });
  
  $( "#grep" ).click(function() {
    var file_list = "";
    $(".file").each(function () {
      if ($(this).children('input[type=checkbox]').is(':checked') ) {
        file_list += $(this).attr("rel") + "#";
      }
    });
    if (file_list == "") { return false;}
    if ( $("#pattern").val() == "") { return false;}
    $("#path").val(file_list);
    $("#action").val("grep");
    $( "#log_form" ).submit();
    return false;
  });
  
  $( "#check" ).click(function() {
    $(".file").each(function () {
      var child = $(this).children('input[type=checkbox]');
      if (! $(child).is(':checked') ) {
        $(child).prop( "checked", true );
      }
    });
    return false;
  });
  $( "#uncheck" ).click(function() {
    $(".file").each(function () {
      var child = $(this).children('input[type=checkbox]');
      if ($(child).is(':checked') ) {
        $(child).prop( "checked", false );
      }
    });
    return false;
  });
  
  $( "#reload" ).click(function() {
    $( "#log_form" ).submit();
    return false;
  });
  
  $( "#return" ).click(function() {
    $("#path").val("");
    $( "#log_form" ).submit();
    return false;
  });
  var textarea = $("#logcontent")[0]
  
  if ($(textarea) !== undefined) {
    $(textarea).scrollTop($(textarea)[0].scrollHeight - $(textarea).height());
  }
});
</script>
</head><body>"""
if not logpath:
  print """
<form action="/index.cgi" method="post" class="pure-form-aligned" id="log_form">
  <input type="hidden" name="posting-script" value="%s" />
  <div class="box pure-menu pure-menu-open">
    <h2 class="head">Select file(s) in the list bellow</h2>
  <ul class="file-list">""" % script_path
  for base_folder in base_folder_list.values():
    if os.path.exists(base_folder):
      for filename in os.listdir(base_folder):
        path = os.path.join(base_folder, filename)
        if not os.path.isdir(path):
          print """  <li>
      <a href="#" class="script file" rel="%s" title="%s">
      <input type="checkbox" />
      %s</a></li>""" % (
              path, path, filename)
        else:
          # accept a folder containing log files
          for sub_filename in os.listdir(path):
            sub_path = os.path.join(path, sub_filename)
            if os.path.isdir(sub_path):
              continue
            print """  <li><a href="#" class="script file" rel="%s" title="%s">
              <input type="checkbox" />
              %s</a></li>""" % (
              sub_path, sub_path, sub_filename)
  print """  </ul>
  </div>
  <div class='grep'>
    <input type="text" name="pattern" id="pattern" value="" placeholder="type a string | regex here...">
  </div>
  <div style='clear:both'></div>
  <div class="button">
    <div><label for="size">Tail Lines: </label><select name="size" id="size">
      <option value="50" selected>50</option>
      <option value="100">100</option>
      <option value="200">200</option>
      <option value="500">500</option>
      <option value="1500">1500</option>
    </select></div>
    <button type="button" class="pure-button pure-button-primary" id="uncheck">Uncheck All</button>
    <button type="button" class="pure-button pure-button-primary" id="check">Check All</button>
    <button type="button" class="pure-button pure-button-primary" id="open">Tail</button>
    <button type="button" class="pure-button pure-button-primary" id="grep">Grep</button>
  </div>
  <div style='clear:both'></div>
  <input type="hidden" name="path" id="path" value="" />
  <input type="hidden" name="action" id="action" value="tail" />
</form>"""
else:
  path_list = [x for x in logpath.split('#') if x]
  log_content = ""
  title = ""
  pattern = ""
  for filepath in path_list:
    if os.path.exists(filepath) and os.path.isfile(filepath):
      title += " " + filepath.split('/')[-1:][0]
      try:
        if action == "tail":
          pattern = form.getvalue("pattern", "")
          content = logTools.tail(codecs.open(filepath, "r", "utf_8"), size, pattern)
        elif action == "grep":
          pattern = form.getvalue("pattern")
          content = logTools.grep(filepath, pattern)
        else:
          content = ""
      except Exception, e:
        content = str(e)
      if content:
        log_content += "%s FILE %s >>>>\n\n" % (action.upper(), filepath)
        log_content += content + "\n\n\n"
  print """
<form action="/index.cgi" method="post" class="pure-form-aligned" id="log_form">
  <input type="hidden" name="posting-script" value="%s" />
  <input type="hidden" name="path" id="path" value="%s" />
  <input type="hidden" name="action" id="action" value="%s" />
  <input type="hidden" name="pattern" id="pattern" value="%s" />
  <input type="hidden" name="size" id="size" value="%s" />
</form>
<div class="box">
  <h2 class="head">Tail: %s </h2>
  <div class="button">
    <button type="submit" class="pure-button pure-button-primary" id="return">Return</button>
    <button type="submit" class="pure-button pure-button-primary" id="reload">Refresh</button>
  </div>
  <div style='clear:both'></div>
  <textarea id="logcontent" readonly>%s</textarea>
</div>
""" % (script_path, logpath, action, pattern, size, title, log_content)
  if pattern:
    print "<p>Pattern string is: %s</p>" % pattern
print """
</body></html>"""