set( DEBIAN_PACKAGE_NAME "@DEBIAN_PACKAGE_LOWER_NAME@" )
set( DEBIAN_PACKAGE_SECTION "@DEBIAN_PACKAGE_SECTION@" )
set( DEBIAN_PACKAGE_PRIORITY "@DEBIAN_PACKAGE_PRIORITY@" )
set( DEBIAN_PACKAGE_VERSION "@DEBIAN_PACKAGE_VERSION@" )
set( DEBIAN_PACKAGE_RELEASE "@DEBIAN_PACKAGE_RELEASE@" )
set( DEBIAN_PACKAGE_DISTRIBUTIONS "@DEBIAN_PACKAGE_DISTRIBUTIONS@" )
set( DEBIAN_PACKAGE_URGENCY "@DEBIAN_PACKAGE_URGENCY@" )
set( DEBIAN_PACKAGE_MAINTAINER "@DEBIAN_PACKAGE_MAINTAINER@" )
set( DEBIAN_PACKAGE_CHANGELOG "@DEBIAN_PACKAGE_CHANGELOG@" )
set( DEBIAN_PACKAGE_DATE "@DEBIAN_PACKAGE_DATE@" )
set( DEBIAN_PACKAGE_BUILD_DEPENDS "@DEBIAN_PACKAGE_BUILD_DEPENDS@" )
set( DEBIAN_PACKAGE_BINARY_DEPENDS "@DEBIAN_PACKAGE_BINARY_DEPENDS@" )
set( DEBIAN_PACKAGE_ARCHITECTURE "@DEBIAN_PACKAGE_ARCHITECTURE@" )
set( DEBIAN_PACKAGE_DESCRIPTION_FILE "@DEBIAN_PACKAGE_DESCRIPTION_FILE@" )
set( DEBIAN_PACKAGE_DESCRIPTION "@DEBIAN_PACKAGE_DESCRIPTION@" )
set( DEBIAN_PACKAGE_TEMPLATE_DIR "@DEBIAN_PACKAGE_TEMPLATE_DIR@" )
set( DEBIAN_PACKAGE_COPYRIGHT_FILE "@DEBIAN_PACKAGE_COPYRIGHT_FILE@" )
set( DEBIAN_PACKAGE_COPYRIGHT_HEADER_LINES "@DEBIAN_PACKAGE_COPYRIGHT_HEADER_LINES@" )
set( DEBIAN_PACKAGE_COPYRIGHT_HOLDER "@DEBIAN_PACKAGE_COPYRIGHT_HOLDER@" )
set( DEBIAN_PACKAGE_COPYRIGHT_YEARS "@DEBIAN_PACKAGE_COPYRIGHT_YEARS@" )
set( DEBIAN_PACKAGE_COPYRIGHT_LICENSE "@DEBIAN_PACKAGE_COPYRIGHT_LICENSE@" )
set( DEBIAN_PACKAGE_SOURCE_FILENAME "@DEBIAN_PACKAGE_SOURCE_FILENAME@" )
set( DEBIAN_PACKAGE_BUILD_DIR "@DEBIAN_PACKAGE_BUILD_DIR@" )
set( DEBIAN_PACKAGE_RULES_CMAKE_OPTIONS "@DEBIAN_PACKAGE_RULES_CMAKE_OPTIONS@" )
set( DEBIAN_PACKAGE_CONTROL_SOURCE_EXTRA_LINES "@DEBIAN_PACKAGE_CONTROL_SOURCE_EXTRA_LINES@" )
set( DEBIAN_PACKAGE_CONTROL_BINARY_EXTRA_LINES "@DEBIAN_PACKAGE_CONTROL_BINARY_EXTRA_LINES@" )
set( DEBIAN_PACKAGE_NO_SIGN "@DEBIAN_PACKAGE_NO_SIGN@" )
set( DEBIAN_PACKAGE_SIGN_KEY "@DEBIAN_PACKAGE_SIGN_KEY@" )

set( BASE_DIR ${DEBIAN_PACKAGE_BUILD_DIR} )

#
# Find source package
#

file( GLOB PACKAGE_SOURCE_FILEPATH "${DEBIAN_PACKAGE_BUILD_DIR}/${DEBIAN_PACKAGE_SOURCE_FILENAME}.tar.*" )

if( NOT PACKAGE_SOURCE_FILEPATH OR
    NOT EXISTS ${PACKAGE_SOURCE_FILEPATH} )
    message( FATAL_ERROR "Could not find source package '${DEBIAN_PACKAGE_SOURCE_FILENAME}' in '${DEBIAN_PACKAGE_BUILD_DIR}'" )
endif()

#
# Copy source package to generation directory
#

if( PACKAGE_SOURCE_FILEPATH MATCHES ".*(\\.gz)$" )
    set( UNCOMPRESS_OPTION "z" )
elseif( PACKAGE_SOURCE_FILEPATH MATCHES ".*(\\.bz2)$" )
    set( UNCOMPRESS_OPTION "j" )
elseif( PACKAGE_SOURCE_FILEPATH MATCHES ".*(\\.xz)$" )
    set( UNCOMPRESS_OPTION "J" )
else()
    message( FATAL_ERROR "Cannot uncompress source package '${PACKAGE_SOURCE_FILEPATH}', format unknown" )
endif()

set( PACKAGE_SOURCE_EXTENSION ${CMAKE_MATCH_1} )

file( REMOVE_RECURSE "${BASE_DIR}/_DebianPackage" )
file( MAKE_DIRECTORY "${BASE_DIR}/_DebianPackage" )

set( DEBTOOLS_PACKAGE_FILENAME "${DEBIAN_PACKAGE_NAME}_${DEBIAN_PACKAGE_VERSION}.orig.tar${PACKAGE_SOURCE_EXTENSION}" )

file( COPY "${PACKAGE_SOURCE_FILEPATH}" DESTINATION "${BASE_DIR}/_DebianPackage/" )

file( RENAME "${BASE_DIR}/_DebianPackage/${DEBIAN_PACKAGE_SOURCE_FILENAME}.tar${PACKAGE_SOURCE_EXTENSION}" "${BASE_DIR}/_DebianPackage/${DEBTOOLS_PACKAGE_FILENAME}" )

#
# Uncompress source package
#

execute_process(
    COMMAND tar -${UNCOMPRESS_OPTION}xf "${BASE_DIR}/_DebianPackage/${DEBTOOLS_PACKAGE_FILENAME}" -C "${BASE_DIR}/_DebianPackage"
    RESULT_VARIABLE UNCOMPRESS_COMMAND_RES )

if( UNCOMPRESS_COMMAND_RES )
    message( FATAL_ERROR "Failed to uncompress source package '${DEBTOOLS_PACKAGE_FILENAME}'" )
endif()

#
# Copy or configure template debian files
#

if( DEBIAN_PACKAGE_DESCRIPTION_FILE )
    file( READ ${DEBIAN_PACKAGE_DESCRIPTION_FILE} DEBIAN_PACKAGE_DESCRIPTION )
endif()

set( DEBTOOLS_DIR "${BASE_DIR}/_DebianPackage/${DEBIAN_PACKAGE_SOURCE_FILENAME}" )

file( MAKE_DIRECTORY "${DEBTOOLS_DIR}/debian" )
file( MAKE_DIRECTORY "${DEBTOOLS_DIR}/debian/source" )

file( COPY "${DEBIAN_PACKAGE_TEMPLATE_DIR}/debian/compat"
           DESTINATION "${DEBTOOLS_DIR}/debian/" )

file( COPY "${DEBIAN_PACKAGE_TEMPLATE_DIR}/debian/source/format"
           DESTINATION "${DEBTOOLS_DIR}/debian/source/" )

configure_file( "${DEBIAN_PACKAGE_TEMPLATE_DIR}/debian/control.in"
                "${DEBTOOLS_DIR}/debian/control" )

configure_file( "${DEBIAN_PACKAGE_TEMPLATE_DIR}/debian/rules.in"
                "${DEBTOOLS_DIR}/debian/rules" )

configure_file( "${DEBIAN_PACKAGE_TEMPLATE_DIR}/debian/changelog.in"
                "${DEBTOOLS_DIR}/debian/changelog" )

if( DEBIAN_PACKAGE_COPYRIGHT_FILE )
    file( COPY "${DEBIAN_PACKAGE_COPYRIGHT_FILE}"
               DESTINATION "${DEBTOOLS_DIR}/debian/" )
    get_filename_component( COPYRIGHT_FILENAME "${DEBIAN_PACKAGE_COPYRIGHT_FILE}" NAME )
    file( RENAME "${DEBTOOLS_DIR}/debian/${COPYRIGHT_FILENAME}"
                 "${DEBTOOLS_DIR}/debian/copyright" )
else()
    configure_file( "${DEBIAN_PACKAGE_TEMPLATE_DIR}/debian/copyright.in"
                    "${DEBTOOLS_DIR}/debian/copyright" )
endif()

set( DEBUILD_OPTS "" )

if( NOT DEBIAN_PACKAGE_BINARY )
    set( DEBUILD_OPTS ${DEBUILD_OPTS} "-S" )
endif()

if( DEBIAN_PACKAGE_SIGN_KEY )
    set( DEBUILD_OPTS ${DEBUILD_OPTS} "-k${DEBIAN_PACKAGE_SIGN_KEY}" )
endif()

if( DEBIAN_PACKAGE_NO_SIGN )
    set( DEBUILD_OPTS ${DEBUILD_OPTS} "--no-sign" )
endif()

#
# Generate package
#

execute_process(
    COMMAND debuild -i^obj- ${DEBUILD_OPTS}
    WORKING_DIRECTORY "${DEBTOOLS_DIR}"
    RESULT_VARIABLE DEBUILD_COMMAND_RES )

if( DEBUILD_COMMAND_RES )
    message( FATAL_ERROR "Debian package genereration failed" )
endif()

