#!/usr/bin/env python3

# Copyright (C) 2025 Volkov Alexey
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License,
# or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.

from alt_workstation_10_11_upgrade.tools import run
from pathlib import Path
import os
import shutil
import sys

run(["alt-panelmode", "--command=to-panel"])

autostart_name = os.path.basename(sys.argv[0]) + ".desktop"

system_autostart = Path(f"/etc/xdg/autostart/{autostart_name}")

home = Path.home()
destination_dir = home / ".config" / "autostart"
destination = destination_dir / autostart_name

if system_autostart.exists() and not destination.exists():
	try:
		destination_dir.mkdir(parents=True, exist_ok=True)
		shutil.copy2(system_autostart, destination)

		text = destination.read_text()
		new_text = text.replace("Hidden=false", "Hidden=true")

		if new_text != text:
			destination.write_text(new_text)

	except Exception as err:
		print(f"Warning: {err}", file=sys.stderr)
