#!/bin/sh

# generate missing symlinks for all Impacket examples
# except those already included in python3-impacket

set -e

rm -f debian/links
rm -f debian/tests/list-tests

# list all the scripts that don't support the -h option
# nmapAnswerMachine is here because it's currently broken:
# https://github.com/SecureAuthCorp/impacket/pull/572/files
# https://github.com/SecureAuthCorp/impacket/issues/403
DONT_TEST="ifmap karmaSMB mqtt_check nmapAnswerMachine opdump ping ping6 smbrelayx sniff split"

# Prepare debian/tests/list-tests
echo "#!/bin/sh" >> debian/tests/list-tests
echo "set -e" >> debian/tests/list-tests
echo "" >> debian/tests/list-tests
chmod 755 debian/tests/list-tests

for file in "/usr/share/doc/python3-impacket/examples"/*; do
    # Get the script name
    name_script=$(basename "$file")
    # Remove the extension
    example_name="${name_script%.*}"

    # if the link doesn't exist/ not in python3-impacket
    # create symlink and autopkgtest
    if [ ! -e /usr/bin/impacket-$example_name ]; then
	# create symlink
	echo "usr/share/impacket/script usr/bin/impacket-"$example_name >> debian/links

        # create autopkgtest
	if ! $(echo $DONT_TEST | grep -w -q $example_name); then
            echo "echo \"Test impacket-"$example_name\" >> debian/tests/list-tests
	    echo "impacket-"$example_name "-h" >> debian/tests/list-tests
	    echo "" >> debian/tests/list-tests
	fi
    fi
done
