#!/usr/bin/env python3

import os
import sys
import gi

gi.require_version('Gtk', '3.0')
from gi.repository import Gtk


def is_live_environment():
    return os.path.isdir('/union/root')


class LiveWrapper(Gtk.Window):
    def __init__(self):
        super().__init__(title="MyLiveGtk Warning")
        self.set_default_size(450, 120)
        self.set_position(Gtk.WindowPosition.CENTER)

        icon = "/usr/share/icons/hicolor/48x48/apps/mylivegtk.png"
        if os.path.exists(icon):
            self.set_icon_from_file(icon)

        vbox = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=12)
        self.add(vbox)

        label = Gtk.Label(
            label=(
                "\nYou are running MyLiveGtk from a LIVE ISO.\n\n"
                "This program cannot run in a live environment.\n"
            )
        )
        label.set_justify(Gtk.Justification.CENTER)
        label.set_xalign(0.5)

        button_cancel = Gtk.Button.new_with_label("⛔️ Close")
        button_cancel.connect("clicked", self.on_button_cancel_clicked)

        vbox.pack_start(label, True, True, 10)
        vbox.pack_start(button_cancel, False, False, 5)

    def on_button_cancel_clicked(self, widget):
        self.destroy()
        Gtk.main_quit()


if __name__ == "__main__":
    if is_live_environment():
        win = LiveWrapper()
        win.connect("destroy", Gtk.main_quit)
        win.show_all()
        Gtk.main()
    else:
        os.system("rm -f /var/log/mylivegtk.log")
        os.execv(
            "/usr/bin/python3",
            ["python3", "/usr/share/mylivegtk/mylivegtk.py"] + sys.argv[1:]
        )

