# Emulating the symlinks for some of the common (unversioned) names
# -- for those ones that are used here internally in the macros.
#
# This will allow to build an SRPM even if it doesn't build-require
# ghc or ghc%%{ghc_version}-common. It's the case in some legacy SRPMs.
#
# Although a cleaner way would be for the SRPM to simply build-require
# either ghc for building with most recent GHC or ghc%{ghc_version}-common
# for building with a fixed legacy version of GHC.
# And to invoke the unversioned commands if it needs to.
# (That's why these macros are "internal", with %%__.)
#
# That's not very dirty, because we need to know %%ghc_version anyway
# in order to put the build Haskell modules into the correct place
# (i.e., %%_ghclibdir, which is used below).
%__runghc		runghc-%ghc_version
%__ghc			ghc-%ghc_version

%_ghclibdir	%_libdir/ghc-%ghc_version

%ghclibdir %_ghclibdir/lib
%ghc_pkgconfdir %ghclibdir/package.conf.d
%__ghc_gen_filelist %_libexecdir/rpm-build-haskell/ghc_gen_filelist.sh

%ghc_smp_mflags %{?_smp_mflags}

# If there is no Setup.hs file, we create it manually
%__ghc_init_setup %{expand:
    [ -f "Setup.hs" ] || printf "import Distribution.Simple\\\nmain = defaultMain\\\n" > Setup.hs

    %__ghc --make -package Cabal -no-user-package-db Setup
}

%cabal %{expand:
    if [ ! -x Setup ]; then
        %__ghc_init_setup
    fi
    ./Setup \\\
}

%cabal_configure %{expand:
%cabal  %{shrink: configure
        --prefix=%_prefix
        --libdir=%_libdir
        --libsubdir='$compiler/lib/$arch-$os-$compiler/$pkgid'
        --dynlibdir='%_libdir/$compiler/lib/$arch-$os-$compiler'
        --docdir='%_docdir/$compiler/libraries/$pkgid'
        --datadir=%_datadir
        --datasubdir='$compiler/$pkgid'
        %{?_with_tests: --enable-tests}
        --enable-split-sections
        --enable-shared
        -O2
        --ghc
        --with-ghc=%__ghc
        --disable-executable-stripping
        --disable-library-stripping
        --enable-debug-info=3
        %{?cabal_configure_options}
        }
}

%cabal_install %{expand:
    %cabal copy --destdir=%buildroot
}

%ghc_install_pkgconfig %{expand:
    PKGFILE=$(find %_builddir -name "*.conf" | head -n 1);
    if [ -e "$PKGFILE" ]; then
        PKGID="$(basename $PKGFILE | sed 's|.conf||')";
        printf "$PKGID" > %name.pkgid
        PKG_LIBDIR=$(find "%buildroot%ghclibdir" -name "$PKGID" -type d | sed "s|%buildroot||");

        mkdir -p "%buildroot$PKG_LIBDIR";
        mkdir -p "%buildroot%ghc_pkgconfdir";

        if [ -f "$PKGFILE" ]; then
            install -m 644 -t "%buildroot$PKG_LIBDIR" "$PKGFILE";
            install -m 644 -t "%buildroot%ghc_pkgconfdir" "$PKGFILE";
        elif [ -d "$PKGFILE" ]; then
            for i in $(ls $PKGFILE); do
                sub=$(echo $i | sed -e "s/^[0-9]\\+-//");
                install -m 644 "$PKGFILE/$i" "%buildroot$PKG_LIBDIR/$sub.conf";
                install -m 644 "$PKGFILE/$i" "%buildroot%ghc_pkgconfdir/$sub.conf";
            done;
        else
            echo "Could not find $PKGFILE, aborting install";
            exit 1;
        fi;
        rename ".conf" ".pkg" "%buildroot$PKG_LIBDIR"/*;
    fi;
}

%ghc_delete_rpaths %{expand:
    find %buildroot -type f | xargs -n 1 file | grep 'dynamically linked' | cut -f1 -d: \\\
                            | xargs -n 1 chrpath -d
}

%ghc_lib_build %{expand:
    %cabal_configure
    %cabal build %{?ghc_smp_mflags}
    %if_with docs
        %cabal haddock --html --hyperlink-source --hoogle %{?cabal_haddock_options}
    %endif
    %cabal register --gen-pkg-config
}

%ghc_lib_install %{expand:
    %cabal_install
    %ghc_install_pkgconfig
    %ghc_delete_rpaths
}

%ghc_bin_build %{expand:
    %cabal_configure
    %cabal build %{?ghc_smp_mflags}
}

%ghc_bin_install %{expand:
    %cabal_install
    %ghc_delete_rpaths
}

%cabal_test %cabal test %{?cabal_test_options}

%ghc_gen_filelist %{shrink:
    %__ghc_gen_filelist %buildroot %_libdir %_ghclibdir
                        %_builddir/%{?buildsubdir}
                        %_docdir %_datadir
}
