#!/bin/sh

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

set -e

if [ ! -d /usr/share/doc/python3-impacket ]; then
    echo "Please install python3-impacket" >&2
    exit 1
fi

# list all the scripts that won't get a symlink
# - kintercept requires asyncore (removed in python 3.12)
DONT_SYMLINK="kintercept"

# list all the scripts that don't support the -h option
DONT_TEST="ifmap karmaSMB mqtt_check opdump ping ping6 sniff split"

# prepare debian/links and debian/tests/list-tests
rm -f debian/links
rm -f debian/tests/list-tests

cat << EOF > debian/tests/list-tests
#!/bin/sh
set -e

EOF

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%.*}"

    # skip some scripts
    if $(echo $DONT_SYMLINK | grep -w -q $example_name); then
	continue
    fi

    # if the link doesn't exist / isn't 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
