#! /bin/sh
# Copyright (C) 2015 Free Software Foundation, Inc.
#
# This program 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 3 of the License, or
# (at your option) any later version.
#
# This program 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, see <http://www.gnu.org/licenses/>.

# This test verifies that "find -L -type d -delete" will successfully
# delete a symbollic link to a directory.

testname="$(basename $0)"

. "${srcdir}"/binary_locations.sh

setup() {
    rm -rf parent
    mkdir parent parent/child && ( cd parent && ln -s child symlink )
}

runtest() {
    find="$1"; shift
    runtest_rv=0
    if "${find}" -L parent -depth -type d -delete; then
	true
    else
	echo "FAIL: ${executable} returned nonzero status" >&2
	runtest_rv=1
    fi

    if [ -d parent ] || [ -d parent/child ] || [ -e parent/symlink ]; then
	echo "FAIL: something was not deleted: " >&2
	ls -lR parent >&2
	runtest_rv=1
    fi
    return $runtest_rv
}

rv=0
if outdir=$(mktemp -d); then
    echo "Temporary directory is ${outdir}"
    for executable in "$oldfind" "$ftsfind"
    do
	full_test_name="${testname}_$(basename ${executable})"
	if ( cd "${outdir}" && setup && runtest "${executable}" ); then
	    echo "PASS: $full_test_name"
	else
	    rv=1
	    echo "FAIL: $full_test_name"
	fi
	rm -rf "${outdir}"/*
    done
else
    echo "FAIL: could not create a test directory."  >&2
    exit 1
fi
rm -rf "${outdir}"
 exit "${rv}"

