#include "ubinstall-gtk.h"

int yon_installation_start(main_window *widgets){
            gtk_widget_show(gtk_widget_get_parent(widgets->InstallationProgress));
    return !pthread_create(&main_config.install_thread,NULL,on_config_save,widgets);
    return 1;
}

void yon_quick_install(GtkWidget *self, main_window *widgets){
    if (!yon_installation_check_packages_size(widgets)) return;
    main_config.save_configured=1;
    gtk_widget_hide(self);
    gtk_widget_show(gtk_widget_get_parent(widgets->InstallationProgress));
    pthread_create(&main_config.install_thread,NULL,on_quick_install_start,widgets);
    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_INSTALLATION);
    yon_page_init(widgets,YON_PAGE_INSTALLATION);
    yon_page_update(widgets);
}

void *on_quick_install_start(void *data){
    main_window *widgets = (main_window*)data;

        g_idle_add((GSourceFunc)yon_progress_bar_start,widgets);
        if (!main_config.dry_run){
            char *install_path = main_config.config_load_path;
            if (!install_path) install_path = "system";
            char *command = yon_debug_output("%s\n",auto_install_command(install_path));
            yon_debug_output("%s\n","Entered installation");
            if (system(yon_debug_output("%s\n",command))){

                gdk_threads_add_idle((GSourceFunc)on_install_error,widgets);
                free(command);
                g_mutex_lock(&main_config.install_mutex);
                main_config.install_complete=1;
                g_mutex_unlock(&main_config.install_mutex);
                return 0;
            };
            free(command);
            g_mutex_lock(&main_config.install_mutex);
            main_config.install_complete=1;
            g_mutex_unlock(&main_config.install_mutex);
            g_idle_add((GSourceFunc)on_install_success,widgets);
        } else {
            yon_launch("ubconfig --source system get /");
        }
    return 0;
}

void on_autoinstall_start(GtkWidget *, main_window *widgets){
    main_config.save_configured=1;
    gtk_widget_show(gtk_widget_get_parent(widgets->InstallationProgress));
    pthread_create(&main_config.install_thread,NULL,on_auto_install_start,widgets);
    gtk_notebook_set_current_page(GTK_NOTEBOOK(widgets->Notebook),YON_PAGE_INSTALLATION);
    yon_page_init(widgets,YON_PAGE_INSTALLATION);
    yon_page_update(widgets);

}
void *on_auto_install_start(void *data){
    main_window *widgets = (main_window*)data;

        g_idle_add((GSourceFunc)yon_progress_bar_start,widgets);
        if (!main_config.dry_run){
            char *install_path = main_config.config_load_path;
            if (!install_path) install_path = "system";
            char *command = yon_debug_output("%s\n",auto_install_command(install_path));
            yon_debug_output("%s\n","Entered installation");
            if (system(yon_debug_output("%s\n",command))){

                gdk_threads_add_idle((GSourceFunc)on_install_error,widgets);
                free(command);
                g_mutex_lock(&main_config.install_mutex);
                main_config.install_complete=1;
                g_mutex_unlock(&main_config.install_mutex);
                return 0;
            };
            free(command);
            g_mutex_lock(&main_config.install_mutex);
            main_config.install_complete=1;
            g_mutex_unlock(&main_config.install_mutex);
            g_idle_add((GSourceFunc)on_install_success,widgets);
        } else {
            yon_launch("ubconfig --source system get /");
        }
    return 0;
}