#!/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/

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 > ${TMP}/switch-map.txt


$KLASK exportdb > ${TMP}/hostname-db.txt
$KLASK exportsw > ${TMP}/switch-db.txt
$KLASK ip-free  > ${TMP}/ipfree-db.txt


$CAT <<END > ${TMP}/index.html
<html>
<head>
 <link rel="stylesheet" type="text/css" href="style-klask.css" />
 <meta http-equiv="refresh" content="3600">
 <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">Klask</a></b>
- Un outil pour <a href="switch-map.png">cartographier</a> le r&eacute;seau [${LOCALTIME}] - site ${SITE_NAME}
</h1>

<p>
Ce portail pr&eacute;sente la liste de toutes les IP d&eacute;tect&eacute;es par Klask.
La liste des <a href="ip-free.html">IP non d&eacute;tect&eacute;es</a> par Klask depuis plus de deux ann&eacute;es est aussi disponible.
</p>

<p>
Les r&eacute;sultats brut de Klask sont accessibles directement via wget ou curl, par exemple, pour un traitement sp&eacute;cifique :
<a href="hostname-db.txt">Machines</a>,
<a href="switch-db.txt">Commutateurs</a>,
<a href="ipfree-db.txt">IP disponibles</a>,
<a href="switch-map.txt">Carte</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="3600">
 <script src="sorttable-klask.js"></script>
</head>
<body>
<h1>
<b><a href="http://servforge.legi.grenoble-inp.fr/projects/klask">Klask</a></b>
- Un outil pour <a href="switch-map.png">cartographier</a> le r&eacute;seau [${LOCALTIME}] - site ${SITE_NAME}
</h1>

<p>
Cette page pr&eacute;sente la liste des IP non d&eacute;tect&eacute;es par Klask depuis plus de deux ann&eacute;es.
Le portail contient la liste de toutes les <a href="index.html">IP d&eacute;tect&eacute;es</a> par Klask.
</p>

<p>
Les r&eacute;sultats brut de Klask sont accessibles directement via wget ou curl, par exemple, pour un traitement sp&eacute;cifique :
<a href="hostname-db.txt">Machines</a>,
<a href="switch-db.txt">Commutateurs</a>,
<a href="ipfree-db.txt">IP disponibles</a>,
<a href="switch-map.txt">Carte</a>.
</p>
END

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

$CAT <<END >> ${TMP}/ip-free.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}/*-db.txt \
      ${TMP}/switch-map.txt \
      ${REMOTE_FOLDER}/

   [ -x ${DOT} ] && (cd ${REMOTE_FOLDER}; ${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}/*-db.txt \
      ${TMP}/switch-map.txt \
      ${REMOTE_USER}@${REMOTE_SERVER}:${REMOTE_FOLDER}/


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