/* checkupdates.c generated by valac 0.56.14, 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_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

#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _Block1Data Block1Data;
#define _g_main_loop_unref0(var) ((var == NULL) ? NULL : (var = (g_main_loop_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 gint _vala_main (void);

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 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);
}

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

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

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

