import re

path = '/opt/struktur/obsidian-graphiti-import/aa043-writer-loop.sh'
with open(path) as f:
    lines = f.readlines()

out = []
for i, line in enumerate(lines):
    if 'usage=$CUR_USAGE spent=$SPENT done=' in line:
        # Replace this line with two clean lines
        out.append('  DONE_COUNT=$(sqlite3 $DB "select count(*) from graphiti_import_queue where reconciliation_record_id like \'aa043:%\' and graphiti_status=\'done\'")\n')
        out.append('  echo "$(date -u +%H:%M:%S) usage=$CUR_USAGE spent=$SPENT done=$DONE_COUNT" | tee -a $LOG\n')
        print(f'Fixed line {i+1}')
    else:
        out.append(line)

with open(path, 'w') as f:
    f.writelines(out)
print('Done')
