#!/bin/sh
# Copyright 1999-2026 Gentoo Authors
# Distributed under the terms of the GNU General Public License v2

# IPX stop helper for systemd and OpenRC.
#
# This script disables automatic IPX behaviour, removes configured static
# routes, removes all configured IPX interfaces and deletes the internal IPX
# network if present.

set -u

retval=0

oldifs=${IFS}
IFS='
'
for entry in ${IPX_ROUTES:-}; do
    [ -n "${entry}" ] || continue

    case "${entry}" in
	\#*)
	    continue
	    ;;
    esac

    target=${entry%%:*}

    if ! /sbin/ipx_route del "${target}"; then
	printf '%s\n' "ipx: failed to remove route to ${target}" >&2
	retval=1
    fi
done
IFS=${oldifs}

if ! /sbin/ipx_configure --auto_primary=off --auto_interface=off; then
    printf '%s\n' "ipx: failed to disable automatic IPX configuration" >&2
    retval=1
fi

if ! /sbin/ipx_interface delall; then
    printf '%s\n' "ipx: failed to remove IPX interfaces" >&2
    retval=1
fi

if ! /sbin/ipx_internal_net del; then
    :
fi

exit "${retval}"
