#!/usr/bin/env python3
"""pppd ip-down hook (ADR-0018 b4) — tear down a Vendora PPPoE session's shaping
+ its reaper record. Scoped by the session-map file's existence (only Vendora
PPPoE-server sessions have one), so a WAN PPPoE client is ignored.

pppd args: $1=iface $2=tty $3=speed $4=local-ip $5=remote-ip $6=ipparam
"""
import os
import sys

sys.path.insert(0, "/opt/vendora_sbc/services")
try:
    from common import pppoe_config as pc
    from common import tc as vtc
except Exception:
    sys.exit(0)

iface = sys.argv[1] if len(sys.argv) > 1 else os.environ.get("IFNAME", "")
if not iface:
    sys.exit(0)

mapfile = os.path.join(pc.SESSION_DIR, iface)
if not os.path.exists(mapfile):
    sys.exit(0)  # not a Vendora PPPoE session

try:
    vtc.unshape_ppp_iface(iface)
except Exception:
    pass
try:
    os.remove(mapfile)
except Exception:
    pass

sys.exit(0)
