"""Step 3: create the API key the game uses (only requests with this key are translated).

DirectAdmin > Setup Python App > Execute python script:  lt_key.py  > Run Script
The key is shown and also saved in lt_key.txt in this folder. Copy it into
Admin > Game options > Chat translation > API key, then delete lt_key.txt.
"""
import contextlib
import io
import os
import sys

HERE = os.path.dirname(os.path.abspath(__file__))
os.chdir(HERE)   # the key database is db/api_keys.db in this folder, where the app reads it

from libretranslate.manage import manage  # noqa: E402

out = io.StringIO()
sys.argv = ['ltmanage', 'keys', 'add', '600']   # 600 translations a minute
with contextlib.redirect_stdout(out):
    manage()
text = out.getvalue().strip()
with open(os.path.join(HERE, 'lt_key.txt'), 'w') as f:
    f.write(text + '\n')
print('API key: ' + text)
print('Also saved in lt_key.txt. Restart the app now.')
