/* Generated by Nim Compiler v0.11.2 */
/*   (c) 2015 Andreas Rumpf */
/* The generated code is subject to the original license. */
#define NIM_INTBITS 32
#include "nimbase.h"

#include <dirent.h>

#include <sys/types.h>

#include <string.h>

#include <sys/stat.h>

#include <time.h>
typedef struct NimStringDesc NimStringDesc;
typedef struct TGenericSeq TGenericSeq;
typedef struct Tlineinfo163338 Tlineinfo163338;
typedef struct Tlinkedlist130028 Tlinkedlist130028;
typedef struct Tlistentry130022 Tlistentry130022;
typedef struct Stringtableobj134012 Stringtableobj134012;
typedef struct TNimObject TNimObject;
typedef struct TNimType TNimType;
typedef struct TNimNode TNimNode;
typedef struct Keyvaluepairseq134010 Keyvaluepairseq134010;
typedef struct Keyvaluepair134008 Keyvaluepair134008;
struct  TGenericSeq  {
NI len;
NI reserved;
};
struct  NimStringDesc  {
  TGenericSeq Sup;
NIM_CHAR data[SEQ_DECL_SIZE];
};
struct  Tlineinfo163338  {
NI16 line;
NI16 col;
NI32 fileindex;
};
struct  Tlinkedlist130028  {
Tlistentry130022* head;
Tlistentry130022* tail;
NI counter;
};
typedef NIM_CHAR TY107377[256];
typedef N_NIMCALL_PTR(void, TY3289) (void* p, NI op);
typedef N_NIMCALL_PTR(void*, TY3294) (void* p);
struct  TNimType  {
NI size;
NU8 kind;
NU8 flags;
TNimType* base;
TNimNode* node;
void* finalizer;
TY3289 marker;
TY3294 deepcopy;
};
struct  TNimObject  {
TNimType* m_type;
};
struct  Stringtableobj134012  {
  TNimObject Sup;
NI counter;
Keyvaluepairseq134010* data;
NU8 mode;
};
struct Keyvaluepair134008 {
NimStringDesc* Field0;
NimStringDesc* Field1;
};
struct  Tlistentry130022  {
  TNimObject Sup;
Tlistentry130022* prev;
Tlistentry130022* next;
};
struct  TNimNode  {
NU8 kind;
NI offset;
TNimType* typ;
NCSTRING name;
NI len;
TNimNode** sons;
};
struct Keyvaluepairseq134010 {
  TGenericSeq Sup;
  Keyvaluepair134008 data[SEQ_DECL_SIZE];
};
N_NIMCALL(NIM_BOOL, contains_130219)(Tlinkedlist130028 list, NimStringDesc* data);
N_NIMCALL(void, prependstr_130419)(Tlinkedlist130028* list, NimStringDesc* data);
N_NIMCALL(void, addpathrec_173258)(NimStringDesc* dir, Tlineinfo163338 info);
N_NIMCALL(Stringtableobj134012*, nstnewStringTable)(NU8 mode);
N_NIMCALL(NimStringDesc*, cstrToNimstr)(NCSTRING str);
static N_INLINE(NIM_BOOL, eqStrings)(NimStringDesc* a, NimStringDesc* b);
N_NIMCALL(NimStringDesc*, HEX2F_119292)(NimStringDesc* head, NimStringDesc* tail);
N_NIMCALL(void, addpackage_173126)(Stringtableobj134012* packages, NimStringDesc* p);
N_NIMCALL(NI, versionsplitpos_173023)(NimStringDesc* s);
N_NIMCALL(NimStringDesc*, copyStrLast)(NimStringDesc* s, NI start_76443, NI last);
N_NIMCALL(NimStringDesc*, copyStrLast)(NimStringDesc* s, NI first, NI last);
N_NIMCALL(NimStringDesc*, copyStr)(NimStringDesc* s, NI start);
N_NIMCALL(NimStringDesc*, copyStr)(NimStringDesc* s, NI first);
N_NIMCALL(NIM_BOOL, HEX3CHEX2E_173064)(NimStringDesc* a, NimStringDesc* b);
N_NIMCALL(NI, npuParseInt)(NimStringDesc* s, NI* number, NI start);
N_NIMCALL(NimStringDesc*, nstGet)(Stringtableobj134012* t, NimStringDesc* key);
N_NIMCALL(void, nstPut)(Stringtableobj134012* t, NimStringDesc* key, NimStringDesc* val);
static N_INLINE(void, appendString)(NimStringDesc* dest, NimStringDesc* src);
static N_INLINE(void, appendChar)(NimStringDesc* dest, NIM_CHAR c);
N_NIMCALL(NimStringDesc*, rawNewString)(NI space);
N_NIMCALL(void, addnimblepath_173175)(NimStringDesc* p, Tlineinfo163338 info);
N_NIMCALL(void, message_165188)(Tlineinfo163338 info, NU16 msg, NimStringDesc* arg);
STRING_LITERAL(TMP1659, ".", 1);
STRING_LITERAL(TMP1660, "..", 2);
STRING_LITERAL(TMP1661, "head", 4);
extern Tlinkedlist130028 searchpaths_154132;
extern NI gverbosity_154137;
extern Tlinkedlist130028 lazypaths_154133;

N_NIMCALL(void, addpath_173012)(NimStringDesc* path, Tlineinfo163338 info) {
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = contains_130219(searchpaths_154132, path);
		if (!!(LOC3)) goto LA4;
		prependstr_130419((&searchpaths_154132), path);
	}
	LA4: ;
}

static N_INLINE(NIM_BOOL, eqStrings)(NimStringDesc* a, NimStringDesc* b) {
	NIM_BOOL result;
	NIM_BOOL LOC11;
	int LOC13;
{	result = 0;
	{
		if (!(a == b)) goto LA3;
		result = NIM_TRUE;
		goto BeforeRet;
	}
	LA3: ;
	{
		NIM_BOOL LOC7;
		LOC7 = 0;
		LOC7 = (a == NIM_NIL);
		if (LOC7) goto LA8;
		LOC7 = (b == NIM_NIL);
		LA8: ;
		if (!LOC7) goto LA9;
		result = NIM_FALSE;
		goto BeforeRet;
	}
	LA9: ;
	LOC11 = 0;
	LOC11 = ((*a).Sup.len == (*b).Sup.len);
	if (!(LOC11)) goto LA12;
	LOC13 = 0;
	LOC13 = memcmp(((NCSTRING) ((*a).data)), ((NCSTRING) ((*b).data)), (*a).Sup.len);
	LOC11 = (LOC13 == ((NI32) 0));
	LA12: ;
	result = LOC11;
	goto BeforeRet;
	}BeforeRet: ;
	return result;
}

N_NIMCALL(NI, versionsplitpos_173023)(NimStringDesc* s) {
	NI result;
	result = 0;
	result = (NI)((s ? s->Sup.len : 0) - ((NI) 2));
	{
		while (1) {
			NIM_BOOL LOC3;
			LOC3 = 0;
			LOC3 = (((NI) 1) < result);
			if (!(LOC3)) goto LA4;
			LOC3 = (((NU8)(s->data[result])) >= ((NU8)(48)) && ((NU8)(s->data[result])) <= ((NU8)(57)) || ((NU8)(s->data[result])) == ((NU8)(46)));
			LA4: ;
			if (!LOC3) goto LA2;
			result -= ((NI) 1);
		} LA2: ;
	}
	{
		if (!!(((NU8)(s->data[result]) == (NU8)(45)))) goto LA7;
		result = (s ? s->Sup.len : 0);
	}
	LA7: ;
	return result;
}

N_NIMCALL(NIM_BOOL, HEX3CHEX2E_173064)(NimStringDesc* a, NimStringDesc* b) {
	NIM_BOOL result;
	NI i;
	NI j;
	NI vera;
	NI verb;
{	result = 0;
	{
		if (!eqStrings(a, ((NimStringDesc*) &TMP1661))) goto LA3;
		result = NIM_FALSE;
		goto BeforeRet;
	}
	LA3: ;
	i = ((NI) 0);
	j = ((NI) 0);
	vera = ((NI) 0);
	verb = ((NI) 0);
	{
		while (1) {
			NI ii;
			NI jj;
			ii = npuParseInt(a, (&vera), i);
			jj = npuParseInt(b, (&verb), j);
			{
				NIM_BOOL LOC9;
				LOC9 = 0;
				LOC9 = (ii <= ((NI) 0));
				if (LOC9) goto LA10;
				LOC9 = (jj <= ((NI) 0));
				LA10: ;
				if (!LOC9) goto LA11;
				result = (((NI) 0) < jj);
				goto BeforeRet;
			}
			LA11: ;
			{
				if (!(vera < verb)) goto LA15;
				result = NIM_TRUE;
				goto BeforeRet;
			}
			goto LA13;
			LA15: ;
			{
				if (!(verb < vera)) goto LA18;
				result = NIM_FALSE;
				goto BeforeRet;
			}
			goto LA13;
			LA18: ;
			LA13: ;
			i += ii;
			j += jj;
			{
				if (!((NU8)(a->data[i]) == (NU8)(46))) goto LA22;
				i += ((NI) 1);
			}
			LA22: ;
			{
				if (!((NU8)(b->data[j]) == (NU8)(46))) goto LA26;
				j += ((NI) 1);
			}
			LA26: ;
		}
	}
	}BeforeRet: ;
	return result;
}

N_NIMCALL(void, addpackage_173126)(Stringtableobj134012* packages, NimStringDesc* p) {
	NI x;
	NimStringDesc* name;
	x = versionsplitpos_173023(p);
	name = copyStrLast(p, ((NI) 0), (NI)(x - ((NI) 1)));
	{
		NimStringDesc* version;
		if (!(x < (p ? p->Sup.len : 0))) goto LA3;
		version = copyStr(p, (NI)(x + ((NI) 1)));
		{
			NimStringDesc* LOC7;
			NIM_BOOL LOC8;
			LOC7 = 0;
			LOC7 = nstGet(packages, name);
			LOC8 = 0;
			LOC8 = HEX3CHEX2E_173064(LOC7, version);
			if (!LOC8) goto LA9;
			nstPut(packages, name, version);
		}
		LA9: ;
	}
	goto LA1;
	LA3: ;
	{
		nstPut(packages, name, ((NimStringDesc*) &TMP1661));
	}
	LA1: ;
}

static N_INLINE(void, appendString)(NimStringDesc* dest, NimStringDesc* src) {
	memcpy(((NCSTRING) ((&(*dest).data[((*dest).Sup.len)- 0]))), ((NCSTRING) ((*src).data)), (NI)((*src).Sup.len + ((NI) 1)));
	(*dest).Sup.len += (*src).Sup.len;
}

static N_INLINE(void, appendChar)(NimStringDesc* dest, NIM_CHAR c) {
	(*dest).data[((*dest).Sup.len)- 0] = c;
	(*dest).data[((NI)((*dest).Sup.len + ((NI) 1)))- 0] = 0;
	(*dest).Sup.len += ((NI) 1);
}

N_NIMCALL(void, addnimblepath_173175)(NimStringDesc* p, Tlineinfo163338 info) {
	{
		NIM_BOOL LOC3;
		LOC3 = 0;
		LOC3 = contains_130219(searchpaths_154132, p);
		if (!!(LOC3)) goto LA4;
		{
			if (!(((NI) 1) <= gverbosity_154137)) goto LA8;
			message_165188(info, ((NU16) 273), p);
		}
		LA8: ;
		prependstr_130419((&lazypaths_154133), p);
	}
	LA4: ;
}

N_NIMCALL(void, addpathrec_173258)(NimStringDesc* dir, Tlineinfo163338 info) {
	Stringtableobj134012* packages;
	NI pos;
	packages = nstnewStringTable(((NU8) 2));
	pos = (NI)((dir ? dir->Sup.len : 0) - ((NI) 1));
	{
		if (!(((NU8)(dir->data[pos])) == ((NU8)(47)) || ((NU8)(dir->data[pos])) == ((NU8)(47)))) goto LA3;
		pos += ((NI) 1);
	}
	LA3: ;
	{
		NU8 k_173284;
		NimStringDesc* p_173285;
		DIR* d_173299;
		k_173284 = 0;
		p_173285 = 0;
		d_173299 = opendir(dir->data);
		{
			int LOC43;
			if (!!((d_173299 == NIM_NIL))) goto LA8;
			{
				while (1) {
					struct dirent* x_173301;
					NimStringDesc* y_173303;
					x_173301 = readdir(d_173299);
					{
						if (!(x_173301 == NIM_NIL)) goto LA14;
						goto LA10;
					}
					LA14: ;
					y_173303 = cstrToNimstr(((NCSTRING) ((*x_173301).d_name)));
					{
						NIM_BOOL LOC18;
						struct stat s_173305;
						NU8 k_173307;
						LOC18 = 0;
						LOC18 = !(eqStrings(y_173303, ((NimStringDesc*) &TMP1659)));
						if (!(LOC18)) goto LA19;
						LOC18 = !(eqStrings(y_173303, ((NimStringDesc*) &TMP1660)));
						LA19: ;
						if (!LOC18) goto LA20;
						memset((void*)(&s_173305), 0, sizeof(s_173305));
						y_173303 = HEX2F_119292(dir, y_173303);
						k_173307 = ((NU8) 0);
						{
							int LOC24;
							LOC24 = 0;
							LOC24 = lstat(y_173303->data, (&s_173305));
							if (!(LOC24 < ((NI32) 0))) goto LA25;
							goto LA10;
						}
						LA25: ;
						{
							NIM_BOOL LOC29;
							LOC29 = 0;
							LOC29 = S_ISDIR(s_173305.st_mode);
							if (!LOC29) goto LA30;
							k_173307 = ((NU8) 2);
						}
						LA30: ;
						{
							NIM_BOOL LOC34;
							LOC34 = 0;
							LOC34 = S_ISLNK(s_173305.st_mode);
							if (!LOC34) goto LA35;
							k_173307 = (NU8)(k_173307 + ((NI) 1));
						}
						LA35: ;
						k_173284 = k_173307;
						p_173285 = y_173303;
						{
							NIM_BOOL LOC39;
							LOC39 = 0;
							LOC39 = (k_173284 == ((NU8) 2));
							if (!(LOC39)) goto LA40;
							LOC39 = !(((NU8)(p_173285->data[pos]) == (NU8)(46)));
							LA40: ;
							if (!LOC39) goto LA41;
							addpackage_173126(packages, p_173285);
						}
						LA41: ;
					}
					LA20: ;
				}
			} LA10: ;
			LOC43 = 0;
			LOC43 = closedir(d_173299);
		}
		LA8: ;
	}
	{
		NimStringDesc* p_173295;
		p_173295 = 0;
		{
			NimStringDesc* key_173313;
			NimStringDesc* val_173315;
			key_173313 = 0;
			val_173315 = 0;
			{
				NI h_173317;
				NI HEX3Atmp_173319;
				NI res_173321;
				h_173317 = 0;
				HEX3Atmp_173319 = 0;
				HEX3Atmp_173319 = ((*packages).data ? ((*packages).data->Sup.len-1) : -1);
				res_173321 = ((NI) 0);
				{
					while (1) {
						if (!(res_173321 <= HEX3Atmp_173319)) goto LA48;
						h_173317 = res_173321;
						{
							NimStringDesc* res_173325;
							if (!!((*packages).data->data[h_173317].Field0 == 0)) goto LA51;
							key_173313 = (*packages).data->data[h_173317].Field0;
							val_173315 = (*packages).data->data[h_173317].Field1;
							{
								if (!eqStrings(val_173315, ((NimStringDesc*) &TMP1661))) goto LA55;
								res_173325 = key_173313;
							}
							goto LA53;
							LA55: ;
							{
								NimStringDesc* LOC58;
								LOC58 = 0;
								LOC58 = rawNewString(key_173313->Sup.len + val_173315->Sup.len + 1);
appendString(LOC58, key_173313);
appendChar(LOC58, 45);
appendString(LOC58, val_173315);
								res_173325 = LOC58;
							}
							LA53: ;
							p_173295 = res_173325;
							addnimblepath_173175(p_173295, info);
						}
						LA51: ;
						res_173321 += ((NI) 1);
					} LA48: ;
				}
			}
		}
	}
}

N_NIMCALL(void, nimblepath_173356)(NimStringDesc* path, Tlineinfo163338 info) {
	addpathrec_173258(path, info);
	addnimblepath_173175(path, info);
}
NIM_EXTERNC N_NOINLINE(void, nimsrc_nimblecmdInit)(void) {
}

NIM_EXTERNC N_NOINLINE(void, nimsrc_nimblecmdDatInit)(void) {
}

