/* checkupdates.c generated by valac 0.56.18-dirty, the Vala compiler
 * generated from checkupdates.vala, do not modify */

/*
 *  pamac-vala
 *
 *  Copyright (C) 2023 Guillaume Benoit <guillaume@manjaro.org>
 *
 *  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 get of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <glib.h>
#include "pamac.h"
#include <glib-object.h>
#include <gio/gio.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

typedef struct _Block1Data Block1Data;
#define _g_main_loop_unref0(var) ((var == NULL) ? NULL : (var = (g_main_loop_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

struct _Block1Data {
	int _ref_count_;
	GMainLoop* loop;
};

VALA_EXTERN gint checkupdates (void);
static Block1Data* block1_data_ref (Block1Data* _data1_);
static void block1_data_unref (void * _userdata_);
static void ___lambda4_ (Block1Data* _data1_);
static void ____lambda4__gasync_ready_callback (GObject* source_object,
                                         GAsyncResult* res,
                                         gpointer self);
static void ___lambda5_ (Block1Data* _data1_);
static void ____lambda5__gasync_ready_callback (GObject* source_object,
                                         GAsyncResult* res,
                                         gpointer self);
static void ____lambda6_ (Block1Data* _data1_);
static void _____lambda6__gasync_ready_callback (GObject* source_object,
                                          GAsyncResult* res,
                                          gpointer self);
static gint _vala_main (void);

static Block1Data*
block1_data_ref (Block1Data* _data1_)
{
	g_atomic_int_inc (&_data1_->_ref_count_);
	return _data1_;
}

static void
block1_data_unref (void * _userdata_)
{
	Block1Data* _data1_;
	_data1_ = (Block1Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data1_->_ref_count_)) {
		_g_main_loop_unref0 (_data1_->loop);
		g_slice_free (Block1Data, _data1_);
	}
}

static void
___lambda4_ (Block1Data* _data1_)
{
	GMainLoop* _tmp0_;
	_tmp0_ = _data1_->loop;
	g_main_loop_quit (_tmp0_);
}

static void
____lambda4__gasync_ready_callback (GObject* source_object,
                                    GAsyncResult* res,
                                    gpointer self)
{
	___lambda4_ (self);
	block1_data_unref (self);
}

static gint
vala_g_ptr_array_get_length (GPtrArray* self)
{
	gint result;
	g_return_val_if_fail (self != NULL, 0);
	result = (gint) self->len;
	return result;
}

static void
___lambda5_ (Block1Data* _data1_)
{
	GMainLoop* _tmp0_;
	_tmp0_ = _data1_->loop;
	g_main_loop_quit (_tmp0_);
}

static void
____lambda5__gasync_ready_callback (GObject* source_object,
                                    GAsyncResult* res,
                                    gpointer self)
{
	___lambda5_ (self);
	block1_data_unref (self);
}

static void
____lambda6_ (Block1Data* _data1_)
{
	GMainLoop* _tmp0_;
	_tmp0_ = _data1_->loop;
	g_main_loop_quit (_tmp0_);
}

static void
_____lambda6__gasync_ready_callback (GObject* source_object,
                                     GAsyncResult* res,
                                     gpointer self)
{
	____lambda6_ (self);
	block1_data_unref (self);
}

gint
checkupdates (void)
{
	Block1Data* _data1_;
	PamacConfig* config = NULL;
	PamacConfig* _tmp0_;
	PamacConfig* _tmp1_;
	PamacConfig* _tmp2_;
	PamacDatabase* database = NULL;
	PamacConfig* _tmp3_;
	PamacDatabase* _tmp4_;
	PamacTransaction* transaction = NULL;
	PamacDatabase* _tmp5_;
	PamacTransaction* _tmp6_;
	GMainLoop* _tmp7_;
	PamacDatabase* _tmp8_;
	PamacUpdates* updates = NULL;
	PamacDatabase* _tmp11_;
	PamacUpdates* _tmp12_;
	guint updates_nb = 0U;
	PamacUpdates* _tmp13_;
	GPtrArray* _tmp14_;
	GPtrArray* _tmp15_;
	gint _tmp16_;
	gint _tmp17_;
	PamacUpdates* _tmp18_;
	GPtrArray* _tmp19_;
	GPtrArray* _tmp20_;
	gint _tmp21_;
	gint _tmp22_;
	PamacUpdates* _tmp23_;
	GPtrArray* _tmp24_;
	GPtrArray* _tmp25_;
	gint _tmp26_;
	gint _tmp27_;
	gint result;
	_data1_ = g_slice_new0 (Block1Data);
	_data1_->_ref_count_ = 1;
	_tmp0_ = pamac_config_new ("/etc/pamac.conf");
	config = _tmp0_;
	_tmp1_ = config;
	pamac_config_set_enable_appstream (_tmp1_, FALSE);
	_tmp2_ = config;
	pamac_config_set_enable_snap (_tmp2_, FALSE);
	_tmp3_ = config;
	_tmp4_ = pamac_database_new (_tmp3_);
	database = _tmp4_;
	_tmp5_ = database;
	_tmp6_ = pamac_transaction_new (_tmp5_);
	transaction = _tmp6_;
	_tmp7_ = g_main_loop_new (NULL, FALSE);
	_data1_->loop = _tmp7_;
	_tmp8_ = database;
	if (pamac_database_need_refresh (_tmp8_)) {
		PamacTransaction* _tmp9_;
		GMainLoop* _tmp10_;
		_tmp9_ = transaction;
		pamac_transaction_refresh_dbs_async (_tmp9_, ____lambda4__gasync_ready_callback, block1_data_ref (_data1_));
		_tmp10_ = _data1_->loop;
		g_main_loop_run (_tmp10_);
	}
	_tmp11_ = database;
	_tmp12_ = pamac_database_get_updates (_tmp11_);
	updates = _tmp12_;
	_tmp13_ = updates;
	_tmp14_ = pamac_updates_get_repos_updates (_tmp13_);
	_tmp15_ = _tmp14_;
	_tmp16_ = vala_g_ptr_array_get_length (_tmp15_);
	_tmp17_ = _tmp16_;
	_tmp18_ = updates;
	_tmp19_ = pamac_updates_get_aur_updates (_tmp18_);
	_tmp20_ = _tmp19_;
	_tmp21_ = vala_g_ptr_array_get_length (_tmp20_);
	_tmp22_ = _tmp21_;
	_tmp23_ = updates;
	_tmp24_ = pamac_updates_get_flatpak_updates (_tmp23_);
	_tmp25_ = _tmp24_;
	_tmp26_ = vala_g_ptr_array_get_length (_tmp25_);
	_tmp27_ = _tmp26_;
	updates_nb = (guint) ((_tmp17_ + _tmp22_) + _tmp27_);
	if (updates_nb == ((guint) 0)) {
		result = 0;
		_g_object_unref0 (updates);
		_g_object_unref0 (transaction);
		_g_object_unref0 (database);
		_g_object_unref0 (config);
		block1_data_unref (_data1_);
		_data1_ = NULL;
		return result;
	} else {
		PamacTransaction* _tmp28_;
		GMainLoop* _tmp29_;
		PamacConfig* _tmp30_;
		gboolean _tmp31_;
		gboolean _tmp32_;
		PamacUpdates* _tmp35_;
		GPtrArray* _tmp36_;
		GPtrArray* _tmp37_;
		PamacUpdates* _tmp58_;
		GPtrArray* _tmp59_;
		GPtrArray* _tmp60_;
		PamacUpdates* _tmp71_;
		GPtrArray* _tmp72_;
		GPtrArray* _tmp73_;
		_tmp28_ = transaction;
		pamac_transaction_refresh_files_dbs_async (_tmp28_, ____lambda5__gasync_ready_callback, block1_data_ref (_data1_));
		_tmp29_ = _data1_->loop;
		g_main_loop_run (_tmp29_);
		_tmp30_ = config;
		_tmp31_ = pamac_config_get_download_updates (_tmp30_);
		_tmp32_ = _tmp31_;
		if (_tmp32_) {
			PamacTransaction* _tmp33_;
			GMainLoop* _tmp34_;
			_tmp33_ = transaction;
			pamac_transaction_download_updates_async (_tmp33_, _____lambda6__gasync_ready_callback, block1_data_ref (_data1_));
			_tmp34_ = _data1_->loop;
			g_main_loop_run (_tmp34_);
		}
		_tmp35_ = updates;
		_tmp36_ = pamac_updates_get_repos_updates (_tmp35_);
		_tmp37_ = _tmp36_;
		{
			GPtrArray* pkg_collection = NULL;
			guint pkg_index = 0U;
			pkg_collection = _tmp37_;
			for (pkg_index = 0; pkg_index < pkg_collection->len; pkg_index = pkg_index + 1) {
				PamacAlpmPackage* pkg = NULL;
				pkg = (PamacAlpmPackage*) g_ptr_array_index (pkg_collection, pkg_index);
				{
					PamacAlpmPackage* _tmp38_;
					const gchar* _tmp39_;
					const gchar* _tmp40_;
					_tmp38_ = pkg;
					_tmp39_ = pamac_package_get_installed_version ((PamacPackage*) _tmp38_);
					_tmp40_ = _tmp39_;
					if (_tmp40_ != NULL) {
						FILE* _tmp41_;
						PamacAlpmPackage* _tmp42_;
						const gchar* _tmp43_;
						const gchar* _tmp44_;
						PamacAlpmPackage* _tmp45_;
						const gchar* _tmp46_;
						const gchar* _tmp47_;
						PamacAlpmPackage* _tmp48_;
						const gchar* _tmp49_;
						const gchar* _tmp50_;
						_tmp41_ = stdout;
						_tmp42_ = pkg;
						_tmp43_ = pamac_package_get_name ((PamacPackage*) _tmp42_);
						_tmp44_ = _tmp43_;
						_tmp45_ = pkg;
						_tmp46_ = pamac_package_get_installed_version ((PamacPackage*) _tmp45_);
						_tmp47_ = _tmp46_;
						_tmp48_ = pkg;
						_tmp49_ = pamac_package_get_version ((PamacPackage*) _tmp48_);
						_tmp50_ = _tmp49_;
						fprintf (_tmp41_, "%s  %s -> %s\n", _tmp44_, _tmp47_, _tmp50_);
					} else {
						FILE* _tmp51_;
						PamacAlpmPackage* _tmp52_;
						const gchar* _tmp53_;
						const gchar* _tmp54_;
						PamacAlpmPackage* _tmp55_;
						const gchar* _tmp56_;
						const gchar* _tmp57_;
						_tmp51_ = stdout;
						_tmp52_ = pkg;
						_tmp53_ = pamac_package_get_name ((PamacPackage*) _tmp52_);
						_tmp54_ = _tmp53_;
						_tmp55_ = pkg;
						_tmp56_ = pamac_package_get_version ((PamacPackage*) _tmp55_);
						_tmp57_ = _tmp56_;
						fprintf (_tmp51_, "%s  %s\n", _tmp54_, _tmp57_);
					}
				}
			}
		}
		_tmp58_ = updates;
		_tmp59_ = pamac_updates_get_aur_updates (_tmp58_);
		_tmp60_ = _tmp59_;
		{
			GPtrArray* pkg_collection = NULL;
			guint pkg_index = 0U;
			pkg_collection = _tmp60_;
			for (pkg_index = 0; pkg_index < pkg_collection->len; pkg_index = pkg_index + 1) {
				PamacAURPackage* pkg = NULL;
				pkg = (PamacAURPackage*) g_ptr_array_index (pkg_collection, pkg_index);
				{
					FILE* _tmp61_;
					PamacAURPackage* _tmp62_;
					const gchar* _tmp63_;
					const gchar* _tmp64_;
					PamacAURPackage* _tmp65_;
					const gchar* _tmp66_;
					const gchar* _tmp67_;
					PamacAURPackage* _tmp68_;
					const gchar* _tmp69_;
					const gchar* _tmp70_;
					_tmp61_ = stdout;
					_tmp62_ = pkg;
					_tmp63_ = pamac_package_get_name ((PamacPackage*) _tmp62_);
					_tmp64_ = _tmp63_;
					_tmp65_ = pkg;
					_tmp66_ = pamac_package_get_installed_version ((PamacPackage*) _tmp65_);
					_tmp67_ = _tmp66_;
					_tmp68_ = pkg;
					_tmp69_ = pamac_package_get_version ((PamacPackage*) _tmp68_);
					_tmp70_ = _tmp69_;
					fprintf (_tmp61_, "%s  %s -> %s\n", _tmp64_, _tmp67_, _tmp70_);
				}
			}
		}
		_tmp71_ = updates;
		_tmp72_ = pamac_updates_get_flatpak_updates (_tmp71_);
		_tmp73_ = _tmp72_;
		{
			GPtrArray* pkg_collection = NULL;
			guint pkg_index = 0U;
			pkg_collection = _tmp73_;
			for (pkg_index = 0; pkg_index < pkg_collection->len; pkg_index = pkg_index + 1) {
				PamacFlatpakPackage* pkg = NULL;
				pkg = (PamacFlatpakPackage*) g_ptr_array_index (pkg_collection, pkg_index);
				{
					const gchar* app_name = NULL;
					PamacFlatpakPackage* _tmp74_;
					const gchar* _tmp75_;
					const gchar* _tmp76_;
					const gchar* _tmp77_;
					_tmp74_ = pkg;
					_tmp75_ = pamac_package_get_app_name ((PamacPackage*) _tmp74_);
					_tmp76_ = _tmp75_;
					app_name = _tmp76_;
					_tmp77_ = app_name;
					if (_tmp77_ == NULL) {
						FILE* _tmp78_;
						PamacFlatpakPackage* _tmp79_;
						const gchar* _tmp80_;
						const gchar* _tmp81_;
						PamacFlatpakPackage* _tmp82_;
						const gchar* _tmp83_;
						const gchar* _tmp84_;
						_tmp78_ = stdout;
						_tmp79_ = pkg;
						_tmp80_ = pamac_package_get_name ((PamacPackage*) _tmp79_);
						_tmp81_ = _tmp80_;
						_tmp82_ = pkg;
						_tmp83_ = pamac_package_get_version ((PamacPackage*) _tmp82_);
						_tmp84_ = _tmp83_;
						fprintf (_tmp78_, "%s  %s\n", _tmp81_, _tmp84_);
					} else {
						FILE* _tmp85_;
						const gchar* _tmp86_;
						PamacFlatpakPackage* _tmp87_;
						const gchar* _tmp88_;
						const gchar* _tmp89_;
						_tmp85_ = stdout;
						_tmp86_ = app_name;
						_tmp87_ = pkg;
						_tmp88_ = pamac_package_get_version ((PamacPackage*) _tmp87_);
						_tmp89_ = _tmp88_;
						fprintf (_tmp85_, "%s  %s\n", _tmp86_, _tmp89_);
					}
				}
			}
		}
		result = 100;
		_g_object_unref0 (updates);
		_g_object_unref0 (transaction);
		_g_object_unref0 (database);
		_g_object_unref0 (config);
		block1_data_unref (_data1_);
		_data1_ = NULL;
		return result;
	}
}

static gint
_vala_main (void)
{
	gint result;
	result = checkupdates ();
	return result;
}

int
main (int argc,
      char ** argv)
{
	return _vala_main ();
}

