#!/usr/bin/env python3
"""Fix nested <script> tag in search_app.py"""
PATH = '/root/gov_crawler/search_app.py'

with open(PATH, 'r') as f:
    content = f.read()

# Remove the standalone <script> line that's nested inside another script block
old = '}\n<script>\nfunction filterStatus(status) {'
new = '}\n\nfunction filterStatus(status) {'

if old in content:
    content = content.replace(old, new, 1)
    with open(PATH, 'w') as f:
        f.write(content)
    import py_compile
    py_compile.compile(PATH, doraise=True)
    print("Fixed: removed nested <script> tag")
else:
    print("ERROR: pattern not found!")
    # Debug: show context around 'sortTable' and 'filterStatus'
    idx = content.find('function sortTable')
    if idx >= 0:
        print(f"sortTable found at {idx}")
        print(repr(content[idx:idx+500]))
