#!/bin/sh
# arch-tag: Debhelper script for Haskell
#
# Copyright (C) 2009 Marco Túlio Gontijo e Silva <marcot@holoscopio.com>
# Based on mk-haskell-depends by Martin Sjögren <sjogren@debian.org>
#
# CREATIVE COMMONS CORPORATION IS NOT A LAW FIRM AND DOES NOT PROVIDE LEGAL
# SERVICES. DISTRIBUTION OF THIS CC0 WAIVER DOES NOT CREATE AN ATTORNEY-CLIENT
# RELATIONSHIP. CREATIVE COMMONS PROVIDES THIS INFORMATION ON AN "AS-IS"
# BASIS. CREATIVE COMMONS MAKES NO WARRANTIES REGARDING THE INFORMATION
# PROVIDED, AND DISCLAIMS LIABILITY FOR DAMAGES RESULTING FROM ITS USE.
#
# Statement of Purpose
#
# The laws of various jurisdictions throughout the world automatically confer
# certain exclusive rights upon the creator of an original work of authorship
# and/or a database (each, a “Work”), and the subsequent owners thereof.
#
# Some owners of these exclusive rights wish to permanently remove these
# restrictions from a Work for the purpose of contributing to a commons that
# the public can reliably build upon, modify, reuse and redistribute as freely
# as possible from such restrictions for any purposes and in any form
# whatsoever, whether modified or unmodified, in whole or in part. Such owners
# therefore wish to fully, permanently, irrevocably and unconditionally waive,
# abandon and relinquish their Copyright Related Rights (defined below) with
# respect to a Work to the fullest extent permitted by applicable law.
#
# A Work associated with this CC0 Waiver may be protected by copyright and
# related or neighboring rights, database rights (such as those arising under
# Directive 96/9/EC of the European Parliament and of the Council of 11 March
# 1996 on the legal protection of databases, and under any national
# implementation thereof, (including any amended or successor version of such
# directive)), and/or other similar equivalent or corresponding rights
# throughout the world as such rights may be available under applicable law or
# treaty (and national implementations thereof), including but not limited to
# moral rights retained by the original author(s), publicity and privacy rights
# pertaining to an individual’s image or likeness depicted in a Work, rights
# protecting against unfair competition in regards to the Work, and any rights
# protecting the extraction, dissemination and reuse of data in the Work (such
# rights all, collectively, the “Copyright Related Rights”).
#
# The person associating this CC0 Waiver with a Work is the owner of Copyright
# Related Rights in the Work (the “Affirmer”), and does openly, overtly and
# voluntarily elect to apply this CC0 Waiver to his or her Work with knowledge
# of his or her Copyright Related Rights in the Work and the meaning and
# intended legal effect of this CC0 Waiver.
#
# Subject to rights of other persons in the Work, and any patent and trademark
# rights held by Affirmer, by associating this CC0 Waiver with a Work the
# Affirmer does intend to fully, permanently, irrevocably and unconditionally
# waive, abandon and relinquish all of his or her Copyright Related Rights in
# such Work, to the fullest extent permitted by (but not in contravention of)
# applicable law.
#
# CC0 Waiver
#
# 1.  To the fullest extent permitted by (but not in contravention of)
# applicable law, Affirmer hereby fully, permanently, irrevocably and
# unconditionally waives, abandons and relinquishes all of Affirmer’s Copyright
# Related Rights and associated claims and causes of action, whether present or
# future, vested or contingent, in the Work, (such waiver, abandonment, and
# relinquishment, the "Waiver"). Affirmer makes the Waiver for the benefit of
# each member of the public at large and to the detriment of Affirmer's heirs
# or successors.
#
# 2. Should the Waiver for any reason be judged legally invalid or ineffective
# under applicable law, then the Waiver shall be preserved to the maximum
# extent permitted and Affirmer hereby grants to each such affected recipient
# of the Work a worldwide, royalty-free, non exclusive, perpetual (for the
# duration of the applicable copyright), non transferable, non sublicensable,
# irrevocable and unconditional license to exercise Affirmer’s Copyright
# Related Rights in the Work, which license shall be deemed effective as of the
# date this CC0 Waiver was applied by Affirmer to the Work.
#
# 3. No trademark or patent rights held by Affirmer are waived, abandoned,
# relinquished, licensed or otherwise affected by this CC0 Waiver.
#
# 4. Affirmer hereby fully and completely disclaims responsibility for clearing
# rights of other persons that may apply to the Work or any intended use
# thereof, including without limitation any such person’s Copyright Related
# Rights (including privacy and publicity rights). Further, Affirmer hereby
# fully and completely disclaims responsibility for obtaining any necessary
# consents, permissions or other rights required for such intended use.
#
# 5. Affirmer understands and acknowledges that Creative Commons is not party
# to this CC0 Waiver and has no duty or obligation with respect to this CC0
# Waiver or use of the Work.

set -e

. /usr/share/haskell-devscripts/Dh_Haskell.sh

pkgs=`dh_listpackages $args`

for pkg in $pkgs
do
    case "$pkg" in
        libghc6-*-dev)
            config=`find_config_for_ghc6 $pkg`
            [ -f "$config" ] && files="$files $config"
            ;;
    esac
done

if [ -z "$files" ]
then
	echo "No installed package description files can not be found" >&2
	exit 1
fi

T_DIR=debian/dh_haskell_shlibdeps
mkdir $T_DIR 2> /dev/null || true
echo '[]' > $T_DIR/package.conf
for file in $files
do
    pkg=`echo $file | cut -d/ -f2 | cut -d- -f2- | sed -e 's/-[^-]\+$//'`
    pkgconflocal=$T_DIR/$pkg.package.conf
    pkgdir=`echo $file | sed -ne "s#^.*debian/libghc6-$pkg-dev\(/.*\)/\([^/]\+\)#\1#p"`
    cat $file | sed -e \
        "/^\(import\|library\|include\)-dirs: / { \
        s#$pkgdir#$PWD/debian/libghc6-$pkg-dev$pkgdir#g }" \
        > $pkgconflocal
    ghc-pkg -f $T_DIR/package.conf register $pkgconflocal --force \
        2>&1 > /dev/null || true
done
for pkg in $pkgs
do
    case "$pkg" in
        libghc6-*-dev)
            sfile=debian/$pkg.substvars
            pkgname=`basename debian/$pkg/usr/lib/haskell-packages/ghc6/lib/* | sed -e 's/-[^-]\+$//'`
            touch $sfile
            echo "module Main where main = return ()" > $T_DIR/a.hs
            ghc --make $T_DIR/a.hs -o $T_DIR/a.out -package-conf \
                $T_DIR/package.conf -package $pkgname
            dpkg-shlibdeps -Tdebian/$pkg.substvars $T_DIR/a.out
            ;;
    esac
done
