#! /bin/bash
# Author: Till Maas <opensource@till.name>
#
# {{{ GPLv2+ license header
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
# }}}
#
# This scripts finds some basic ghc dependencies. To use it, add these lines to your specfile:
# %define __find_requires /bin/bash ghcdep.sh --requires
# %define _use_internal_dependency_generator 0 

# from pythondeps.sh
[ $# -ge 1 ] || {
    cat > /dev/null
    exit 0
}

case $1 in
-R|--requires)
    while read
    do
        test -f "${REPLY}" && /usr/lib/rpm/rpmdeps --requires "${REPLY}" 2>/dev/null
        if  echo "${REPLY}" | grep -s "/usr/lib[^/]*/ghc-[0-9]*\\.[0-9]*\\.[0-9]*/" &>/dev/null
        then
            # remove prefix, thanks to the * after ${RPM_BUILD_ROOT}, it should
            # also work in most cases, where RPM_BUILD_ROOT is not defined
            # If /usr/lib*/ghc- matches several times, it may fail
            ghc_num_ver=${REPLY#${RPM_BUILD_ROOT}*/usr/lib*/ghc-}
            # remove suffix
            ghc_num_ver=${ghc_num_ver%%/*}

            # print ghc dependency
            echo "ghc = ${ghc_num_ver}"

            # check for ghc-prof dependency, based on existence of *.p_hi or *_p.a files
            if echo "${REPLY}" | grep -s "/usr/lib[^/]*/ghc-${ghc_num_ver}/.*\\.p_hi$" &>/dev/null ||  echo "${REPLY}" | grep -s "/usr/lib[^/]*/ghc-${ghc_num_ver}/.*\\_p.a$" &>/dev/null
            then
                echo "ghc-prof = ${ghc_num_ver}"
            fi
        fi
    # Output every dependency only once
    done | sort -u
    exit 0
    ;;
esac

exit 0
