FEATURE add building package
diff --git a/CMakeLists.txt b/CMakeLists.txt
index d8d9936..2180921 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -23,6 +23,7 @@
 
 set(CMAKE_C_FLAGS         "${CMAKE_C_FLAGS} -Wall -Wextra -fvisibility=hidden")
 set(CMAKE_C_FLAGS_RELEASE "-O2 -DNDEBUG")
+set(CMAKE_C_FLAGS_PACKAGE "-g -O2 -DNDEBUG")
 set(CMAKE_C_FLAGS_DEBUG   "-g -O0")
 
 # set version
@@ -46,6 +47,70 @@
     set(ENABLE_DNSSEC OFF)
 endif()
 
+# package options
+find_program (DEB_BUILDER NAMES debuild)
+find_program (RPM_BUILDER NAMES rpmbuild)
+
+if (NOT DEFINED ENV{TRAVIS_BRANCH})
+    execute_process(COMMAND "git" "rev-parse" "--abbrev-ref" "HEAD"
+                    OUTPUT_VARIABLE GIT_BRANCH
+                    OUTPUT_STRIP_TRAILING_WHITESPACE
+                    ERROR_QUIET
+                   )
+    if (NOT GIT_BRANCH)
+        set(ENV{TRAVIS_BRANCH} "master")
+    else()
+        if (GIT_BRANCH MATCHES "master|devel")
+            set(ENV{TRAVIS_BRANCH} ${GIT_BRANCH})
+        else()
+            set(ENV{TRAVIS_BRANCH} "master")
+        endif()
+    endif()
+    set(GIT_BRANCH $ENV{TRAVIS_BRANCH})
+endif()
+
+if ($ENV{TRAVIS_BRANCH} STREQUAL "master")
+    set(PACKAGE_NAME "libnetconf2")
+    set(BRANCH "master")
+    set(BUILD_TYPE "Package")
+    set(CONFLICT_PACKAGE_NAME "libnetconf2-experimental")
+    set(COMPAT_PACKAGES "")
+else ()
+    set(PACKAGE_NAME "libnetconf2-experimental")
+    set(BRANCH "devel")
+    set(BUILD_TYPE "debug")
+    set(CONFLICT_PACKAGE_NAME "libnetconf2")
+    set(COMPAT_PACKAGES "-experimental")
+endif()
+# change version in config files
+configure_file(${PROJECT_SOURCE_DIR}/packages/libnetconf2.spec.in ${PROJECT_BINARY_DIR}/build-packages/libnetconf2.spec)
+configure_file(${PROJECT_SOURCE_DIR}/packages/libnetconf2.dsc.in ${PROJECT_BINARY_DIR}/build-packages/libnetconf2.dsc)
+configure_file(${PROJECT_SOURCE_DIR}/packages/debian.control.in ${PROJECT_BINARY_DIR}/build-packages/debian.control @ONLY)
+configure_file(${PROJECT_SOURCE_DIR}/packages/debian.rules.in ${PROJECT_BINARY_DIR}/build-packages/debian.rules)
+
+if (NOT DEB_BUILDER)
+    message(WARNING "Missing tools (devscripts, debhelper package) for building deb package.\nYou won't be able to generate deb package from source code.\nCompiling libnetconf2 should still works fine.")
+else ()
+    # target for local build deb package
+    add_custom_target(build-deb
+                      WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
+                      COMMAND build-packages/local-deb.sh
+    )
+    configure_file(${PROJECT_SOURCE_DIR}/packages/local-deb.sh.in ${PROJECT_BINARY_DIR}/build-packages/local-deb.sh @ONLY)
+endif()
+
+if (NOT RPM_BUILDER)
+    message(WARNING "Missing tools (rpm package) for building rpm package. \nYou won't be able to generate rpm package from source code.\nCompiling libnetconf2 should still works fine.")
+else ()
+    # target for local build rpm package
+    string(REPLACE ${PROJECT_SOURCE_DIR} "." EXCLUDE_BUILD_DIR ${PROJECT_BINARY_DIR})
+    add_custom_target(build-rpm
+                      WORKING_DIRECTORY ${PROJECT_BINARY_DIR}
+                      COMMAND build-packages/local-rpm.sh
+    )
+    configure_file(${PROJECT_SOURCE_DIR}/packages/local-rpm.sh.in ${PROJECT_BINARY_DIR}/build-packages/local-rpm.sh @ONLY)
+endif()
+
 # source files
 set(libsrc
     src/io.c
@@ -86,7 +151,7 @@
 add_library(netconf2 SHARED ${libsrc})
 set_target_properties(netconf2 PROPERTIES VERSION ${LIBNETCONF2_VERSION} SOVERSION ${LIBNETCONF2_SOVERSION})
 
-if(CMAKE_BUILD_TYPE STREQUAL debug)
+if((CMAKE_BUILD_TYPE STREQUAL debug) OR (CMAKE_BUILD_TYPE STREQUAL Package))
     option(ENABLE_BUILD_TESTS "Build tests" ON)
     option(ENABLE_VALGRIND_TESTS "Build tests with valgrind" ON)
 else()