/* umockdev-run.c generated by valac 0.18.1, the Vala compiler
 * generated from umockdev-run.vala, do not modify */


#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <umockdev.h>
#include <glib/gstdio.h>
#include <unistd.h>
#include <errno.h>

#define _g_option_context_free0(var) ((var == NULL) ? NULL : (var = (g_option_context_free (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (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)))


extern gchar** opt_load;
gchar** opt_load = NULL;
extern gchar** opt_ioctl;
gchar** opt_ioctl = NULL;
extern gchar** opt_program;
gchar** opt_program = NULL;

gint _vala_main (gchar** args, int args_length1);
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func);
static gint _vala_array_length (gpointer array);

const GOptionEntry options[4] = {{"load", 'l', 0, G_OPTION_ARG_FILENAME_ARRAY, &opt_load, "Load an umockdev-record file into the testbed. Can be specified multip" \
"le times.", "filename"}, {"ioctl", 'i', 0, G_OPTION_ARG_FILENAME_ARRAY, &opt_ioctl, "Load an ioctl record file into the testbed. Can be specified multiple " \
"times.", "devname=ioctlfilename"}, {"", (gchar) 0, 0, G_OPTION_ARG_STRING_ARRAY, &opt_program, "", ""}, {NULL}};

gint _vala_main (gchar** args, int args_length1) {
	gint result = 0;
	GOptionContext* _tmp0_;
	GOptionContext* oc;
	UMockdevTestbed* _tmp5_;
	UMockdevTestbed* testbed;
	gchar** _tmp6_;
	gint _tmp6__length1;
	gchar** _tmp18_;
	gint _tmp18__length1;
	gchar** _tmp34_;
	gint _tmp34__length1;
	gchar** _tmp36_;
	gint _tmp36__length1;
	const gchar* _tmp37_;
	gchar** _tmp38_;
	gint _tmp38__length1;
	FILE* _tmp39_;
	gint _tmp40_;
	const gchar* _tmp41_ = NULL;
	GError * _inner_error_ = NULL;
	_tmp0_ = g_option_context_new ("-- program [args..]");
	oc = _tmp0_;
	g_option_context_set_summary (oc, "Run a program under an umockdev testbed.");
	g_option_context_add_main_entries (oc, options, NULL);
	{
		g_option_context_parse (oc, &args_length1, &args, &_inner_error_);
		if (_inner_error_ != NULL) {
			if (_inner_error_->domain == G_OPTION_ERROR) {
				goto __catch0_g_option_error;
			}
			_g_option_context_free0 (oc);
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return 0;
		}
	}
	goto __finally0;
	__catch0_g_option_error:
	{
		GError* e = NULL;
		FILE* _tmp1_;
		const gchar* _tmp2_;
		gchar** _tmp3_;
		gint _tmp3__length1;
		const gchar* _tmp4_;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp1_ = stderr;
		_tmp2_ = e->message;
		_tmp3_ = args;
		_tmp3__length1 = args_length1;
		_tmp4_ = _tmp3_[0];
		fprintf (_tmp1_, "Error: %s\nRun %s --help for how to use this program\n", _tmp2_, _tmp4_);
		exit (1);
		_g_error_free0 (e);
	}
	__finally0:
	if (_inner_error_ != NULL) {
		_g_option_context_free0 (oc);
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return 0;
	}
	g_setenv ("LD_PRELOAD", "libumockdev-preload.so.0", TRUE);
	_tmp5_ = umockdev_testbed_new ();
	testbed = _tmp5_;
	_tmp6_ = opt_load;
	_tmp6__length1 = _vala_array_length (opt_load);
	{
		gchar** path_collection = NULL;
		gint path_collection_length1 = 0;
		gint _path_collection_size_ = 0;
		gint path_it = 0;
		path_collection = _tmp6_;
		path_collection_length1 = _tmp6__length1;
		for (path_it = 0; path_it < _tmp6__length1; path_it = path_it + 1) {
			gchar* _tmp7_;
			gchar* path = NULL;
			_tmp7_ = g_strdup (path_collection[path_it]);
			path = _tmp7_;
			{
				gchar* record = NULL;
				{
					const gchar* _tmp8_;
					gchar* _tmp9_ = NULL;
					_tmp8_ = path;
					g_file_get_contents (_tmp8_, &_tmp9_, NULL, &_inner_error_);
					_g_free0 (record);
					record = _tmp9_;
					if (_inner_error_ != NULL) {
						if (_inner_error_->domain == G_FILE_ERROR) {
							goto __catch1_g_file_error;
						}
						_g_free0 (record);
						_g_free0 (path);
						_g_object_unref0 (testbed);
						_g_option_context_free0 (oc);
						g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
						g_clear_error (&_inner_error_);
						return 0;
					}
				}
				goto __finally1;
				__catch1_g_file_error:
				{
					GError* e = NULL;
					FILE* _tmp10_;
					const gchar* _tmp11_;
					const gchar* _tmp12_;
					e = _inner_error_;
					_inner_error_ = NULL;
					_tmp10_ = stderr;
					_tmp11_ = path;
					_tmp12_ = e->message;
					fprintf (_tmp10_, "Error: Cannot open %s: %s\n", _tmp11_, _tmp12_);
					exit (1);
					_g_error_free0 (e);
				}
				__finally1:
				if (_inner_error_ != NULL) {
					_g_free0 (record);
					_g_free0 (path);
					_g_object_unref0 (testbed);
					_g_option_context_free0 (oc);
					g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
					g_clear_error (&_inner_error_);
					return 0;
				}
				{
					UMockdevTestbed* _tmp13_;
					const gchar* _tmp14_;
					_tmp13_ = testbed;
					_tmp14_ = record;
					umockdev_testbed_add_from_string (_tmp13_, _tmp14_, &_inner_error_);
					if (_inner_error_ != NULL) {
						goto __catch2_g_error;
					}
				}
				goto __finally2;
				__catch2_g_error:
				{
					GError* e = NULL;
					FILE* _tmp15_;
					const gchar* _tmp16_;
					const gchar* _tmp17_;
					e = _inner_error_;
					_inner_error_ = NULL;
					_tmp15_ = stderr;
					_tmp16_ = path;
					_tmp17_ = e->message;
					fprintf (_tmp15_, "Error: Invalid record file %s: %s\n", _tmp16_, _tmp17_);
					exit (1);
					_g_error_free0 (e);
				}
				__finally2:
				if (_inner_error_ != NULL) {
					_g_free0 (record);
					_g_free0 (path);
					_g_object_unref0 (testbed);
					_g_option_context_free0 (oc);
					g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
					g_clear_error (&_inner_error_);
					return 0;
				}
				_g_free0 (record);
				_g_free0 (path);
			}
		}
	}
	_tmp18_ = opt_ioctl;
	_tmp18__length1 = _vala_array_length (opt_ioctl);
	{
		gchar** i_collection = NULL;
		gint i_collection_length1 = 0;
		gint _i_collection_size_ = 0;
		gint i_it = 0;
		i_collection = _tmp18_;
		i_collection_length1 = _tmp18__length1;
		for (i_it = 0; i_it < _tmp18__length1; i_it = i_it + 1) {
			gchar* _tmp19_;
			gchar* i = NULL;
			_tmp19_ = g_strdup (i_collection[i_it]);
			i = _tmp19_;
			{
				const gchar* _tmp20_;
				gchar** _tmp21_;
				gchar** _tmp22_ = NULL;
				gchar** parts;
				gint parts_length1;
				gint _parts_size_;
				_tmp20_ = i;
				_tmp22_ = _tmp21_ = g_strsplit (_tmp20_, "=", 2);
				parts = _tmp22_;
				parts_length1 = _vala_array_length (_tmp21_);
				_parts_size_ = parts_length1;
				{
					UMockdevTestbed* _tmp23_;
					gchar** _tmp24_;
					gint _tmp24__length1;
					const gchar* _tmp25_;
					gchar** _tmp26_;
					gint _tmp26__length1;
					const gchar* _tmp27_;
					_tmp23_ = testbed;
					_tmp24_ = parts;
					_tmp24__length1 = parts_length1;
					_tmp25_ = _tmp24_[0];
					_tmp26_ = parts;
					_tmp26__length1 = parts_length1;
					_tmp27_ = _tmp26_[1];
					umockdev_testbed_load_ioctl (_tmp23_, _tmp25_, _tmp27_, &_inner_error_);
					if (_inner_error_ != NULL) {
						if (_inner_error_->domain == G_FILE_ERROR) {
							goto __catch3_g_file_error;
						}
						parts = (_vala_array_free (parts, parts_length1, (GDestroyNotify) g_free), NULL);
						_g_free0 (i);
						_g_object_unref0 (testbed);
						_g_option_context_free0 (oc);
						g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
						g_clear_error (&_inner_error_);
						return 0;
					}
				}
				goto __finally3;
				__catch3_g_file_error:
				{
					GError* e = NULL;
					FILE* _tmp28_;
					gchar** _tmp29_;
					gint _tmp29__length1;
					const gchar* _tmp30_;
					gchar** _tmp31_;
					gint _tmp31__length1;
					const gchar* _tmp32_;
					const gchar* _tmp33_;
					e = _inner_error_;
					_inner_error_ = NULL;
					_tmp28_ = stderr;
					_tmp29_ = parts;
					_tmp29__length1 = parts_length1;
					_tmp30_ = _tmp29_[1];
					_tmp31_ = parts;
					_tmp31__length1 = parts_length1;
					_tmp32_ = _tmp31_[0];
					_tmp33_ = e->message;
					fprintf (_tmp28_, "Error: Cannot install %s for device %s: %s\n", _tmp30_, _tmp32_, _tmp33_);
					exit (1);
					_g_error_free0 (e);
				}
				__finally3:
				if (_inner_error_ != NULL) {
					parts = (_vala_array_free (parts, parts_length1, (GDestroyNotify) g_free), NULL);
					_g_free0 (i);
					_g_object_unref0 (testbed);
					_g_option_context_free0 (oc);
					g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
					g_clear_error (&_inner_error_);
					return 0;
				}
				parts = (_vala_array_free (parts, parts_length1, (GDestroyNotify) g_free), NULL);
				_g_free0 (i);
			}
		}
	}
	_tmp34_ = opt_program;
	_tmp34__length1 = _vala_array_length (opt_program);
	if (_tmp34__length1 == 0) {
		FILE* _tmp35_;
		_tmp35_ = stderr;
		fprintf (_tmp35_, "No program specified. See --help for how to use umockdev-run\n");
		exit (1);
	}
	_tmp36_ = opt_program;
	_tmp36__length1 = _vala_array_length (opt_program);
	_tmp37_ = _tmp36_[0];
	_tmp38_ = opt_program;
	_tmp38__length1 = _vala_array_length (opt_program);
	execvp (_tmp37_, _tmp38_);
	_tmp39_ = stderr;
	_tmp40_ = errno;
	_tmp41_ = g_strerror (_tmp40_);
	fprintf (_tmp39_, "Cannot run program: %s\n", _tmp41_);
	result = 1;
	_g_object_unref0 (testbed);
	_g_option_context_free0 (oc);
	return result;
}


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


static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	if ((array != NULL) && (destroy_func != NULL)) {
		int 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, gint array_length, GDestroyNotify destroy_func) {
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}


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



