%hs_pkgconfdir %_ghclibdir/package.conf.d
%hs_setup      runghc Setup

%hs_configure  configure

# hs_package_register <NAME.pkg> <BUILDROOT/PKGCONFDIR/NAME.conf>
%hs_package_register() \
[ -s "%2" ] || echo '[]' >"%2"; \
ghc-pkg --package-conf="%2" --force register "%1"; \
%nil


# internal macro, cloned from rpm-build-python's py_package_deps_internal
%__hs_package_deps_internal() \
args="%{*}"; \
set $args ''; \
while test $# -gt 1; do \
  mod=${1%%,}; shift; \
  if test $# -gt 1; then \
    if test "x$1" = "x<" -o "x$1" = "x<=" -o "x$1" = "x=" -o "x$1" = "x>=" -o "x$1" = "x>"; then \
      verstr="$1 $2"; shift; shift; \
    fi; \
  fi; \
  echo -n "ghc(${mod}) $verstr "; \
  unset mod; unset verstr; \
done; \
%nil


# hs_package_dependencies <hspackage1> [[>= <hspackage1_version>] [<hspackage2> ...]]

%hs_package_dependencies() %(%{expand: %%__hs_package_deps_internal %{*}})

%hs_gen_filelist \
%_libexecdir/rpm-build-haskell/hs_gen_filelist.sh  %buildroot  %pkg_libdir  %_builddir/%{?buildsubdir}  %name  %version %hs_pkgconfdir/%f_pkg_name-%version.conf; \
%nil

%hs_build \
if runghc Setup makefile -f cabal-rpm.mk; \
then \
    %make_build -f cabal-rpm.mk || : ; \
fi; \
runghc Setup build; \
runghc Setup haddock || : ;\
runghc Setup register --gen-pkg-config=%f_pkg_name.pkg; \
%nil

%hs_configure2 \
if [ -f configure.ac -a ! -f configure ]; then autoreconf; fi; \
runghc Setup configure \\\
    --prefix=%_prefix \\\
    --libdir=%_libdir \\\
    --docdir=%_docdir/%hsc_namever-%f_pkg_name-%version \\\
    --libsubdir='$compiler/lib/$pkgid' \\\
    --docdir=%_docdir/%name-%version \\\
    --datadir=%_datadir \\\
    --datasubdir=%name-%version \\\
    --enable-split-objs \\\
    --enable-library-profiling \\\
    --enable-library-for-ghci \\\
    --disable-executable-profiling \\\
    --enable-shared \\\
    -O2 \\\
    --ghc %nil

%hs_install \
runghc Setup copy --destdir=%buildroot; \
if [ -f %f_pkg_name.pkg ]; then \
    install -m 755 %f_pkg_name.pkg %buildroot%pkg_libdir; \
    mkdir -p %buildroot%hs_pkgconfdir; \
    install -D -m 644 %f_pkg_name.pkg %buildroot%hs_pkgconfdir/%f_pkg_name-%version.conf; \
fi \
%nil
