##
##  node-opencv.spec -- OpenPKG RPM Package Specification
##  Copyright (c) 2000-2025 OpenPKG Project <http://openpkg.org/>
##
##  Permission to use, copy, modify, and 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.
##
##  THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
##  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
##  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
##  IN NO EVENT SHALL THE AUTHORS AND COPYRIGHT HOLDERS AND THEIR
##  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
##  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
##  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
##  USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
##  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
##  OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
##  OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
##  SUCH DAMAGE.
##

#   package version
%define       V_openpkg            20180414
%define       V_opencv             6.0.0
%define       V_opencv_snap        20180414
%define       V_opencv4nodejs      4.2.0
%define       V_opencv4nodejs_snap 20180414

#   package information
Name:         node-opencv
Summary:      Node Bindings to OpenCV
URL:          https://github.com/peterbraden/node-opencv
Vendor:       Peter Braden, Vincent Mühler
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Algorithm
License:      MIT
Version:      %{V_openpkg}
Release:      20180414

#   list of sources
Source0:      http://download.openpkg.org/components/versioned/node-opencv/node-opencv-%{V_opencv}-%{V_opencv_snap}.tar.xz
Source1:      http://download.openpkg.org/components/versioned/node-opencv/opencv4nodejs-%{V_opencv4nodejs}-%{V_opencv4nodejs_snap}.tar.xz
Patch0:       node-opencv.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc, gcc::with_cxx = yes, npm, node-openpkg
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  node, opencv
PreReq:       node, opencv

%description
    This is a Node.js bindings package for OpenCV.

%track
    prog node-opencv:opencv-release = {
        version   = %{V_opencv}
        url       = https://github.com/peterbraden/node-opencv/releases
        regex     = v(\d+\.\d+\.\d+)\.tar\.gz
    }
    prog node-opencv:opencv-snapshot = {
        version   = %{V_opencv}-%{V_opencv_snap}
        url       = http://download.openpkg.org/components/versioned/node-opencv/
        regex     = node-opencv-(__VER__)\.tar\.xz
    }
    prog node-opencv:opencv4nodejs-release = {
        version   = %{V_opencv4nodejs}
        url       = https://www.npmjs.com/package/opencv4nodejs
        regex     = "latest":"(\d+\.\d+\.\d+)"
    }
    prog node-opencv:sopencv4nodejs-napshot = {
        version   = %{V_opencv4nodejs}-%{V_opencv4nodejs_snap}
        url       = http://download.openpkg.org/components/versioned/node-opencv/
        regex     = opencv4nodejs-(__VER__)\.tar\.xz
    }

%prep
    %setup -q -c
    %setup -q -T -D -a 1
    %patch -p0

%build
    #   build node-opencv module
    (   cd node-opencv
        npm run install
        rm -f [A-Z]*
        rm -f appveyor.yml binding.gyp* index.html js_code_style.xml native_code_style.xml publish-binaries.md
        rm -rf examples smoke src test utils vagrant
        find node_modules -name "*.orig" -type f -print | xargs rm -f
        %{l_prefix}/bin/node-openpkg squeeze
    ) || exit $?

    #   build opencv4nodejs module
    (   cd opencv4nodejs
        %{l_shtool} subst \
            -e 's;<\(opencv2/.*\)>;"\1";g' \
            `find . -name "*.h" -print` \
            `find . -name "*.c" -print`
        OPENCV4NODEJS_DISABLE_AUTOBUILD=1 \
        OPENCV_INCLUDE_DIR=%{l_prefix}/include \
        OPENCV_LIB_DIR=%{l_prefix}/lib \
        npm run install
        %{l_prefix}/bin/node-openpkg squeeze
    ) || exit $?

%install
    #   create installation hierarchy
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/lib/node/pkg/opencv \
        $RPM_BUILD_ROOT%{l_prefix}/lib/node/pkg/opencv4nodejs

    #   install modules
    cp -rp node-opencv/* \
        $RPM_BUILD_ROOT%{l_prefix}/lib/node/pkg/opencv/
    cp -rp opencv4nodejs/* \
        $RPM_BUILD_ROOT%{l_prefix}/lib/node/pkg/opencv4nodejs/

    #   determine installation files
    %{l_rpmtool} files -v -ofiles -r$RPM_BUILD_ROOT %{l_files_std}

%files -f files

%clean

