OPENJFX_SUFFIX="-22"

export CXXFLAGS="${CXXFLAGS} -Wno-expansion-to-defined -fPIC "
export CFLAGS="${CFLAGS} -Wno-expansion-to-defined -fPIC "

unset _JAVA_OPTIONS
unset VERSION

chmod a+x gradlew
SINGLE="--daemon"
BUILD_WEBKIT="true"

if [[ "${CROSS:-$ARCH}" != "amd64" ]]; then
    SINGLE="--no-daemon"
    BUILD_WEBKIT="false"
fi

abinfo 'Building bootstrap OpenJFX...'
cat << EOF > gradle.properties
COMPILE_WEBKIT=${BUILD_WEBKIT}
COMPILE_MEDIA=true
BUILD_JAVADOC=true
BUILD_SRC_ZIP=true
CONF=Release
COMPANY_NAME="AOSC"
EOF

if [[ "${CROSS:-$ARCH}" = "amd64" ]]; then
    "$SRCDIR"/gradlew web:compileNativeLinux
fi

abinfo 'Stripping all the native libraries to make diet jmods ...'
find "$SRCDIR"/modules -name '*.so' -exec 'strip' '-v' '{}' ';'
"$SRCDIR"/gradlew "$SINGLE" jmods

_builddir="$SRCDIR"/build
_sdkdir="${_builddir}"/sdk
install -dv "$PKGDIR"/usr/lib/openjfx/{lib,jmods}
install -dv "$PKGDIR"/usr/share/doc/openjfx
install -vm644 "${_builddir}"/jmods/* "$PKGDIR"/usr/lib/openjfx/jmods/

abinfo 'Building final OpenJFX...'
echo 'BUILD_FXPACKAGER=true' >> gradle.properties
"$SRCDIR"/gradlew "$SINGLE" sdk javadoc

install -vm644 "${_sdkdir}"/lib/* "$PKGDIR"/usr/lib/openjfx/lib/
chmod 0777 "$PKGDIR"/usr/lib/openjfx/lib/*.so
cp -arv "${_builddir}"/javadoc/* "$PKGDIR"/usr/share/doc/openjfx

if [[ -n "$OPENJFX_SUFFIX" ]]; then
    abinfo "Renaming for openjfx suffix ..."
    for path in "$PKGDIR"{/usr/lib/openjfx,/usr/share/doc/openjfx}; do
        mv -vf "$path" "$path""$OPENJFX_SUFFIX"
    done
fi
