#******************************************************************************
#
# Copyright:   2018-2020 Paul Obermeier (obermeier@poSoft.de)
#
#              See the file "Mawt_License.txt" for information on
#              usage and redistribution of this file, and for a
#              DISCLAIMER OF ALL WARRANTIES.
#
# Description: Makefile for the Movie Automation With Tcl (MAWT) package.
#
#******************************************************************************

# - Try to find ffmpeg libraries (libavcodec, libavformat and libavutil)
# Once done this will define
#
# FFMPEG_FOUND - system has ffmpeg or libav
# FFMPEG_INCLUDE_DIR - the ffmpeg include directory
# FFMPEG_LIBRARIES - Link these to use ffmpeg
# FFMPEG_LIBAVCODEC
# FFMPEG_LIBAVFORMAT
# FFMPEG_LIBAVUTIL
#
# Copyright (c) 2008 Andreas Schneider <mail@cynapses.org>
# Modified for other libraries by Lasse Kärkkäinen <tronic>
# Modified for Hedgewars by Stepik777
#
# Redistribution and use is allowed according to the terms of the New
# BSD license.
#

IF (FFMPEG_LIBRARIES AND FFMPEG_INCLUDE_DIR)
    # in cache already
    SET (FFMPEG_FOUND TRUE)
ELSE()
    # use pkg-config to get the directories and then use these values
    # in the FIND_PATH() and FIND_LIBRARY() calls
    FIND_PACKAGE (PkgConfig)
    IF (PKG_CONFIG_FOUND)
        pkg_check_modules(_FFMPEG_AVCODEC  libavcodec)
        pkg_check_modules(_FFMPEG_AVFORMAT libavformat)
        pkg_check_modules(_FFMPEG_AVUTIL   libavutil)
        pkg_check_modules(_FFMPEG_SWSCALE  libswscale)
    ENDIF ()

    FIND_PATH (FFMPEG_AVCODEC_INCLUDE_DIR
        NAMES libavcodec/avcodec.h
        PATHS ${_FFMPEG_AVCODEC_INCLUDE_DIRS} /usr/include /usr/local/include /opt/local/include /sw/include
        PATH_SUFFIXES ffmpeg libav
    )

    FIND_LIBRARY (FFMPEG_LIBAVCODEC
        NAMES avcodec
        PATHS ${_FFMPEG_AVCODEC_LIBRARY_DIRS} /usr/lib /usr/local/lib /opt/local/lib /sw/lib
    )

    FIND_LIBRARY (FFMPEG_LIBAVFORMAT
        NAMES avformat
        PATHS ${_FFMPEG_AVFORMAT_LIBRARY_DIRS} /usr/lib /usr/local/lib /opt/local/lib /sw/lib
    )

    FIND_LIBRARY (FFMPEG_LIBAVUTIL
        NAMES avutil
        PATHS ${_FFMPEG_AVUTIL_LIBRARY_DIRS} /usr/lib /usr/local/lib /opt/local/lib /sw/lib
    )

    FIND_LIBRARY (FFMPEG_LIBSWSCALE
        NAMES swscale
        PATHS ${_FFMPEG_SWSCALE_LIBRARY_DIRS} /usr/lib /usr/local/lib /opt/local/lib /sw/lib
    )

    IF (FFMPEG_LIBAVCODEC AND FFMPEG_LIBAVFORMAT)
        SET (FFMPEG_FOUND TRUE)
    ENDIF()

    if (FFMPEG_FOUND)
        SET (FFMPEG_INCLUDE_DIR ${FFMPEG_AVCODEC_INCLUDE_DIR})

        SET (FFMPEG_LIBRARIES
            ${FFMPEG_LIBAVCODEC}
            ${FFMPEG_LIBAVFORMAT}
            ${FFMPEG_LIBAVUTIL}
            ${FFMPEG_LIBSWSCALE}
        )
    ENDIF ()

    IF (FFMPEG_FOUND)
        IF (NOT FFMPEG_FIND_QUIETLY)
            MESSAGE (STATUS "Found FFMPEG or Libav: ${FFMPEG_LIBRARIES}, ${FFMPEG_INCLUDE_DIR}")
        ENDIF ()
    ELSE ()
        IF (FFMPEG_FIND_REQUIRED)
            MESSAGE (FATAL_ERROR "Could not find libavcodec or libavformat or libavutil")
        ENDIF ()
    ENDIF ()
ENDIF ()
