/* backup.c generated by valac 0.16.0, the Vala compiler
 * generated from backup.vala, do not modify */


#include <glib.h>
#include <glib-object.h>
#include <gio/gio.h>
#include <SQLHeavy.h>
#include <stdlib.h>
#include <string.h>

#define _g_main_loop_unref0(var) ((var == NULL) ? NULL : (var = (g_main_loop_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
typedef struct _CopyData CopyData;

struct _CopyData {
	int _state_;
	GObject* _source_object_;
	GAsyncResult* _res_;
	GSimpleAsyncResult* _async_result;
	GMainLoop* loop;
	const gchar* _tmp0_;
	gchar* _tmp1_;
	gchar* _tmp2_;
	SQLHeavyDatabase* _tmp3_;
	SQLHeavyDatabase* _tmp4_;
	SQLHeavyDatabase* src;
	SQLHeavyDatabase* _tmp5_;
	SQLHeavyDatabase* dest;
	SQLHeavyDatabase* _tmp6_;
	SQLHeavyDatabase* _tmp7_;
	SQLHeavyBackup* _tmp8_;
	SQLHeavyBackup* backup;
	SQLHeavyBackup* _tmp9_;
	GError* e;
	const gchar* _tmp10_;
	GMainLoop* _tmp11_;
	GError * _inner_error_;
};



static void copy_data_free (gpointer _data);
void copy (GMainLoop* loop, GAsyncReadyCallback _callback_, gpointer _user_data_);
void copy_finish (GAsyncResult* _res_);
static gboolean copy_co (CopyData* _data_);
static void copy_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_);
gint _vala_main (gchar** args, int args_length1);


static void copy_data_free (gpointer _data) {
	CopyData* _data_;
	_data_ = _data;
	_g_main_loop_unref0 (_data_->loop);
	g_slice_free (CopyData, _data_);
}


static gpointer _g_main_loop_ref0 (gpointer self) {
	return self ? g_main_loop_ref (self) : NULL;
}


void copy (GMainLoop* loop, GAsyncReadyCallback _callback_, gpointer _user_data_) {
	CopyData* _data_;
	GMainLoop* _tmp0_;
	GMainLoop* _tmp1_;
	_data_ = g_slice_new0 (CopyData);
	_data_->_async_result = g_simple_async_result_new (g_object_newv (G_TYPE_OBJECT, 0, NULL), _callback_, _user_data_, copy);
	g_simple_async_result_set_op_res_gpointer (_data_->_async_result, _data_, copy_data_free);
	_tmp0_ = loop;
	_tmp1_ = _g_main_loop_ref0 (_tmp0_);
	_data_->loop = _tmp1_;
	copy_co (_data_);
}


void copy_finish (GAsyncResult* _res_) {
	CopyData* _data_;
	_data_ = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (_res_));
}


static void copy_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_) {
	CopyData* _data_;
	_data_ = _user_data_;
	_data_->_source_object_ = source_object;
	_data_->_res_ = _res_;
	copy_co (_data_);
}


static gboolean copy_co (CopyData* _data_) {
	switch (_data_->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	{
		_data_->_tmp0_ = NULL;
		_data_->_tmp0_ = g_get_home_dir ();
		_data_->_tmp1_ = NULL;
		_data_->_tmp1_ = g_build_filename (_data_->_tmp0_, ".liferea_1.6", "liferea.db", NULL);
		_data_->_tmp2_ = _data_->_tmp1_;
		_data_->_tmp3_ = sql_heavy_database_new (_data_->_tmp2_, SQL_HEAVY_FILE_MODE_READ, &_data_->_inner_error_);
		_data_->_tmp4_ = _data_->_tmp3_;
		_g_free0 (_data_->_tmp2_);
		_data_->src = _data_->_tmp4_;
		if (_data_->_inner_error_ != NULL) {
			if (_data_->_inner_error_->domain == SQL_HEAVY_ERROR) {
				goto __catch0_sql_heavy_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error_->message, g_quark_to_string (_data_->_inner_error_->domain), _data_->_inner_error_->code);
			g_clear_error (&_data_->_inner_error_);
			return FALSE;
		}
		_data_->_tmp5_ = sql_heavy_database_new ("liferea.db", (SQL_HEAVY_FILE_MODE_READ | SQL_HEAVY_FILE_MODE_WRITE) | SQL_HEAVY_FILE_MODE_CREATE, &_data_->_inner_error_);
		_data_->dest = _data_->_tmp5_;
		if (_data_->_inner_error_ != NULL) {
			_g_object_unref0 (_data_->src);
			if (_data_->_inner_error_->domain == SQL_HEAVY_ERROR) {
				goto __catch0_sql_heavy_error;
			}
			_g_object_unref0 (_data_->src);
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error_->message, g_quark_to_string (_data_->_inner_error_->domain), _data_->_inner_error_->code);
			g_clear_error (&_data_->_inner_error_);
			return FALSE;
		}
		_data_->_tmp6_ = _data_->src;
		_data_->_tmp7_ = _data_->dest;
		_data_->_tmp8_ = sql_heavy_backup_new (_data_->_tmp6_, _data_->_tmp7_, &_data_->_inner_error_);
		_data_->backup = _data_->_tmp8_;
		if (_data_->_inner_error_ != NULL) {
			_g_object_unref0 (_data_->dest);
			_g_object_unref0 (_data_->src);
			if (_data_->_inner_error_->domain == SQL_HEAVY_ERROR) {
				goto __catch0_sql_heavy_error;
			}
			_g_object_unref0 (_data_->dest);
			_g_object_unref0 (_data_->src);
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error_->message, g_quark_to_string (_data_->_inner_error_->domain), _data_->_inner_error_->code);
			g_clear_error (&_data_->_inner_error_);
			return FALSE;
		}
		_data_->_tmp9_ = _data_->backup;
		_data_->_state_ = 1;
		sql_heavy_backup_execute_async (_data_->_tmp9_, NULL, copy_ready, _data_);
		return FALSE;
		_state_1:
		sql_heavy_backup_execute_finish (_data_->_tmp9_, _data_->_res_, &_data_->_inner_error_);
		if (_data_->_inner_error_ != NULL) {
			_g_object_unref0 (_data_->backup);
			_g_object_unref0 (_data_->dest);
			_g_object_unref0 (_data_->src);
			if (_data_->_inner_error_->domain == SQL_HEAVY_ERROR) {
				goto __catch0_sql_heavy_error;
			}
			_g_object_unref0 (_data_->backup);
			_g_object_unref0 (_data_->dest);
			_g_object_unref0 (_data_->src);
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error_->message, g_quark_to_string (_data_->_inner_error_->domain), _data_->_inner_error_->code);
			g_clear_error (&_data_->_inner_error_);
			return FALSE;
		}
		_g_object_unref0 (_data_->backup);
		_g_object_unref0 (_data_->dest);
		_g_object_unref0 (_data_->src);
	}
	goto __finally0;
	__catch0_sql_heavy_error:
	{
		_data_->e = _data_->_inner_error_;
		_data_->_inner_error_ = NULL;
		_data_->_tmp10_ = _data_->e->message;
		g_error ("backup.vala:9: Error: %s", _data_->_tmp10_);
		_g_error_free0 (_data_->e);
	}
	__finally0:
	if (_data_->_inner_error_ != NULL) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _data_->_inner_error_->message, g_quark_to_string (_data_->_inner_error_->domain), _data_->_inner_error_->code);
		g_clear_error (&_data_->_inner_error_);
		return FALSE;
	}
	_data_->_tmp11_ = _data_->loop;
	g_main_loop_quit (_data_->_tmp11_);
	if (_data_->_state_ == 0) {
		g_simple_async_result_complete_in_idle (_data_->_async_result);
	} else {
		g_simple_async_result_complete (_data_->_async_result);
	}
	g_object_unref (_data_->_async_result);
	return FALSE;
}


gint _vala_main (gchar** args, int args_length1) {
	gint result = 0;
	GMainLoop* _tmp0_;
	GMainLoop* loop;
	_tmp0_ = g_main_loop_new (NULL, FALSE);
	loop = _tmp0_;
	copy (loop, NULL, NULL);
	g_main_loop_run (loop);
	result = 0;
	_g_main_loop_unref0 (loop);
	return result;
}


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



