# Greenbone Security Assistant
# $Id$
# Description: Top-level cmake control for greenbone-security-assistant
#
# Authors:
# Matthew Mundell <matthew.mundell@greenbone.net>
# Michael Wiegand <michael.wiegand@greenbone.net>
#
# Copyright:
# Copyright (C) 2009 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 version 2,
# or, at your option, any later version as published by the Free
# Software Foundation
#
# 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.

# Needed for 'pkg_check_modules' function
include (FindPkgConfig)

message ("-- Configuring greenbone-security-assistant...")

project (greenbone-security-assistant C)

cmake_minimum_required (VERSION 2.6)

if (POLICY CMP0005)
  cmake_policy (SET CMP0005 OLD)
endif (POLICY CMP0005)

if (NOT CMAKE_BUILD_TYPE)
  set (CMAKE_BUILD_TYPE Debug)
endif (NOT CMAKE_BUILD_TYPE)

## Check existence required tools

set (MANDATORY_TOOL_MISSING FALSE)

find_program (PATH_TO_PKGCONFIG pkg-config DOC "pkg-config program to retrieve "
                        "information about installed libraries in the system.")

find_program (PATH_TO_LIBOPENVASCONFIG libopenvas-config DOC "libopenvas-config program"
                        " to retrieve information about installed"
                        " openvas-libraries in the system.")

if (NOT USE_LIBXSLT)
  set (USE_LIBXSLT 0)
endif (NOT USE_LIBXSLT)

find_program (PATH_TO_XSLTPROC xsltproc DOC "xsltproc command line "
              "xslt processor.")
if (NOT USE_LIBXSLT AND NOT PATH_TO_XSLTPROC)
  set (MANDATORY_TOOL_MISSING TRUE)
endif (NOT USE_LIBXSLT AND NOT PATH_TO_XSLTPROC)

## TODO this check is obsolete with the FindPkgConfig module (see below,
## pkg_check_module sets OKG_CONFIG_FOUND)
message (STATUS "Looking for pkg-config... ${PATH_TO_PKGCONFIG}")
if (NOT PATH_TO_PKGCONFIG)
  set (MANDATORY_TOOL_MISSING TRUE)
endif (NOT PATH_TO_PKGCONFIG)

if (MANDATORY_TOOL_MISSING)
  if (NOT PATH_TO_PKGCONFIG)
    message ("The pkg-config tool is required.")
  endif (NOT PATH_TO_PKGCONFIG)
  message (FATAL_ERROR "One or more tools or libraries could not be found on "
                      "your system. Please check the logs above.")
endif (MANDATORY_TOOL_MISSING)

# TODO: Check for (optional) rats, flawfinder, splint, doxygen.

## Check for existance of required libraries
pkg_check_modules (LIBMICROHTTPD REQUIRED libmicrohttpd>=0.4.2)
pkg_check_modules (LIBXML REQUIRED libxml-2.0)
pkg_check_modules (GLIB REQUIRED glib-2.0>=2.14)
pkg_check_modules (GTHREAD REQUIRED gthread-2.0)
pkg_check_modules (LIBEXSLT REQUIRED libexslt)
pkg_check_modules (LIBOPENVAS REQUIRED libopenvas>=4.0.0)
pkg_check_modules (LIBXSLT REQUIRED libxslt)
pkg_check_modules (GNUTLS REQUIRED gnutls)

execute_process (COMMAND pkg-config --cflags libmicrohttpd
  OUTPUT_VARIABLE LIBMICROHTTPD_CFLAGS
  OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process (COMMAND pkg-config --libs libmicrohttpd
  OUTPUT_VARIABLE LIBMICROHTTPD_LDFLAGS
  OUTPUT_STRIP_TRAILING_WHITESPACE)

execute_process (COMMAND pkg-config --cflags libxml-2.0
  OUTPUT_VARIABLE LIBXML_CFLAGS
  OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process (COMMAND pkg-config --libs libxml-2.0
  OUTPUT_VARIABLE LIBXML_LDFLAGS
  OUTPUT_STRIP_TRAILING_WHITESPACE)

execute_process (COMMAND pkg-config --cflags glib-2.0
  OUTPUT_VARIABLE GLIB_CFLAGS
  OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process (COMMAND pkg-config --libs glib-2.0
  OUTPUT_VARIABLE GLIB_LDFLAGS
  OUTPUT_STRIP_TRAILING_WHITESPACE)

execute_process (COMMAND pkg-config --cflags gthread-2.0
  OUTPUT_VARIABLE GTHREAD_CFLAGS
  OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process (COMMAND pkg-config --libs gthread-2.0
  OUTPUT_VARIABLE GTHREAD_LDFLAGS
  OUTPUT_STRIP_TRAILING_WHITESPACE)

execute_process (COMMAND pkg-config --cflags libexslt
  OUTPUT_VARIABLE LIBEXSLT_CFLAGS
  OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process (COMMAND pkg-config --libs libexslt
  OUTPUT_VARIABLE LIBEXSLT_LDFLAGS
  OUTPUT_STRIP_TRAILING_WHITESPACE)

execute_process (COMMAND pkg-config --cflags libopenvas
  OUTPUT_VARIABLE OPENVAS_CFLAGS
  OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process (COMMAND pkg-config --libs libopenvas
  OUTPUT_VARIABLE OPENVAS_LDFLAGS
  OUTPUT_STRIP_TRAILING_WHITESPACE)

execute_process (COMMAND pkg-config --cflags libxslt
  OUTPUT_VARIABLE LIBXSLT_CFLAGS
  OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process (COMMAND pkg-config --libs libxslt
  OUTPUT_VARIABLE LIBXSLT_LDFLAGS
  OUTPUT_STRIP_TRAILING_WHITESPACE)

execute_process (COMMAND pkg-config --cflags gnutls
  OUTPUT_VARIABLE GNUTLS_CFLAGS
  OUTPUT_STRIP_TRAILING_WHITESPACE)
execute_process (COMMAND pkg-config --libs gnutls
  OUTPUT_VARIABLE GNUTLS_LDFLAGS
  OUTPUT_STRIP_TRAILING_WHITESPACE)

if (NOT LIBMICROHTTPD_FOUND OR NOT LIBXML_FOUND OR NOT GLIB_FOUND OR NOT GTHREAD_FOUND OR
    NOT LIBEXSLT_FOUND OR NOT LIBOPENVAS_FOUND OR NOT LIBXSLT_FOUND OR NOT GNUTLS_FOUND)
  message (FATAL_ERROR "One or more required libraries was not found "
    "(see message above), please install the missing "
    "libraries and run cmake again.")
endif (NOT LIBMICROHTTPD_FOUND OR NOT LIBXML_FOUND OR NOT GLIB_FOUND OR NOT GTHREAD_FOUND OR
  NOT LIBEXSLT_FOUND OR NOT LIBOPENVAS_FOUND OR NOT LIBXSLT_FOUND OR NOT GNUTLS_FOUND)

## Retrieve svn revision (at configure time)
#  Not using Subversion_WC_INFO, as it would have to connect to the repo
find_program (SVN_EXECUTABLE svn DOC "subversion command line client")

macro (Subversion_GET_REVISION dir variable)
  execute_process (COMMAND ${SVN_EXECUTABLE} info ${CMAKE_SOURCE_DIR}/${dir}
    OUTPUT_VARIABLE ${variable}
    OUTPUT_STRIP_TRAILING_WHITESPACE)
  string (REGEX REPLACE "^(.*\n)?Revision: ([^\n]+).*"
    "\\2" ${variable} "${${variable}}")
endmacro (Subversion_GET_REVISION)

if (NOT CMAKE_BUILD_TYPE MATCHES "Release")
  if (EXISTS ".svn/")
    if (SVN_EXECUTABLE)
      Subversion_GET_REVISION(. ProjectRevision)
      set (SVN_REVISION ".SVN.r${ProjectRevision}")
    else (SVN_EXECUTABLE)
      set (SVN_REVISION ".SVN")
    endif (SVN_EXECUTABLE)
  endif (EXISTS ".svn/")
endif (NOT CMAKE_BUILD_TYPE MATCHES "Release")

## CPack configuration

set (CPACK_CMAKE_GENERATOR "Unix Makefiles")
set (CPACK_GENERATOR "TGZ")
set (CPACK_INSTALL_CMAKE_PROJECTS ".;greenbone-security-assistant;ALL;/")
set (CPACK_MODULE_PATH "")
set (CPACK_RESOURCE_FILE_LICENSE "${CMAKE_SOURCE_DIR}/COPYING")
set (CPACK_RESOURCE_FILE_README "${CMAKE_SOURCE_DIR}/README")
set (CPACK_RESOURCE_FILE_WELCOME "${CMAKE_SOURCE_DIR}/README")
set (CPACK_SOURCE_GENERATOR "TGZ")
set (CPACK_SOURCE_TOPLEVEL_TAG "")
set (CPACK_SYSTEM_NAME "")
set (CPACK_TOPLEVEL_TAG "")
set (CPACK_PACKAGE_VERSION_MAJOR "2")
set (CPACK_PACKAGE_VERSION_MINOR "0")
set (CPACK_PACKAGE_VERSION_PATCH "1${SVN_REVISION}")
set (CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
set (CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}-${CPACK_PACKAGE_VERSION}")
set (CPACK_SOURCE_PACKAGE_FILE_NAME "${PROJECT_NAME}-${CPACK_PACKAGE_VERSION}")
set (CPACK_PACKAGE_VENDOR "The OpenVAS Project")
set (CPACK_SOURCE_IGNORE_FILES
"/.svn/"
"/_CPack_Packages/"
"/CMakeFiles/"
"cmake$"
"swp$"
"Cache.txt$"
".tar.gz"
"src/libgsad_omp.a$"
"src/libgsad_oap.a$"
"src/libgsad_base.a$"
"src/gsad$"
"install_manifest.txt"
"Makefile"
"src/html/build/"
"/doc/generated/"
"log.conf$"
"Doxyfile$"
"Doxyfile_full$"
"VERSION$"
)

include (CPack)

## Variables

if (NOT SYSCONFDIR)
  set (SYSCONFDIR "${CMAKE_INSTALL_PREFIX}/etc")
endif (NOT SYSCONFDIR)

if (NOT EXEC_PREFIX)
  set (EXEC_PREFIX "${CMAKE_INSTALL_PREFIX}")
endif (NOT EXEC_PREFIX)

if (NOT BINDIR)
  set (BINDIR "${EXEC_PREFIX}/bin")
endif (NOT BINDIR)

if (NOT SBINDIR)
  set (SBINDIR "${EXEC_PREFIX}/sbin")
endif (NOT SBINDIR)

if (NOT LIBDIR)
  set (LIBDIR "${EXEC_PREFIX}/lib")
endif (NOT LIBDIR)

if (NOT LOCALSTATEDIR)
  set (LOCALSTATEDIR "${CMAKE_INSTALL_PREFIX}/var")
endif (NOT LOCALSTATEDIR)

if (NOT INCLUDEDIR)
  set (INCLUDEDIR "${CMAKE_INSTALL_PREFIX}/include")
endif (NOT INCLUDEDIR)

if (NOT DATADIR)
  set (DATADIR "${CMAKE_INSTALL_PREFIX}/share")
endif (NOT DATADIR)

set (OPENVAS_STATE_DIR "${LOCALSTATEDIR}/lib/openvas")
set (OPENVAS_LOG_DIR   "${LOCALSTATEDIR}/log/openvas")
set (GSAD_PID_DIR   "${LOCALSTATEDIR}/run")
set (GSA_DATA_DIR "${DATADIR}/openvas/gsa")

set (OPENVAS_LIB_INSTALL_DIR    "${LIBDIR}")
set (OPENVAS_HEADER_INSTALL_DIR "${INCLUDEDIR}")
# TODO: Eventually use own certificates
set (OPENVAS_SERVER_CERTIFICATE "${OPENVAS_STATE_DIR}/CA/servercert.pem")
set (OPENVAS_SERVER_KEY         "${OPENVAS_STATE_DIR}/private/CA/serverkey.pem")
set (OPENVAS_CA_CERTIFICATE     "${OPENVAS_STATE_DIR}/CA/cacert.pem")

# TODO: Eventually have all configurations collected here:
set (GSA_CONFIG_DIR         "${SYSCONFDIR}/openvas/")
set (GSA_CONFIG_FILE        "${SYSCONFDIR}/openvas/gsa.conf")

message ("-- Install prefix: ${CMAKE_INSTALL_PREFIX}")

## Version

string (REPLACE "
" "" GSAD_VERSION ${CPACK_PACKAGE_VERSION})

# Configure Doxyfile with version number
configure_file (doc/Doxyfile.in doc/Doxyfile)
configure_file (doc/Doxyfile_full.in doc/Doxyfile_full)
configure_file (VERSION.in VERSION)
configure_file (src/gsad_log_conf.cmake_in src/gsad_log.conf)

## Install

install (FILES src/html/favicon.gif src/html/gsad.xsl src/html/gsa-style.css
               src/html/help.xsl  src/html/oap.xsl  src/html/omp.xsl
               src/html/IE6fixes.css
         DESTINATION ${GSA_DATA_DIR})

install (FILES src/html/login/login.html src/html/gsa-style.css
               src/html/favicon.gif src/html/IE6fixes.css
         DESTINATION ${GSA_DATA_DIR}/login)

install (FILES src/html/img/gsa_splash.png
         DESTINATION ${GSA_DATA_DIR}/login/img)

install (FILES src/html/img/style/window_dec_a.png
               src/html/img/style/window_dec_b.png
               src/html/img/style/window_dec_c.png
         DESTINATION ${GSA_DATA_DIR}/login/img/style)

install (FILES src/html/img/agent.png src/html/img/descending.png
               src/html/img/key.png src/html/img/p_bar_bg.png
               src/html/img/start_inactive.png src/html/img/alert_sign.png
               src/html/img/details.png src/html/img/list_inactive.png
               src/html/img/p_bar_done.png src/html/img/start.png
               src/html/img/ascending_inactive.png src/html/img/download.png
               src/html/img/list.png src/html/img/p_bar_error.png
               src/html/img/stop_inactive.png src/html/img/ascending.png
               src/html/img/edit_inactive.png src/html/img/log.png
               src/html/img/p_bar_new.png src/html/img/stop.png
               src/html/img/edit.png
               src/html/img/low_big.png src/html/img/p_bar.png
               src/html/img/bullet2.png src/html/img/exe.png
               src/html/img/low.png src/html/img/p_bar_request.png
               src/html/img/trend_down.png src/html/img/bullet.png
               src/html/img/medium_big.png
               src/html/img/refresh.png src/html/img/trend_less.png
               src/html/img/deb.png src/html/img/gsa_splash.png
               src/html/img/medium.png src/html/img/resume_inactive.png
               src/html/img/trend_more.png src/html/img/delete_inactive.png
               src/html/img/help.png src/html/img/new_note.png
               src/html/img/resume.png src/html/img/trend_nochange.png
               src/html/img/delete_note.png src/html/img/high_big.png
               src/html/img/new.png src/html/img/rpm.png
               src/html/img/trend_up.png src/html/img/delete.png
               src/html/img/high.png src/html/img/none_big.png
               src/html/img/scheduled_inactive.png
               src/html/img/descending_inactive.png
               src/html/img/note.png
               src/html/img/scheduled.png src/html/img/pause.png
               src/html/img/override.png src/html/img/new_override.png
               src/html/img/pause_inactive.png src/html/img/false_positive.png
			   src/html/img/indicator_js.png
         DESTINATION ${GSA_DATA_DIR}/img)

install (FILES src/html/img/style/logo_l.png src/html/img/style/logo_r.png
               src/html/img/style/window_dec_a.png
               src/html/img/style/window_dec_b.png
               src/html/img/style/window_dec_c.png src/html/img/style/logo_m.png
               src/html/img/style/window_dec_a_error.png
               src/html/img/style/window_dec_b_error.png
               src/html/img/style/window_dec_c_error.png
         DESTINATION ${GSA_DATA_DIR}/img/style)

install (FILES src/gsad_log.conf
         DESTINATION ${GSA_CONFIG_DIR})

## Program

set (HARDENING_FLAGS            "-Wformat -Wformat-security -O2 -D_FORTIFY_SOURCE=2 -fstack-protector -Wl,-z,relro -Wl,-z,now")

set (CMAKE_C_FLAGS_DEBUG        "${CMAKE_C_FLAGS_DEBUG}")
set (CMAKE_C_FLAGS              "${CMAKE_C_FLAGS} ${HARDENING_FLAGS} -Wall -Werror")

add_subdirectory (src)

## Documentation

add_subdirectory (doc)

## End
