#!/bin/bash
#
# 2007/10/23 gabriel
# 2010/11/03 gabriel
# 2011/03/30 gabriel - make it generic

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>
<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);
 }
 </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> (<a href="switch-map.png">png</a>|<a href="switch-map.svg">svg</a>)
the local network [${LOCALTIME}] - site ${SITE_NAME}
</h1>

<div id="navbar">
 <ul>
  <li id="selected"><a href="index.html">IP detected</a></li>
  <li><a href="ip-free.html">IP available</a></li>
  <li><a href="bad-vlan-id.html">VLAN mismatch</a></li>
 </ul>
</div>

<div id="content">
<p>
The main raw results of Klask are accessible directly, via wget or curl for example, for a specific treatment:
<a href="hostname-db.txt">Computers DB</a>,
<a href="switch-db.txt">Switches DB</a>,
<a href="ipfree-db.txt">IP available</a>,
<a href="badvlan-db.txt">VLAN mismatch</a>,
<a href="switch-map.txt">Map dot source</a>.
</p>
END

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

cat <<END >> ${TMP}/index.html
</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


cat <<END > ${TMP}/ip-free.html
<html>
<head>
 <link rel="stylesheet" type="text/css" href="style-klask.css" />
 <meta http-equiv="refresh" content="1800">
 <script src="sorttable-klask.js"></script>
</head>
<body>
<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> (<a href="switch-map.png">png</a>|<a href="switch-map.svg">svg</a>)
the local network [${LOCALTIME}] - site ${SITE_NAME}
</h1>

<div id="navbar">
 <ul>
  <li><a href="index.html">IP detected</a></li>
  <li id="selected"><a href="ip-free.html">IP available</a></li>
  <li><a href="bad-vlan-id.html">VLAN mismatch</a></li>
 </ul>
</div>

<div id="content">
<p>
The main raw results of Klask are accessible directly, via wget or curl for example, for a specific treatment:
<a href="hostname-db.txt">Computers DB</a>,
<a href="switch-db.txt">Switches DB</a>,
<a href="ipfree-db.txt">IP available</a>,
<a href="bad-vlan-id-db.txt">VLAN mismatch</a>,
<a href="switch-map.txt">Map dot source</a>.
</p>
END

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

cat <<END >> ${TMP}/ip-free.html
</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


cat <<END > ${TMP}/bad-vlan-id.html
<html>
<head>
 <link rel="stylesheet" type="text/css" href="style-klask.css" />
 <meta http-equiv="refresh" content="1800">
 <script src="sorttable-klask.js"></script>
</head>
<body>
<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> (<a href="switch-map.png">png</a>|<a href="switch-map.svg">svg</a>)
the local network [${LOCALTIME}] - site ${SITE_NAME}
</h1>

<div id="navbar">
 <ul>
  <li><a href="index.html">IP detected</a></li>
  <li><a href="ip-free.html">IP available</a></li>
  <li id="selected"><a href="bad-vlan-id.html">VLAN mismatch</a></li>
 </ul>
</div>

<div id="content">
<p>
The main raw results of Klask are accessible directly, via wget or curl for example, for a specific treatment:
<a href="hostname-db.txt">Computers DB</a>,
<a href="switch-db.txt">Switches DB</a>,
<a href="ipfree-db.txt">IP available</a>,
<a href="bad-vlan-id-db.txt">VLAN mismatch</a>,
<a href="switch-map.txt">Map dot source</a>.
</p>
END

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

cat <<END >> ${TMP}/bad-vlan-id.html
</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}/ip-free.html \
      ${TMP}/bad-vlan-id.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}/ip-free.html \
      ${TMP}/bad-vlan-id.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
