%define pkg_name    http-client-tls
%define pkg_version 0.3.5.1
%define pkg_release 1%{?_dist_release}

%define ghc_version 8.2.2

Summary: Haskell libraries: %{pkg_name} and dependent libraries
Name:    libghc-%{pkg_name}
Version: %{pkg_version}
Release: %{pkg_release}

License: BSD3
Group:   Applications/Text
URL:     http://hackage.haskell.org

Source0: %{pkg_name}-%{version}.tar.gz
Source1: packages

# num=11; for pkg in `cat packages`; do echo "Source${num}: ${pkg}.tar.gz"; num=`expr ${num} + 1`; done
Source11: base64-bytestring-1.0.0.1.tar.gz
Source12: basement-0.0.4.tar.gz
Source13: blaze-builder-0.4.0.2.tar.gz
Source14: byteable-0.1.1.tar.gz
Source15: cereal-0.5.4.0.tar.gz
Source16: exceptions-0.8.3.tar.gz
Source17: hourglass-0.2.10.tar.gz
Source18: http-types-0.11.tar.gz
Source19: mime-types-0.1.0.7.tar.gz
Source20: pem-0.2.2.tar.gz
Source21: foundation-0.0.17.tar.gz
Source22: streaming-commons-0.1.18.tar.gz
Source23: cookie-0.4.3.tar.gz
Source24: socks-0.5.6.tar.gz
Source25: memory-0.14.11.tar.gz
Source26: http-client-0.5.7.1.tar.gz
Source27: cryptonite-0.24.tar.gz
Source28: asn1-types-0.3.2.tar.gz
Source29: asn1-encoding-0.9.5.tar.gz
Source30: asn1-parse-0.9.4.tar.gz
Source31: x509-1.7.2.tar.gz
Source32: x509-store-1.6.5.tar.gz
Source33: x509-validation-1.6.9.tar.gz
Source34: x509-system-1.6.6.tar.gz
Source35: tls-1.4.0.tar.gz
Source36: connection-0.2.8.tar.gz

# to fix wrong dependencies in cabal files
# do not use '-'
%define exceptions_version 0.8.3
Source51: exceptions.cabal

BuildRoot: %{_tmppath}/%{name}-%{version}-root

BuildRequires: ghc haskell-platform
BuildRequires: libffi-devel gmp-devel zlib-devel
BuildRequires: libghc-dlist
BuildRequires: libghc-data-default
BuildRequires: libghc-aeson

BuildRequires: docbook-utils docbook-utils-pdf docbook-style-xsl

Requires: ghc haskell-platform
Requires: libghc-dlist
Requires: libghc-data-default
Requires: libghc-aeson


Vendor: Project Vine
Distribution: Vine Linux
Packager: ara_t


%description
Haskell libraries: %{pkg_name} and dependent libraries


%prep
%{__rm} -rf ${RPM_BUILD_ROOT}
%{__rm} -rf %{_builddir}/package.conf
%{__rm} -rf ${HOME}/.ghc
%setup -q -n %{pkg_name}-%{version}

%build
# Initialise the package db
ghc-pkg init %{_builddir}/package.conf

PKG_CONF_DIR=${RPM_BUILD_ROOT}%{_libdir}/ghc-%{ghc_version}/package.conf.d
%{__mkdir_p} ${PKG_CONF_DIR}

# install dependent packages
cd %{_builddir}
for pkg in `cat %{SOURCE1}`; do
  %{__tar} xzf %{_sourcedir}/${pkg}.tar.gz
  cd ${pkg}
  # fix wrong dependencies
  case ${pkg} in
      "exceptions-%{exceptions_version}") %{__cp} %{SOURCE51} . ;;
      *) ;;
  esac

  cabal configure \
    --prefix=%{_prefix} \
    --libdir=%{_libdir}/ghc-lib/%{name}/${pkg} \
    --libsubdir= \
    --datadir=%{_datadir}/%{name}/${pkg} \
    --datasubdir= \
    --docdir=%{_docdir}/%{name}/${pkg}

  cabal build
  cabal haddock || : # aviod Error
  cabal copy --destdir=${RPM_BUILD_ROOT}
  cabal register --inplace
  cabal register --gen-pkg-config=${PKG_CONF_DIR}/%{name}_${pkg}.conf
  cd ..
done

# build
cd %{_builddir}/%{pkg_name}-%{version}
cabal configure \
    --prefix=%{_prefix} \
    --libdir=%{_libdir}/ghc-lib/%{name}/%{pkg_name}-%{version} \
    --libsubdir= \
    --datadir=%{_datadir}/%{name}/%{pkg_name}-%{version} \
    --datasubdir= \
    --docdir=%{_docdir}/%{name}/%{pkg_name}-%{version}

cabal build
cabal haddock
cabal copy --destdir=${RPM_BUILD_ROOT}
cabal register --inplace
cabal register --gen-pkg-config=${PKG_CONF_DIR}/%{pkg_name}-%{version}.conf

%install
cabal copy --destdir=${RPM_BUILD_ROOT}

%{__cp} LICENSE \
        ${RPM_BUILD_ROOT}%{_docdir}/%{name}/%{pkg_name}-%{version}

%clean
%{__rm} -rf ${RPM_BUILD_ROOT}

%post
ghc-pkg recache

%postun
ghc-pkg recache


%files
%defattr(-, root, root)
%{_libdir}/ghc-%{ghc_version}/
%{_libdir}/ghc-lib/
%{_datadir}/


%changelog
* Tue Jan 09 2018 Toshiaki Ara <ara_t@384.jp> 0.3.5.1-1
- update to 0.3.5.1
- build using ghc-8.2.2

* Tue Feb 21 2017 Toshiaki Ara <ara_t@384.jp> 0.3.3.1-1
- update to 0.3.3.1
- build using ghc-8.0.2

* Fri Feb 03 2017 Toshiaki Ara <ara_t@384.jp> 0.2.4.1-1
- update to 0.2.4.1
- build using ghc-8.0.1

* Wed May 04 2016 Toshiaki Ara <ara_t@384.jp> 0.2.2-3
- rebuilt

* Tue Apr 12 2016 Toshiaki Ara <ara_t@384.jp> 0.2.2-2
- correct SPEC file

* Tue Mar 01 2016 Toshiaki Ara <ara_t@384.jp> 0.2.2-1
- new package