#############################################################################
#
#  $Id$
#
#  Copyright 2013 Andreas Stewering-Bone
#
#  This is the main cmake file for the canrestbus package
#
#  The following options can be specified on the command line of cmake to
#  change the installation paths (Defaults are in <>)
#       -DCMAKE_INSTALL_PREFIX=</usr/local>
#       -DCMAKE_INSTALL_INCLUDEDIR=<${CMAKE_INSTALL_PREFIX}/include>
#       -DCMAKE_INSTALL_LIBDIR=<${CMAKE_INSTALL_PREFIX}/lib(64)>
#       -DCMAKE_INSTALL_LOCALSTATEDIR=<${CMAKE_INSTALL_PREFIX}/var>
#
#  For debugging, use the
#       -DCMAKE_BUILD_TYPE=Debug|Release|RelWithDebInfo|MinSizeRel
#  option
#
#  For Runtime debugging, use the
#       -DDEBUG=1, or
#       -DVERBOSE=1
#
#  For building the library tool box, use
#       -DBUILDTOOLS=1
#
#  For building the library tests, use
#       -DBUILDTEST=1
#
#  For installing Matlab interface, use
#       -DMATLAB=1
#
#
#  This file is part of the canrestbus package.
#
#  canrestbus 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 3 of the License, or
#  (at your option) any later version.
#
#  canrestbus 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 canrestbus. See COPYING. If not, see
#  <http://www.gnu.org/licenses/>.
#
#############################################################################


CMAKE_MINIMUM_REQUIRED (VERSION 2.8.5)
PROJECT (canrestbus)
SET (CMAKE_ALLOW_LOOSE_LOOP_CONSTRUCTS TRUE)

LIST (APPEND CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake/Modules/")

#
# Release Instructions
#
# - Update version numbers below
# - Update SOVERSION below
# - Update ChangeLog (hg log --branch <branch> --style=changelog > ChangeLog)
# - Update NEWS file
# - make dist
#
SET (MAJOR_VERSION 2)
SET (MINOR_VERSION 1)
SET (PATCH_LEVEL 0)
SET (VERSION ${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_LEVEL})

# library version
SET (SOVERSION ${MAJOR_VERSION}.${MINOR_VERSION})

IF (DEFINED PREFIX)
    MESSAGE (WARNING "Using PREFIX is deprecated. Use CMAKE_INSTALL_PREFIX")
    SET (CMAKE_INSTALL_PREFIX ${PREFIX})
ENDIF ()

# GNU Paths:
#       CMAKE_INSTALL_*
INCLUDE (GNUInstallDirs)

OPTION ( DEBUG "Debugging output" OFF )
SET  (CS_DEBUG ${DEBUG})

OPTION ( VERBOSE "Verbose output" OFF )
SET  (CS_VERBOSE ${VERBOSE})

OPTION ( BUILDTOOLS "Build library tools" OFF )
SET  (CS_BUILDTOOLS ${BUILDTOOLS})

OPTION ( BUILDTEST "Build test suite tools" OFF )
SET  (CS_BUILDTEST ${BUILDTEST})

OPTION ( MATLAB "Install Matlan interface" OFF )
SET  (CS_MATLAB ${MATLAB})

if (DEBUG)
    string(LENGTH "${PROJECT_SOURCE_DIR}/src/" SRC_PATH_LENGTH)
endif (DEBUG)

FIND_PACKAGE (PkgConfig REQUIRED)
PKG_CHECK_MODULES (LIBPCRE REQUIRED libpcre)

# Search for mercurial, used to add ID information to the library
FIND_PACKAGE (Mercurial)
IF (MERCURIAL_FOUND AND EXISTS "${CMAKE_SOURCE_DIR}/.hg")
    MERCURIAL_HG_INFO (${PROJECT_SOURCE_DIR} Package)
ENDIF ()

SET (PROJECT_INCLUDE_DIR "${CMAKE_SOURCE_DIR}/include")

ADD_SUBDIRECTORY (src)
if (BUILDTEST)
  ADD_SUBDIRECTORY (test)
endif (BUILDTEST)
if (BUILDTOOLS)
  ADD_SUBDIRECTORY (tools)
endif(BUILDTOOLS)
if (MATLAB)
  ADD_SUBDIRECTORY (matlab)
endif(MATLAB)

INCLUDE (InstallRequiredSystemLibraries)

SET (CPACK_PACKAGE_DESCRIPTION_SUMMARY
    "CAN Restbus library, UDS-, XCP-, CCP-Stack, ASC- TRC-Fileparser, VBF-Container handler")
SET (CPACK_PACKAGE_VENDOR "Andreas Stewering-Bone <andreas.stewering-bone@gmx.de>")
SET (CPACK_PACKAGE_DESCRIPTION_FILE "${CMAKE_CURRENT_SOURCE_DIR}/README")
SET (CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/COPYING")
SET (CPACK_PACKAGE_VERSION_MAJOR ${MAJOR_VERSION})
SET (CPACK_PACKAGE_VERSION_MINOR ${MINOR_VERSION})
SET (CPACK_PACKAGE_VERSION_PATCH ${PATCH_LEVEL})
SET (CPACK_PACKAGE_INSTALL_DIRECTORY
    "CMake ${CMake_VERSION_MAJOR}.${CMake_VERSION_MINOR}")
SET (CPACK_SOURCE_GENERATOR "TBZ2")
SET (CPACK_SOURCE_PACKAGE_FILE_NAME
      "${PROJECT_NAME}-${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}")
SET (CPACK_GENERATOR "TGZ;DEB;RPM")


SET (CPACK_DEBIAN_PACKAGE_MAINTAINER "Andreas Stewering-Bone") #required
SET (CPACK_DEBIAN_PACKAGE_DEPENDS "libc6 (>= 2.7), libpcre3 (>= 8.10)")
SET (CPACK_DEBIAN_PACKAGE_SECTION "devel")
SET (CPACK_DEBIAN_PACKAGE_PRIORITY "optional")
SET (CPACK_DEBIAN_PACKAGE_HOMEPAGE "www.etherlab.org")
SET (CPACK_DEBIAN_PACKAGE_VERSION 1)

#TODO fix plattform string
SET (CPACK_PACKAGE_FILE_NAME "${PROJECT_NAME}_${CPACK_PACKAGE_VERSION_MAJOR}.${CPACK_PACKAGE_VERSION_MINOR}.${CPACK_PACKAGE_VERSION_PATCH}-${CPACK_DEBIAN_PACKAGE_VERSION}_i386")

LIST (APPEND CPACK_SOURCE_IGNORE_FILES
  "/\\\\.hg"
  "/build*"
  "\\\\.sw.$"
  "~$"
  )

INCLUDE (CPack)
ADD_CUSTOM_TARGET (dist COMMAND ${CMAKE_MAKE_PROGRAM} package_source)

INSTALL (FILES
  include/cs_a2l.h
  include/cs_ccp_a2l.h
  include/cs_ccp_api.h
  include/cs_ccp_codes.h
  include/cs_ccp_decoder.h
  include/cs_ccp.h
  include/cs_dbc.h
  include/cs_defines.h
  include/cs_file_parser.h
  include/cs_isotp.h
  include/cs_isotp_decoder.h
  include/cs_message.h
  include/cs_parser.h
  include/cs_schedule.h
  include/cs_signal.h
  include/cs_socketcan.h
  include/cs_uds_defines.h
  include/cs_uds.h
  include/cs_uds_decoder.h
  include/cs_vbf.h
  include/cs_xcp_a2l.h
  include/cs_xcp_api.h
  include/cs_xcp_can.h
  include/cs_xcp_codes.h
  include/cs_xcp.h
  include/cs_lists.h
  include/cs_lin.h
  include/cs_lin_bit.h
  include/cs_process.h
  include/cs_debug.h
  include/cs_version.h
  DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/restbus"
  )

CONFIGURE_FILE (
    "${PROJECT_SOURCE_DIR}/config.h.in"
    "${PROJECT_BINARY_DIR}/config.h"
    )

