#!/bin/bash
#
# 2007/10/23 Gabriel Moreau
# 2010/11/03 Gabriel Moreau
# 2011/03/30 Gabriel Moreau - make it generic
# 2017/11/04 Gabriel Moreau - all reworks - use tab

export PATH=/bin:/sbin:/usr/bin:/usr/sbin
export LANG=C

SITE_NAME=''
REMOTE_SERVER=localhost
REMOTE_USER=''
REMOTE_FOLDER=/var/www/klask/

GRAPH_MODULO=9
GRAPH_SHIFT=1

if [ -f /etc/klask/push-web.conf ]
then
   . /etc/klask/push-web.conf
fi

TMP='/tmp/klask-push-web'


# remote command
DOT=/usr/bin/dot


mkdir -p ${TMP}
if [ ! -d ${TMP} ]
then
   echo "Error: Folder ${TMP} do not exist !"
   exit 1
fi
cd ${TMP}


LOCALTIME=$(date "+%Y-%m-%d %H:%M")


klask exportsw --format dot --modulo ${GRAPH_MODULO} --shift ${GRAPH_SHIFT} > ${TMP}/switch-map.txt


klask exportdb    > ${TMP}/hostname-db.txt
klask exportsw    > ${TMP}/switch-db.txt
klask ip-free     > ${TMP}/ipfree-db.txt
klask bad-vlan-id > ${TMP}/badvlan-db.txt


cat <<END > ${TMP}/index.html
<!DOCTYPE html>
<html lang="en">
<head>
 <link rel="stylesheet" type="text/css" href="style-klask.css" />
 <!-- <meta http-equiv="refresh" content="1800"> -->
 <title>Klask mapping net-tools</title>
 <noscript>
 </noscript>
 <script src="sorttable-klask.js"></script>
 <script>
function delayedRefresh() {
 setTimeout( "refresh()", 1800*1000 );
 }

function refresh() {
 history.go(0);
 }
 
function selectTab(event, tabName) {
 var i;
 var x = document.getElementsByClassName("selectableTabContent");
 var tablinks = document.getElementsByClassName("selectableTabButton");
 for (i = 0; i < x.length; i++) {
  x[i].style.display = "none";
  }
 for (i = 0; i < x.length; i++) {
  tablinks[i].id = "";
  }
 document.getElementById(tabName).style.display = "block";
 event.currentTarget.id = "selected";
 }
 </script>
</head>
<body> <!-- onload="delayedRefresh" -->
<h1>
<b><a href="http://servforge.legi.grenoble-inp.fr/projects/klask" class="circle">Klask</a></b>
Tool for <a href="switch-map.png">mapping</a> the local network - site ${SITE_NAME}
<a class="refresh" href="./">${LOCALTIME}</a>
</h1>

<div id="navbar">
 <ul>
  <li class="selectableTabButton" onclick="selectTab(event, 'IP_detected')" id="selected">IP detected</li>
  <li class="selectableTabButton" onclick="selectTab(event, 'IP_available')">IP available</li>
  <li class="selectableTabButton" onclick="selectTab(event, 'VLAN_mismatch')">VLAN mismatch</li>
  <li class="selectableTabButton" onclick="selectTab(event, 'MAP')">MAP (png)</li>
  <li class="selectableTabButton" onclick="selectTab(event, 'RAW_data')">RAW data</li>
 </ul>
</div>

<div id="content">

 <div id="IP_detected" class="selectableTabContent">
  <p>
  List of all IP addresses detected on the local network (on all VLANs)
  since the origin of the database
  (except those that have been deleted manually and voluntarily).
  </p>
END

klask exportdb --format html >> ${TMP}/index.html

cat <<END >> ${TMP}/index.html
 </div>

 <div id="IP_available" class="selectableTabContent" style="display:none">
  <p>
  List all free IP addresses (which have never been used or not used for two years).
  These IP addresses are available to assign to all new computers.
  </p>
END

klask ip-free --format html >> ${TMP}/index.html

cat <<END >> ${TMP}/index.html
 </div>

 <div id="VLAN_mismatch" class="selectableTabContent" style="display:none">
  <p>
  List all computers connected to switches where the VLAN is misconfigured.
  Returns the port and VLAN to configure on these switches.
  </p>
END

klask bad-vlan-id --format html >> ${TMP}/index.html

cat <<END >> ${TMP}/index.html
 </div>

 <div id="MAP" class="selectableTabContent" style="display:none">
   <p>
   Direct link to local network map in the formats <a href="switch-map.png">png</a> and <a href="switch-map.svg">svg</a>.
   </p>
   <img class="map" src="switch-map.png">
 </div>

 <div id="RAW_data" class="selectableTabContent" style="display:none">
  <p>
   The main raw results of Klask are accessible directly,
   via <tt>wget</tt> or <tt>curl</tt> tools for example,
   for any specific treatment:
   <ul>
    <li><a href="hostname-db.txt">Computers DB</a> (command <tt>klask exportdb --format txt</tt>),</li>
    <li><a href="switch-db.txt">Switches DB</a> (command <tt>klask exportsw --format txt</tt>),</li>
    <li><a href="ipfree-db.txt">IP available</a> (command <tt>klask ip-free --format txt</tt>),</li>
    <li><a href="badvlan-db.txt">VLAN mismatch</a> (command <tt>klask bad-vlan-id --format txt</tt>),</li>
    <li>Map format: <a href="switch-map.txt">dot source</a> (graphviz), <a href="switch-map.png">png</a>, <a href="switch-map.svg">svg</a>.</li>
   </ul>
  </p>
 </div>
</div>

<p class="copyright">
Klask (version $(klask version | grep ^Version | cut -f 2 -d ' ')) - $(klask version | grep ^Copyright | cut -f 1 -d '<')
</p>
</body>
</html>
END


if [ "${REMOTE_SERVER}" = "localhost" ]
then
   rsync \
      /usr/share/klask/sorttable-klask.js \
      /usr/share/klask/style-klask.css \
      ${TMP}/index.html \
      ${TMP}/*-db.txt \
      ${TMP}/switch-map.txt \
      ${REMOTE_FOLDER}/

   [ -x ${DOT} ] && (cd ${REMOTE_FOLDER}; ${DOT} -T svg switch-map.txt > switch-map.svg; ${DOT} -T png switch-map.txt > switch-map.png)

else
   rsync \
      /usr/share/klask/sorttable-klask.js \
      /usr/share/klask/style-klask.css \
      ${TMP}/index.html \
      ${TMP}/*-db.txt \
      ${TMP}/switch-map.txt \
      ${REMOTE_USER}@${REMOTE_SERVER}:${REMOTE_FOLDER}/


   ssh ${REMOTE_USER}@${REMOTE_SERVER} "(cd ${REMOTE_FOLDER}; ${DOT} -T svg switch-map.txt > switch-map.svg; ${DOT} -T png switch-map.txt > switch-map.png)"
fi
