# - Find PNGwriter library,
#     a C++ library for creating PNG images
#     https://github.com/pngwriter/pngwriter
#
# Use this module by invoking find_package with the form:
#   find_package(PNGwriter
#     [version] [EXACT]     # Minimum or EXACT version, e.g. 0.5.5
#     [REQUIRED]            # Fail with an error if PNGwriter or a required
#                           #   component is not found
#     [QUIET]               # Do not warn if this module was not found
#     [COMPONENTS <...>]    # Compiled in components: not implemented
#   )
#
# To provide a hint to this module where to find the PNGwriter installation,
# set the PNGWRITER_ROOT environment variable.
#
# This module requires a valid installation of libpng (1.2.9+) which in turn
# requires ZLib.
#
# Set the following CMake variables BEFORE calling find_packages to
# change the behavior of this module:
#   PNGwriter_USE_STATIC_LIBS - Set to ON to prefer linking to the static
#                               library and its static dependencies. Note that it
#                               can fall back to shared libraries. Default: OFF
#
# This module will define the following variables:
#   PNGwriter_INCLUDE_DIRS    - Include directories for the PNGwriter headers.
#   PNGwriter_LIBRARIES       - PNGwriter libraries.
#   PNGwriter_FOUND           - TRUE if FindPNGwriter found a working install
#   PNGwriter_VERSION         - Version in format Major.Minor.Patch
#   PNGwriter_HAS_FREETYPE    - Does this install require a freetype install?
#   PNGwriter_DEFINITIONS     - Compiler definitions you should add with
#                               add_definitions(${PNGwriter_DEFINITIONS})
#


###############################################################################
# Copyright 2014-2015 Axel Huebl
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
# SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
# RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
# NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE
# USE OR PERFORMANCE OF THIS SOFTWARE.
###############################################################################


###############################################################################
# Required cmake version
###############################################################################

cmake_minimum_required(VERSION 2.8.11)


###############################################################################
# PNGwriter
###############################################################################

# we start by assuming we found PNGwriter and falsify it if some
# dependencies are missing (or if we did not find PNGwriter at all)
set(PNGwriter_FOUND TRUE)

# find PNGwriter install ######################################################
#
find_path(PNGwriter_ROOT_DIR
    NAMES include/pngwriter.h
    PATHS ENV PNGWRITER_ROOT
    DOC "PNGwriter ROOT location")

if(NOT PNGwriter_ROOT_DIR)
    set(PNGwriter_FOUND FALSE)
endif(NOT PNGwriter_ROOT_DIR)

# option: prefer static libs ##################################################
#
if(PNGwriter_USE_STATIC_LIBS)
    # carefully: we have to restore the original path in the end
    set(_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
    set(CMAKE_FIND_LIBRARY_SUFFIXES .a .so)
endif()

# find libpng install #########################################################
#
find_package(PNG 1.2.9)

if(NOT PNG_FOUND)
    set(PNGwriter_FOUND FALSE)
endif(NOT PNG_FOUND)


if(PNGwriter_FOUND)
    # PNGwriter headers #######################################################
    #
    list(APPEND PNGwriter_INCLUDE_DIRS ${PNGwriter_ROOT_DIR}/include)
    list(APPEND PNGwriter_INCLUDE_DIRS ${PNG_INCLUDE_DIRS})

    # PNGwriter libraries #####################################################
    #
    find_library(PNGwriter_LIBRARIES
        NAMES pngwriter
        PATHS ${PNGwriter_ROOT_DIR}/lib)

    #  libpng
    list(APPEND PNGwriter_LIBRARIES ${PNG_LIBRARIES})

    # PNGwriter definitions ###################################################
    #
    list(APPEND PNGwriter_DEFINITIONS ${PNG_DEFINITIONS})

    #   freetype support enabled?
    #   (assumes: environment did not change since install)
    include(FindFreetype)
    if(FREETYPE_FOUND)
        list(APPEND PNGwriter_INCLUDE_DIRS ${FREETYPE_INCLUDE_DIRS})
        list(APPEND PNGwriter_LIBRARIES ${FREETYPE_LIBRARIES})

    else(FREETYPE_FOUND)
        # this flag is important for pngwriter headers, see
        # https://github.com/pngwriter/pngwriter/issues/7
        list(APPEND PNGwriter_DEFINITIONS "-DNO_FREETYPE")
    endif(FREETYPE_FOUND)

    # version string ##########################################################
    #
    file(STRINGS "${PNGwriter_ROOT_DIR}/include/pngwriter.h"
         PNGwriter_VERSION_MAJOR_HPP REGEX "#define PNGWRITER_VERSION_MAJOR ")
    file(STRINGS "${PNGwriter_ROOT_DIR}/include/pngwriter.h"
         PNGwriter_VERSION_MINOR_HPP REGEX "#define PNGWRITER_VERSION_MINOR ")
    file(STRINGS "${PNGwriter_ROOT_DIR}/include/pngwriter.h"
         PNGwriter_VERSION_PATCH_HPP REGEX "#define PNGWRITER_VERSION_PATCH ")
    if(PNGwriter_VERSION_MAJOR_HPP)
        string(REGEX MATCH "([0-9]+)" PNGwriter_VERSION_MAJOR
                                    ${PNGwriter_VERSION_MAJOR_HPP})
        string(REGEX MATCH "([0-9]+)" PNGwriter_VERSION_MINOR
                                    ${PNGwriter_VERSION_MINOR_HPP})
        string(REGEX MATCH "([0-9]+)" PNGwriter_VERSION_PATCH
                                    ${PNGwriter_VERSION_PATCH_HPP})

        set(PNGwriter_VERSION "${PNGwriter_VERSION_MAJOR}.${PNGwriter_VERSION_MINOR}.${PNGwriter_VERSION_PATCH}")
        unset(PNGwriter_VERSION_MAJOR)
        unset(PNGwriter_VERSION_MINOR)
        unset(PNGwriter_VERSION_PATCH)
    else()
        # fallback for versions before 0.5.5: 0.XY == 0.X.Y
        file(STRINGS "${PNGwriter_ROOT_DIR}/include/pngwriter.h"
               PNGwriter_VERSION_DEFINE
               REGEX "^[ \t]*#[ \t]*define[ \t]+PNGWRITER_VERSION[ \t]+")
        if("${PNGwriter_VERSION_DEFINE}" MATCHES
            "PNGWRITER_VERSION[ \t]+(0\\.[0-9])([0-9]).*")
            set(PNGwriter_VERSION "${CMAKE_MATCH_1}.${CMAKE_MATCH_2}")
        endif()
        unset(PNGwriter_VERSION_DEFINE)
    endif()

    unset(PNGwriter_VERSION_MAJOR_HPP)
    unset(PNGwriter_VERSION_MINOR_HPP)
    unset(PNGwriter_VERSION_PATCH_HPP)

else(PNGwriter_FOUND)
    message(STATUS "Can NOT find PNGwriter - set PNGWRITER_ROOT")
endif(PNGwriter_FOUND)


# restore defaults of library suffixes ####################################
#
if(PNGwriter_USE_STATIC_LIBS)
    set(CMAKE_FIND_LIBRARY_SUFFIXES ${_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
endif()


# unset checked variables if not found ########################################
#
if(NOT PNGwriter_FOUND)
    unset(PNGwriter_INCLUDE_DIRS)
    unset(PNGwriter_LIBRARIES)
    unset(PNGwriter_HAS_FREETYPE)
endif(NOT PNGwriter_FOUND)


###############################################################################
# FindPackage Options
###############################################################################

# handles the REQUIRED, QUIET and version-related arguments for find_package
include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(PNGwriter
    FOUND_VAR PNGwriter_FOUND
    REQUIRED_VARS PNGwriter_LIBRARIES PNGwriter_INCLUDE_DIRS
    VERSION_VAR PNGwriter_VERSION
)
