#! /bin/sh

# A wrapper script around gcc

# Set STATIC_LIBS to the libraries that need to be linked statically.
# e.g. STATIC_LIBS="pcre z"
#
# ALso honours EARLY_ARGS

real_gcc=/usr/bin/gcc

args=""
opt_found=0
for arg in "$@"; do
    case "$arg" in
	-l*)
	    trans="$arg"
	    for lib in $STATIC_LIBS; do
		if [ "X-l$lib" = "X$arg" ]; then
		    trans="-Wl,-Bstatic $arg -Wl,-Bdynamic"
		    break
		fi
	    done
	    args="$args $trans" ;;
	*.opt)
	    if [ "X$prev_arg" = "X-o" ]; then
		opt_found=1
	    fi
	    args="$args $arg" ;;
	*)
	    args="$args $arg" ;;
    esac
    prev_arg="$arg"
done

if [ $opt_found -gt 0 ]; then
    echo "+ $real_gcc $args"
    exec $real_gcc "-Wl,--start-group" $EARLY_ARGS $args "-Wl,--end-group"
else
    $real_gcc "$@"
fi

