#!/bin/sh
#
# OpenVAS
# $Id$
# Description: Mapgenerator for geographically located target systems
#
# This mapgenerator creates a PNG image showing hosts on a worldmap.
# The first step is to create a CSV file from the OMP report using xsltproc.
# Then the file is merged with the file contianing the location information.
# To generate the map, the resulting CSV file is converted into the shape format
# used by MapServer and then drawn to the image file using shp2img.
#
# Authors:
# Raimund Renkert <raimund.renkert@greenbone.net>
# Jan-Oliver Wagner <jan-oliver.wagner@greenbone.net>
#
# Copyright:
# Copyright (C) 2010 Greenbone Networks GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.

TMP=`mktemp -d` || exit 1
RWD=`pwd | sed -e "s/\//\\\\\\\\\//g"`

# Create csv file from report using xslt.
xsltproc ./report_to_csv_host.xsl $1 | grep -v "^ *$" > ${TMP}/hosts.csv

# Remove leading whitespaces from file.
sed -e 's/^[ \t]*//' ${TMP}/hosts.csv > ${TMP}/hosts_clean.csv

# Merge locations file and host file
cat locations.csv ${TMP}/hosts_clean.csv | awk -f ./join.awk > \
  ${TMP}/host_locations.csv

# We need the .vrt file where the shapefile is
cp hosts.vrt ${TMP}

# Set the paths
echo  "sed -e \"s/__RWD__/"${RWD}"/g\"" > ${TMP}/do_sed
. ${TMP}/do_sed < world_map.map > ${TMP}/world_map.map

# Create shape file using ogr2ogr.
( cd ${TMP} ; ogr2ogr -f "ESRI Shapefile" . host_locations.csv )
( cd ${TMP} ; ogr2ogr -f "ESRI Shapefile" . hosts.vrt )

# Get host count (all hosts)
HIGH_COUNTER_1=`grep ", red" ${TMP}/hosts_clean.csv | wc -l`
MED_COUNTER_1=`grep ", orange" ${TMP}/hosts_clean.csv | wc -l`
LOW_COUNTER_1=`grep ", blue" ${TMP}/hosts_clean.csv | wc -l`

# Get host count (referenced hosts)
HIGH_COUNTER_2=`grep ", red" ${TMP}/host_locations.csv | wc -l`
MED_COUNTER_2=`grep ", orange" ${TMP}/host_locations.csv | wc -l`
LOW_COUNTER_2=`grep ", blue" ${TMP}/host_locations.csv | wc -l`

# Calculate none referenced hosts
HIGH_COUNT=$((HIGH_COUNTER_1-HIGH_COUNTER_2))
MED_COUNT=$((MED_COUNTER_1-MED_COUNTER_2))
LOW_COUNT=$((LOW_COUNTER_1-LOW_COUNTER_2))

sed -e "s/target_count_high/$HIGH_COUNT/g; \
  s/target_count_med/$MED_COUNT/g; \
  s/target_count_low/$LOW_COUNT/g" \
  non-georeferenced.dot > ${TMP}/non-georeferenced.dot

cp non-georeferenced.map ${TMP}
twopi -Tpng ${TMP}/non-georeferenced.dot > ${TMP}/non-georeferenced.png

# Render image
( cd "${TMP}" ; shp2img -m world_map.map -o "${TMP}/out.png" )

cat "${TMP}/out.png" && rm -rf "${TMP}"

