<?xl version="0.1" encoding="UTF-8" ?>

<Sequence> ^Env0
<Define> ^gv-load ()
<Arguments>
	^ses
	^entry-url
</Arguments>
<Sequence> ()


	<Define> ^meta-info
		<GetElement>
		<RemoteSession type="normal"> ^ses
			<URL> ^entry-url </URL>
			<Command> Get </Command>
			<Get mode="meta">
			 ($ (+ SessionPath SessionResource)) </Get>
		</RemoteSession>
		^'meta
		</GetElement>
	</Define>
	<Define> ^resource-category
		(GetElement (GetElement meta-info 'category) 1)
	</Define>
	<Define> ^load-list
		(Next
		(GetElement gv-resource-list
			([Cast type="symbol"] resource-category)))
	</Define>
	<Define> ^target-url () </Define>
	<For> (Not (= load-list ()))
	<Sequence> ()
		<Define> ^type
			(GetElement (GetElement load-list 0) 0)
		</Define>
		<set> ^target-url
			(GetElement meta-info
				'file
				"type" type)
		</set>
		<If> (Not (= target-url ()))
		<Then><Break>()</Break></Then>
		</If>
		<set> ^load-list (Next load-list) </set>
	</Sequence>
	</For>
	<If> (Not (= target-url ()))
	<Then><Sequence> ()
		<If> (= (ListLength target-url) 1)
		<Then><set> ^target-url ^entry-url </set> </Then>
		<Else><set> ^target-url 
			(ComposeURL entry-url (GetElement target-url 1)) </set>
		</Else>
		</If>

		<set> ^load-list
			(GetElement load-list 0)
		</set>
		<Define> ^rid
			<gv-new-resource>
				(GetElement load-list 2)
				^entry-url ^target-url ^meta-info
			</gv-new-resource>
		</Define>
		<If> (Not (= (GetElement load-list 1) ()))
		<Then>
		<Eval><CurrentEnvironment/>
			<quote>
				(($ (GetElement load-list 1))
				ses rid entry-url target-url meta-info)
			</quote>
		</Eval>
		</Then>
		</If>
		^rid
	</Sequence></Then>
	<Else> -1
	</Else>
	</If>
</Sequence>
</Define>

<Define> ^gv-load-coordinate ()
<Arguments> ^ses ^rid ^entry-url ^target-url ^meta-info </Arguments>
<Sequence> ()
	<gv-resource rid="^rid">
		<gv-c-link type="children">
			<RemoteSession type="normal"> ^ses
				<URL> (+ entry-url ".chi") </URL>
				<Command> Get </Command>
				<Get> ($ (+ SessionPath SessionResource))
				</Get>
			</RemoteSession>
		</gv-c-link>
	</gv-resource>
</Sequence>
</Define>

<Define> ^GetLastElement ()
<Arguments> ^list </Arguments>
<Sequence> ()
	<For> (Not (= (Next list) ()))
		<set> ^list (Next list) </set>
	</For>
	<GetElement> ^list 0 </GetElement>
</Sequence>
</Define>

<Define> ^gv-load-target ()
<Arguments> ^ses ^rid ^entry-url ^target-url ^meta-info </Arguments>
<Sequence> ()

	<gv-resource rid="^rid">
		<Eval><CurrentEnvironment/>
		<GetLastElement>
		<RemoteSession type="normal"> ^ses
			<URL> ^target-url </URL>
			<Command> Get </Command>
			<Get> ($ (+ SessionPath SessionResource))
			</Get>
		</RemoteSession>
		</GetLastElement>
		</Eval>
	</gv-resource>
</Sequence>
</Define>

<Define> ^gv-load-entry ()
<Arguments> ^ses ^rid ^entry-url ^target-url ^meta-info </Arguments>
<Sequence> ()
	<gv-resource rid="^rid">
		<Eval><CurrentEnvironment/>
		<GetLastElement>
		<RemoteSession type="normal"> ^ses
			<URL> ^entry-url </URL>
			<Command> Get </Command>
			<Get> ($ (+ SessionPath SessionResource))
			</Get>
		</RemoteSession>
		</GetLastElement>
		</Eval>
	</gv-resource>
</Sequence>
</Define>

<Define Order="Normal"> ^meta ^s 0 </Define>
<Define> ^point ^List </Define>

</Sequence>


<Sequence> ^Env0
<Define> ^gv-resource-list <quote>
	(
		<coordinate>
			("e2d" gv-load-coordinate ($ rt-coordinate))
		</coordinate>
		<map>
			("xl" gv-load-target ($ rt-map))
		</map>
		<luster>

			("cr" () ($ rt-luster.r64))

			("r64" () ($ rt-luster.r64))
		</luster>
		<vector>
			("pdb" gv-load-entry  ($ rt-vector))
			("xl" gv-load-target ($ rt-vector))
		</vector>
	)
</quote></Define>
</Sequence>
