#!/bin/bash
#
# OpenVAS
# $Id$
# Description: Create signatures for predefined reports.
#
# Call in openvas-manager/src/report_formats.  Pass one argument, the name
# of the Manager database.  Signatures are written into report format subdirs.
#
# Authors:
# Matthew Mundell <matthew.mundell@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.

DB=$1
if [ $# = 0 ]; then DB=/var/lib/openvas/mgr/tasks.db; fi

export LC_ALL=C
echo -n Faking latex.xsl for PDF
ln -s ../LaTeX/latex.xsl PDF/latex.xsl || exit 1
echo .
echo Creating signatures from $DB:
for uuid in `sqlite3 -noheader -init "" $DB 'SELECT uuid FROM report_formats' 2>/dev/null`; do
  name=`sqlite3 -noheader -init "" $DB "SELECT replace (name, ' ', '_') FROM report_formats WHERE uuid = '$uuid'" 2>/dev/null`
  id=`sqlite3 -noheader -init "" $DB "SELECT ROWID FROM report_formats WHERE uuid = '$uuid'" 2>/dev/null`
  if [ -d "$name" ]; then
    echo "  Format $name with UUID $uuid and ROWID $id"
    rm -f "${name}/${uuid}.asc"
    rm -f ${uuid}
    fore=`sqlite3 -separator '' -noheader -init "" $DB "SELECT uuid, extension, content_type, '1' FROM report_formats WHERE uuid = '$uuid'" 2>/dev/null`
    echo -n "$fore" > ${uuid}
    for file in `ls "$name"`; do
      echo "    File $name/$file"
      case "$file" in
        *.BAK)
          ;;
        *.CKP)
          ;;
        *~)
          ;;
        *.bak)
          ;;
        *)
          echo -n `basename "$name/$file"` >> ${uuid}
          base64 -w 0 "$name/$file" >> ${uuid}
          ;;
      esac
    done
    for param in `sqlite3 -noheader -init "" $DB "SELECT ROWID FROM report_format_params WHERE report_format = $id" 2>/dev/null`; do
      echo "    Param $param"
      back=`sqlite3 -separator '' -noheader -init "" $DB "SELECT name FROM report_format_params WHERE ROWID = $param" 2>/dev/null`
      echo -n "$back" >> ${uuid}
      type=`sqlite3 -noheader -init "" $DB "SELECT type FROM report_format_params WHERE ROWID = $param" 2>/dev/null`
      case $type in
        0)
          echo -n boolean >> ${uuid}
          ;;
        1)
          echo -n integer >> ${uuid}
          ;;
        2)
          echo -n selection >> ${uuid}
          ;;
        3)
          echo -n string >> ${uuid}
          ;;
        4)
          echo -n text >> ${uuid}
          ;;
        *)
          echo Error: report format type is $type.
          exit 1
          ;;
      esac
      back=`sqlite3 -separator '' -noheader -init "" $DB "SELECT min, max, value FROM report_format_params WHERE ROWID = $param" 2>/dev/null`
      echo -n "$back" >> ${uuid}
      for value in `sqlite3 -noheader -init "" $DB "SELECT ROWID FROM report_format_param_options WHERE ROWID = $option" 2>/dev/null`/*; do
        echo "    Param option $value"
        echo -n "$value" >> ${uuid}
      done
    done
    echo >> ${uuid}
    gpg --detach-sign --armor ${uuid}
    rm -f ${uuid}
    mv ${uuid}.asc "${name}/${uuid}.asc"
  fi
done
echo -n Remove fake latex.xsl
rm PDF/latex.xsl || exit 1
echo .
echo Done.
