#!/bin/sh
set -e

BUILD="$(pwd)"
newline="
"

if test -f "$BUILD"/mpv_options ; then
    IFS=$newline
    set -- $(cat "$BUILD"/mpv_options) "$@"
    unset -v IFS
fi

OPTIONS=""
if "$BUILD"/scripts/test-libmpv && [ "$BUILDSYSTEM" != "waf" ]; then
    OPTIONS="$OPTIONS -Dc_link_args='-Wl,-Bsymbolic'"
fi

case "$PKG_CONFIG_PATH" in
  '')
    export PKG_CONFIG_PATH="$BUILD/build_libs/lib/pkgconfig"
    ;;
  *)
    export PKG_CONFIG_PATH="$BUILD/build_libs/lib/pkgconfig:$PKG_CONFIG_PATH"
    ;;
esac

echo Using mpv options: $OPTIONS "$@"

cd "$BUILD"/mpv

# add missing private dependencies from libass.pc
# this is necessary due to the hybrid static / dynamic nature of the build
# need to link against stdc++ in case libplacebo was built with glslang,
# which requires that
export LDFLAGS="$LDFLAGS $(pkg-config --libs fontconfig harfbuzz fribidi) -lstdc++"
if [ "$BUILDSYSTEM" = "waf" ]; then
    python3 ./waf configure "$@"
else
    meson setup build -Dbuildtype=release $OPTIONS "$@"
fi
