# Copyright 2023 The Bazel Authors. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

#!/bin/bash
#
# template generated by genshtest

# Find input files
declare -r INPUT_JDEPS="${TEST_SRCDIR}/src/tools/jdeps/testdata/test_data.jdeps"
declare -r OUT_JDEPS="${TEST_TMPDIR}/test_data.filtered.jdeps"
declare -r JDEPS_FILTER_TOOL="${TEST_SRCDIR}/src/tools/jdeps/jdeps"
declare -r JDEPS_PRINT_TOOL="${TEST_SRCDIR}/src/tools/jdeps/print_jdeps"

# Verify that _resource.jar is in jdeps
jdeps=`$JDEPS_PRINT_TOOL --in $INPUT_JDEPS`
matches=`echo "$jdeps" | grep '_resources.jar$' | wc -l`
entries=`echo "$jdeps" | wc -l`
[ $matches -eq 1 ] || die "Original jdeps did not have one resources.jar"

# Run filter tool
$JDEPS_FILTER_TOOL --in $INPUT_JDEPS --target "_resources.jar" --out $OUT_JDEPS

# Verify that _resource.jar is not in jdeps
filtered_jdeps=`$JDEPS_PRINT_TOOL --in $OUT_JDEPS`
filtered_matches=`echo "$filtered_jdeps" | grep '_resources.jar$' | wc -l`
filtered_entries=`echo "$filtered_jdeps" | wc -l`
[ $filtered_matches -eq 0 ] || die "Filtered jdeps still has resources.jar"

# Verify that we removed one item
[ $(($entries-$filtered_entries)) -eq 1 ] || die "Did not remove one item from jdeps"

echo "PASS"
