#!/usr/bin/python3
"""Seed the actual localized desktop once, including for accounts created later."""
import os
import shutil
import subprocess
import sys
from pathlib import Path
from gi.repository import GLib
sys.path.insert(0, "/usr/share/newworld")
from backgrounds import state, save_value

if (state()/"chatgpt-shortcut-created").exists():
    sys.exit(0)
desktop = GLib.get_user_special_dir(GLib.UserDirectory.DIRECTORY_DESKTOP)
if not desktop or Path(desktop) == Path.home():
    sys.exit(0)
directory = Path(desktop)
directory.mkdir(exist_ok=True)
dest = directory/"ChatGPT.desktop"
try:
    with dest.open("xb") as out, Path("/usr/share/applications/newworld-chatgpt.desktop").open("rb") as original:
        shutil.copyfileobj(original, out)
    dest.chmod(0o755)
    subprocess.run(["gio","set", str(dest),"metadata::trusted","true"], capture_output=True)
except FileExistsError:
    pass
save_value("chatgpt-shortcut-created","1")
