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

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'


# local command
MKDIR=/bin/mkdir
DATE=/bin/date
CAT=/bin/cat
TAIL=/usr/bin/tail
HEAD=/usr/bin/head
SSH=/usr/bin/ssh
SCP=/usr/bin/scp
KLASK=/usr/sbin/klask
RSYNC=/usr/bin/rsync

# 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
<html>
<head>
 <link rel="stylesheet" type="text/css" href="style-klask.css" />
 <meta http-equiv="refresh" content="1800">
 <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">Bad VLAN ID</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</a>,
<a href="switch-db.txt">Switches</a>,
<a href="ipfree-db.txt">IP available</a>,
<a href="badvlan-db.txt">Bad VLAN ID</a>,
<a href="switch-map.txt">Map</a>.
</p>
END

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

$CAT <<END >> ${TMP}/index.html
</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">Bad VLAN ID</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</a>,
<a href="switch-db.txt">Switches</a>,
<a href="ipfree-db.txt">IP available</a>,
<a href="bad-vlan-id-db.txt">Bad VLAN ID</a>,
<a href="switch-map.txt">Map</a>.
</p>
END

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

$CAT <<END >> ${TMP}/ip-free.html
</div>
</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">Bad VLAN ID</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</a>,
<a href="switch-db.txt">Switches</a>,
<a href="ipfree-db.txt">IP available</a>,
<a href="bad-vlan-id-db.txt">Bad VLAN ID</a>,
<a href="switch-map.txt">Map</a>.
</p>
END

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

$CAT <<END >> ${TMP}/bad-vlan-id.html
</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
