#!/usr/bin/env python3
"""
Append _N suffix to all aliases in oto.ini, similar to how A3-Soft has _S suffix.
This preserves all timing parameters - only modifies the alias part.
"""

import sys
from pathlib import Path

oto_file = Path(__file__).parent / 'oto.ini'

# Read the file
with open(oto_file, 'r', encoding='shift_jis', errors='replace') as f:
    lines = f.readlines()

updated_lines = []
updated_count = 0

for line in lines:
    line = line.rstrip('\n\r')
    
    # Skip empty lines
    if not line.strip():
        updated_lines.append(line)
        continue
    
    # Parse: filename.wav=alias,param1,param2,...
    if '=' not in line:
        updated_lines.append(line)
        continue
    
    parts = line.split('=', 1)
    if len(parts) != 2:
        updated_lines.append(line)
        continue
    
    filename = parts[0]
    rest = parts[1]
    
    # Split alias from parameters (first comma separates alias from params)
    if ',' in rest:
        alias = rest.split(',', 1)[0].strip()
        params = rest[len(alias):]  # Everything after the alias (including the comma)
    else:
        # No parameters, just alias
        alias = rest.strip()
        params = ''
    
    # Skip if alias already has _N suffix
    if alias.endswith('_N'):
        updated_lines.append(line)
        continue
    
    # Append _N to alias
    new_alias = alias + '_N'
    new_line = f'{filename}={new_alias}{params}'
    updated_lines.append(new_line)
    updated_count += 1

# Write back to file
with open(oto_file, 'w', encoding='shift_jis', errors='replace') as f:
    for line in updated_lines:
        f.write(line + '\n')

print(f'✓ Updated {updated_count} aliases with _N suffix')
print(f'✓ File saved: {oto_file}')

