##
##  minizinc.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 information
Name:         minizinc
Summary:      G12 MiniZinc Constraint Solving
URL:          http://www.minizinc.org/
Vendor:       Optimisation Research Group NICTA
Packager:     OpenPKG Project
Distribution: OpenPKG Community
Class:        EVAL
Group:        Language
License:      BSD
Version:      2.6.2
Release:      20220323

#   list of sources
Source0:      https://github.com/MiniZinc/libminizinc/archive/%{version}.tar.gz
Patch0:       minizinc.patch

#   build information
BuildPreReq:  OpenPKG, openpkg >= 20160101, gcc, gcc::with_cxx = yes, flex, bison, cmake
PreReq:       OpenPKG, openpkg >= 20160101
BuildPreReq:  mpfr
PreReq:       mpfr
PreReq:       gecode
PreReq:       chuffed

%description
     MiniZinc is a medium-level constraint modelling language. It is
     high-level enough to express most constraint problems easily, but
     low-level enough that it can be mapped onto existing solvers easily
     and consistently. It is a subset of the higher-level language
     Zinc. We hope it will be adopted as a standard by the Constraint
     Programming community. FlatZinc is a low-level solver input
     language that is the target language for MiniZinc. It is designed
     to be easy to translate into the form required by a solver like
     Gecode.

%track
    prog minizinc = {
        version   = %{version}
        url       = https://github.com/MiniZinc/libminizinc/releases
        regex     = (\d+\.\d+\.\d+)\.tar\.gz
    }

%prep
    %setup -q -n libminizinc-%{version}
    %patch -p0

%build
    #   configure program
    mkdir build
    cd build
    export HOME=`pwd`
    cmake \
        -DCMAKE_BUILD_TYPE="Release" \
        -DCMAKE_INSTALL_PREFIX="%{l_prefix}" \
        -DCMAKE_C_COMPILER="%{l_cc}" \
        -DCMAKE_C_FLAGS="%{l_cflags} %{l_cppflags}" \
        -DCMAKE_CXX_COMPILER="%{l_cxx}" \
        -DCMAKE_CXX_FLAGS="%{l_cxxflags} %{l_cppflags}" \
        -DCMAKE_EXE_LINKER_FLAGS="%{l_ldflags} -lmpfr -lgmp" \
        -DCMAKE_VERBOSE_MAKEFILE=ON \
        -DUSE_GECODE=OFF \
        ..

    #   build program
    %{l_make} %{l_mflags -O}

%install
    #   install program
    (   cd build
        export HOME=`pwd`
        %{l_make} %{l_mflags} install DESTDIR="$RPM_BUILD_ROOT"
    ) || exit $?

    #   strip down installation
    find $RPM_BUILD_ROOT%{l_prefix} -name "*.orig" -print | xargs rm -f
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/doc
    rm -rf $RPM_BUILD_ROOT%{l_prefix}/share/examples

    #   provide wrapper scripts for executables
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/libexec/minizinc
    for prog in minizinc mzn2doc; do
        mv  $RPM_BUILD_ROOT%{l_prefix}/bin/$prog \
            $RPM_BUILD_ROOT%{l_prefix}/libexec/minizinc/$prog
        (   echo "#!/bin/sh"
            echo "MZN_STDLIB_DIR=\"%{l_prefix}/share/minizinc\""
            echo "export MZN_STDLIB_DIR"
            if [ ".$prog" = .minizinc ]; then
                echo "FLATZINC_CMD=\"%{l_prefix}/bin/flatzinc\""
                echo "export FLATZINC_CMD"
            fi
            echo "exec \"%{l_prefix}/libexec/minizinc/$prog\" \${1+\"\$@\"}"
        ) >$prog
        %{l_shtool} install -c -m 755 \
            $prog $RPM_BUILD_ROOT%{l_prefix}/bin/
    done
    (   echo "#!/bin/sh"
        echo "exec \"%{l_prefix}/bin/minizinc -c \${1+\"\$@\"}"
    ) >mzn2fzn
    %{l_shtool} install -c -m 755 mzn2fzn $RPM_BUILD_ROOT%{l_prefix}/bin/
    (   echo "#!/bin/sh"
        echo "exec \"%{l_prefix}/bin/minizinc --ozn-file \${1+\"\$@\"}"
    ) >solns2out
    %{l_shtool} install -c -m 755 mzn2fzn $RPM_BUILD_ROOT%{l_prefix}/bin/

    #   provide solver configurations
    %{l_shtool} mkdir -f -p -m 755 \
        $RPM_BUILD_ROOT%{l_prefix}/share/minizinc/solvers
    (   echo "{"
        echo "    \"name\":       \"Gecode Solver\","
        echo "    \"version\":    \"1.0\","
        echo "    \"id\":         \"gecode\","
        echo "    \"mznlib\":     \"%{l_prefix}/share/gecode/mznlib\","
        echo "    \"executable\": \"%{l_prefix}/bin/fzn-gecode\""
        echo "}"
    ) >fzn-gecode.msc
    %{l_shtool} install -c -m 755 \
        fzn-gecode.msc $RPM_BUILD_ROOT%{l_prefix}/share/minizinc/solvers/
    (   echo "{"
        echo "    \"name\":       \"Chuffed Solver\","
        echo "    \"version\":    \"1.0\","
        echo "    \"id\":         \"chuffed\","
        echo "    \"mznlib\":     \"%{l_prefix}/share/chuffed/mznlib\","
        echo "    \"executable\": \"%{l_prefix}/bin/fzn-chuffed\""
        echo "}"
    ) >fzn-chuffed.msc
    %{l_shtool} install -c -m 755 \
        fzn-chuffed.msc $RPM_BUILD_ROOT%{l_prefix}/share/minizinc/solvers/

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

%files -f files

%clean

