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

/* -*- Mode: Vala; indent-tabs-mode: nil; tab-width: 2 -*-
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 * SPDX-FileCopyrightText: Michael Terry
 */

#include "deja.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib/gstdio.h>
#include <json-glib/json-glib.h>
#include <glib-object.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

enum  {
	DEJA_DUP_RESTIC_LOGGER_0_PROPERTY,
	DEJA_DUP_RESTIC_LOGGER_NUM_PROPERTIES
};
static GParamSpec* deja_dup_restic_logger_properties[DEJA_DUP_RESTIC_LOGGER_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define __vala_JsonNode_free0(var) ((var == NULL) ? NULL : (var = (_vala_JsonNode_free (var), NULL)))
typedef struct _Block9Data Block9Data;

struct _Block9Data {
	int _ref_count_;
	DejaDupLogObscurer* obscurer;
};

static gpointer deja_dup_restic_logger_parent_class = NULL;

static gchar* deja_dup_restic_logger_read_cachefile (void);
static gchar* deja_dup_restic_logger_replace_json (DejaDupLogObscurer* obscurer,
                                            const gchar* line);
static JsonNode* _vala_JsonNode_copy (JsonNode* self);
static void deja_dup_restic_logger_replace_node (DejaDupLogObscurer* obscurer,
                                          JsonNode* node);
static void _vala_JsonNode_free (JsonNode* self);
static Block9Data* block9_data_ref (Block9Data* _data9_);
static void block9_data_unref (void * _userdata_);
static void ___lambda60_ (Block9Data* _data9_,
                   JsonObject* obj,
                   const gchar* name,
                   JsonNode* n);
static void ____lambda60__json_object_foreach (JsonObject* object,
                                        const gchar* member_name,
                                        JsonNode* member_node,
                                        gpointer self);
static void ____lambda61_ (Block9Data* _data9_,
                    JsonArray* arr,
                    guint i,
                    JsonNode* n);
static void _____lambda61__json_array_foreach (JsonArray* array,
                                        guint index_,
                                        JsonNode* element_node,
                                        gpointer self);
static GType deja_dup_restic_logger_get_type_once (void);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);
static gssize _vala_array_length (gpointer array);

gchar*
deja_dup_restic_logger_from_cache_log (DejaDupLogObscurer* obscurer)
{
	gchar* contents = NULL;
	gchar* _tmp0_;
	const gchar* _tmp1_;
	gchar* _result_ = NULL;
	gchar* _tmp2_;
	gchar** lines = NULL;
	const gchar* _tmp3_;
	gchar** _tmp4_;
	gchar** _tmp5_;
	gint lines_length1;
	gint _lines_size_;
	gchar* result;
	g_return_val_if_fail (obscurer != NULL, NULL);
	_tmp0_ = deja_dup_restic_logger_read_cachefile ();
	contents = _tmp0_;
	_tmp1_ = contents;
	if (_tmp1_ == NULL) {
		result = NULL;
		_g_free0 (contents);
		return result;
	}
	_tmp2_ = g_strdup ("");
	_result_ = _tmp2_;
	_tmp3_ = contents;
	_tmp5_ = _tmp4_ = g_strsplit (_tmp3_, "\n", 0);
	lines = _tmp5_;
	lines_length1 = _vala_array_length (_tmp4_);
	_lines_size_ = lines_length1;
	{
		gint i = 0;
		i = 0;
		{
			gboolean _tmp6_ = FALSE;
			_tmp6_ = TRUE;
			while (TRUE) {
				gchar** _tmp8_;
				gint _tmp8__length1;
				const gchar* line = NULL;
				gchar** _tmp9_;
				gint _tmp9__length1;
				const gchar* _tmp10_;
				gboolean _tmp11_ = FALSE;
				gboolean _tmp12_ = FALSE;
				const gchar* _tmp13_;
				gboolean is_stanza = FALSE;
				const gchar* _tmp28_;
				gchar* _tmp29_;
				if (!_tmp6_) {
					gint _tmp7_;
					_tmp7_ = i;
					i = _tmp7_ + 1;
				}
				_tmp6_ = FALSE;
				_tmp8_ = lines;
				_tmp8__length1 = lines_length1;
				if (!(i < _tmp8__length1)) {
					break;
				}
				_tmp9_ = lines;
				_tmp9__length1 = lines_length1;
				_tmp10_ = _tmp9_[i];
				line = _tmp10_;
				_tmp13_ = line;
				if (g_str_has_prefix (_tmp13_, "{")) {
					const gchar* _tmp14_;
					_tmp14_ = line;
					_tmp12_ = g_str_has_suffix (_tmp14_, "}");
				} else {
					_tmp12_ = FALSE;
				}
				if (_tmp12_) {
					_tmp11_ = TRUE;
				} else {
					gboolean _tmp15_ = FALSE;
					const gchar* _tmp16_;
					_tmp16_ = line;
					if (g_str_has_prefix (_tmp16_, "[")) {
						const gchar* _tmp17_;
						_tmp17_ = line;
						_tmp15_ = g_str_has_suffix (_tmp17_, "]");
					} else {
						_tmp15_ = FALSE;
					}
					_tmp11_ = _tmp15_;
				}
				is_stanza = _tmp11_;
				if (!is_stanza) {
					const gchar* _tmp18_;
					const gchar* _tmp19_;
					gchar* _tmp20_;
					gchar* _tmp21_;
					gchar* _tmp22_;
					_tmp18_ = _result_;
					_tmp19_ = line;
					_tmp20_ = deja_dup_log_obscurer_replace_freeform_text (obscurer, _tmp19_);
					_tmp21_ = _tmp20_;
					_tmp22_ = g_strconcat (_tmp18_, _tmp21_, NULL);
					_g_free0 (_result_);
					_result_ = _tmp22_;
					_g_free0 (_tmp21_);
				} else {
					const gchar* _tmp23_;
					const gchar* _tmp24_;
					gchar* _tmp25_;
					gchar* _tmp26_;
					gchar* _tmp27_;
					_tmp23_ = _result_;
					_tmp24_ = line;
					_tmp25_ = deja_dup_restic_logger_replace_json (obscurer, _tmp24_);
					_tmp26_ = _tmp25_;
					_tmp27_ = g_strconcat (_tmp23_, _tmp26_, NULL);
					_g_free0 (_result_);
					_result_ = _tmp27_;
					_g_free0 (_tmp26_);
				}
				_tmp28_ = _result_;
				_tmp29_ = g_strconcat (_tmp28_, "\n", NULL);
				_g_free0 (_result_);
				_result_ = _tmp29_;
			}
		}
	}
	result = _result_;
	lines = (_vala_array_free (lines, lines_length1, (GDestroyNotify) g_free), NULL);
	_g_free0 (contents);
	return result;
}

gchar*
deja_dup_restic_logger_get_cachefile (void)
{
	gchar* cachedir = NULL;
	const gchar* _tmp0_;
	gchar* _tmp1_;
	const gchar* _tmp2_;
	const gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* result;
	_tmp0_ = g_get_user_cache_dir ();
	_tmp1_ = g_strdup (_tmp0_);
	cachedir = _tmp1_;
	_tmp2_ = cachedir;
	if (_tmp2_ == NULL) {
		result = NULL;
		_g_free0 (cachedir);
		return result;
	}
	_tmp3_ = cachedir;
	_tmp4_ = g_build_filename (_tmp3_, PACKAGE, "restic.log", NULL);
	result = _tmp4_;
	_g_free0 (cachedir);
	return result;
}

static gchar*
deja_dup_restic_logger_read_cachefile (void)
{
	GError* _inner_error0_ = NULL;
	gchar* result;
	{
		gchar* contents = NULL;
		gchar* _tmp0_;
		gchar* _tmp1_;
		gchar* _tmp2_ = NULL;
		_tmp0_ = deja_dup_restic_logger_get_cachefile ();
		_tmp1_ = _tmp0_;
		g_file_get_contents (_tmp1_, &_tmp2_, NULL, &_inner_error0_);
		_g_free0 (contents);
		contents = _tmp2_;
		_g_free0 (_tmp1_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			_g_free0 (contents);
			goto __catch0_g_error;
		}
		result = contents;
		return result;
	}
	goto __finally0;
	__catch0_g_error:
	{
		g_clear_error (&_inner_error0_);
		result = NULL;
		return result;
	}
	__finally0:
	g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
	g_clear_error (&_inner_error0_);
	return NULL;
}

static JsonNode*
_vala_JsonNode_copy (JsonNode* self)
{
	return g_boxed_copy (json_node_get_type (), self);
}

static gpointer
__vala_JsonNode_copy0 (gpointer self)
{
	return self ? _vala_JsonNode_copy (self) : NULL;
}

static void
_vala_JsonNode_free (JsonNode* self)
{
	g_boxed_free (json_node_get_type (), self);
}

static gchar*
deja_dup_restic_logger_replace_json (DejaDupLogObscurer* obscurer,
                                     const gchar* line)
{
	JsonParser* parser = NULL;
	JsonParser* _tmp0_;
	JsonNode* root = NULL;
	JsonParser* _tmp3_;
	JsonNode* _tmp4_;
	JsonNode* _tmp5_;
	JsonNode* _tmp6_;
	JsonNode* _tmp7_;
	gchar* _tmp8_;
	GError* _inner_error0_ = NULL;
	gchar* result;
	g_return_val_if_fail (obscurer != NULL, NULL);
	g_return_val_if_fail (line != NULL, NULL);
	_tmp0_ = json_parser_new ();
	parser = _tmp0_;
	{
		JsonParser* _tmp1_;
		_tmp1_ = parser;
		json_parser_load_from_data (_tmp1_, line, (gssize) -1, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			goto __catch0_g_error;
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		gchar* _tmp2_;
		g_clear_error (&_inner_error0_);
		_tmp2_ = deja_dup_log_obscurer_replace_freeform_text (obscurer, line);
		result = _tmp2_;
		_g_object_unref0 (parser);
		return result;
	}
	__finally0:
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		_g_object_unref0 (parser);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error0_->message, g_quark_to_string (_inner_error0_->domain), _inner_error0_->code);
		g_clear_error (&_inner_error0_);
		return NULL;
	}
	_tmp3_ = parser;
	_tmp4_ = json_parser_get_root (_tmp3_);
	_tmp5_ = __vala_JsonNode_copy0 (_tmp4_);
	root = _tmp5_;
	_tmp6_ = root;
	deja_dup_restic_logger_replace_node (obscurer, _tmp6_);
	_tmp7_ = root;
	_tmp8_ = json_to_string (_tmp7_, FALSE);
	result = _tmp8_;
	__vala_JsonNode_free0 (root);
	_g_object_unref0 (parser);
	return result;
}

static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

static Block9Data*
block9_data_ref (Block9Data* _data9_)
{
	g_atomic_int_inc (&_data9_->_ref_count_);
	return _data9_;
}

static void
block9_data_unref (void * _userdata_)
{
	Block9Data* _data9_;
	_data9_ = (Block9Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data9_->_ref_count_)) {
		_g_object_unref0 (_data9_->obscurer);
		g_slice_free (Block9Data, _data9_);
	}
}

static void
___lambda60_ (Block9Data* _data9_,
              JsonObject* obj,
              const gchar* name,
              JsonNode* n)
{
	g_return_if_fail (obj != NULL);
	g_return_if_fail (name != NULL);
	g_return_if_fail (n != NULL);
	deja_dup_restic_logger_replace_node (_data9_->obscurer, n);
}

static void
____lambda60__json_object_foreach (JsonObject* object,
                                   const gchar* member_name,
                                   JsonNode* member_node,
                                   gpointer self)
{
	___lambda60_ (self, object, member_name, member_node);
}

static void
____lambda61_ (Block9Data* _data9_,
               JsonArray* arr,
               guint i,
               JsonNode* n)
{
	g_return_if_fail (arr != NULL);
	g_return_if_fail (n != NULL);
	deja_dup_restic_logger_replace_node (_data9_->obscurer, n);
}

static void
_____lambda61__json_array_foreach (JsonArray* array,
                                   guint index_,
                                   JsonNode* element_node,
                                   gpointer self)
{
	____lambda61_ (self, array, index_, element_node);
}

static gchar
string_get (const gchar* self,
            glong index)
{
	gchar _tmp0_;
	gchar result;
	g_return_val_if_fail (self != NULL, '\0');
	_tmp0_ = ((gchar*) self)[index];
	result = _tmp0_;
	return result;
}

static void
deja_dup_restic_logger_replace_node (DejaDupLogObscurer* obscurer,
                                     JsonNode* node)
{
	Block9Data* _data9_;
	DejaDupLogObscurer* _tmp0_;
	JsonNodeType node_type = 0;
	g_return_if_fail (obscurer != NULL);
	g_return_if_fail (node != NULL);
	_data9_ = g_slice_new0 (Block9Data);
	_data9_->_ref_count_ = 1;
	_tmp0_ = _g_object_ref0 (obscurer);
	_g_object_unref0 (_data9_->obscurer);
	_data9_->obscurer = _tmp0_;
	node_type = json_node_get_node_type (node);
	if (node_type == JSON_NODE_OBJECT) {
		JsonObject* _tmp1_;
		_tmp1_ = json_node_get_object (node);
		json_object_foreach_member (_tmp1_, ____lambda60__json_object_foreach, _data9_);
	} else {
		if (node_type == JSON_NODE_ARRAY) {
			JsonArray* _tmp2_;
			_tmp2_ = json_node_get_array (node);
			json_array_foreach_element (_tmp2_, _____lambda61__json_array_foreach, _data9_);
		} else {
			if (node_type == JSON_NODE_VALUE) {
				gchar* strval = NULL;
				const gchar* _tmp3_;
				gchar* _tmp4_;
				gboolean _tmp5_ = FALSE;
				const gchar* _tmp6_;
				_tmp3_ = json_node_get_string (node);
				_tmp4_ = g_strdup (_tmp3_);
				strval = _tmp4_;
				_tmp6_ = strval;
				if (_tmp6_ != NULL) {
					const gchar* _tmp7_;
					_tmp7_ = strval;
					_tmp5_ = string_get (_tmp7_, (glong) 0) == '/';
				} else {
					_tmp5_ = FALSE;
				}
				if (_tmp5_) {
					const gchar* _tmp8_;
					gchar* _tmp9_;
					gchar* _tmp10_;
					_tmp8_ = strval;
					_tmp9_ = deja_dup_log_obscurer_replace_path (_data9_->obscurer, _tmp8_);
					_tmp10_ = _tmp9_;
					json_node_set_string (node, _tmp10_);
					_g_free0 (_tmp10_);
				}
				_g_free0 (strval);
			}
		}
	}
	block9_data_unref (_data9_);
	_data9_ = NULL;
}

DejaDupResticLogger*
deja_dup_restic_logger_construct (GType object_type)
{
	DejaDupResticLogger * self = NULL;
	self = (DejaDupResticLogger*) g_object_new (object_type, NULL);
	return self;
}

DejaDupResticLogger*
deja_dup_restic_logger_new (void)
{
	return deja_dup_restic_logger_construct (DEJA_DUP_TYPE_RESTIC_LOGGER);
}

static void
deja_dup_restic_logger_class_init (DejaDupResticLoggerClass * klass,
                                   gpointer klass_data)
{
	deja_dup_restic_logger_parent_class = g_type_class_peek_parent (klass);
}

static void
deja_dup_restic_logger_instance_init (DejaDupResticLogger * self,
                                      gpointer klass)
{
}

 G_GNUC_NO_INLINE static GType
deja_dup_restic_logger_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (DejaDupResticLoggerClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) deja_dup_restic_logger_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DejaDupResticLogger), 0, (GInstanceInitFunc) deja_dup_restic_logger_instance_init, NULL };
	GType deja_dup_restic_logger_type_id;
	deja_dup_restic_logger_type_id = g_type_register_static (G_TYPE_OBJECT, "DejaDupResticLogger", &g_define_type_info, 0);
	return deja_dup_restic_logger_type_id;
}

GType
deja_dup_restic_logger_get_type (void)
{
	static gsize deja_dup_restic_logger_type_id__once = 0;
	if (g_once_init_enter (&deja_dup_restic_logger_type_id__once)) {
		GType deja_dup_restic_logger_type_id;
		deja_dup_restic_logger_type_id = deja_dup_restic_logger_get_type_once ();
		g_once_init_leave (&deja_dup_restic_logger_type_id__once, deja_dup_restic_logger_type_id);
	}
	return deja_dup_restic_logger_type_id__once;
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

static gssize
_vala_array_length (gpointer array)
{
	gssize length;
	length = 0;
	if (array) {
		while (((gpointer*) array)[length]) {
			length++;
		}
	}
	return length;
}

