#!/bin/bash
# pkg-config - wrapper script for cross-compilation.
# Copyright (C) 2005 g10 Code GmbH
# 
# This file is part of GPG4Win.
# 
# GPG4Win 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.
# 
# GPG4Win 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA

CMD=/usr/bin/pkg-config
PREFIX=/usr

if [ ! "$SYSROOT" ]; then
    echo "pkg-config-filter: missing \$SYSROOT environment variable"
    exit 2
fi

if [ ! "$PKG_CONFIG_LIBDIR" ]; then
    export PKG_CONFIG_LIBDIR=$SYSROOT/lib/pkgconfig/
fi

export PKG_CONFIG_LIBDIR
export PKG_CONFIG_PATH

if $CMD $* |
    sed -e "s~\-L/*$SYSROOT/*~-L=/~g; s~\-I/*$SYSROOT/*~-I=/~g;" | # protect already given sysroot 
    sed -e "s~\-L$PREFIX/~-L=/~g; s~\-I$PREFIX/~-I=/~g;" | # add sysroot symbol to all absolute pathes
    sed -e "s~\-L\=~-L$SYSROOT~g; s~\-I\=~-I$SYSROOT~g;"  # replace sysroot sign to sysroot path
#    sed -e "s~\-x11\-~-win32-~g;" # replace x11 with win32 (target)
#    sed -e "s~pangoxft~pangow32ft~g;" # replace x11 with win32 (target)
then    
#    echo "PKG_CONFIG_LIBDIR=$PKG_CONFIG_LIBDIR" >&2
#    echo "PKG_CONFIG_PATH=$PKG_CONFIG_PATH" >&2
#    echo "OKAY" >&2;
    # See question 11 in comp.unix.shell FAQ.
    exit ${PIPESTATUS[0]};
else
    echo "pkg-config pipe failed!" >&2 
    exit 1
fi
