Title: "REBOL/Services Client Test Shell"
	Version: 1.0.1
	Author: "Gabriele Santilli"
	File: %shell.r

lns-port: none

print [system/script/header/title system/script/header/version]

ctx-services/debug-enable: false

print "Type HELP for more information."

help-menu: {
Local Commands:
	QUIT                - Quit the shell
	LOGIN [user [pass]] - Login as user
	LOGOUT              - Logout
	HELP                - This help message
	RESET               - Reset connection
	LOCAL expr          - Evaluate expression locally

Useful Remote Commands:
	INFO                - General info about services
	COMMANDS            - Discovery of services commands
	DATE                - Date/time/zone of server
	SYSTEM              - REBOL version
	LANGUAGE            - Human interface language

Any other command is sent to the server, along with arguments. For example:

	ECHO "How ya doing?"
	ECHO [%file $10.00]
	INFO all
	INFO title

	func [
		"Tries to DO code, then DOes on-error if it fails" [throw]
		code [block!]
		on-error [block!] "The word 'error will refer to the error"
		/local result
	] [
		on-error: func [[throw] error [error!]] on-error
		either error? set/any 'result try code [on-error :result] [get/any 'result]

	func [
		"Forms an error message"
		errobj [object!] "Disarmed error"
		/all "Use the same format as the REBOL console"
		/local errtype text
	] [
		errtype: get in system/error get in errobj 'type
		text: get in errtype get in errobj 'id
		if block? text [text: reform bind/copy text in errobj 'self]
		either all [
			rejoin [
				"** " get in errtype 'type ": " text newline
				either get in errobj 'where [join "** Where: " [mold get in errobj 'where newline]] [""]
				either get in errobj 'near [join "** Near: " [mold/only get in errobj 'near newline]] [""]
		] [

lns-port: open-service lns-url

	func [cmd] [
		bind cmd system/words
		set/any 'cmd if-error cmd [
			prin form-error/all disarm error
		if value? 'cmd [print ["L===" mold cmd]]

forever [
	request: to block! ask "LNS> "
	if not parse request [
		['q | 'quit] (quit)
		'login set user opt string! set pass opt string! (
			user: any [user ask "Username? "]
			pass: any [pass ask/hide "Password? "]
			if not login-service lns-port user pass [
				print "***Unable to log in!"
		'logout (logout-service lns-port)
		'reset (attempt [close-service lns-port lns-port: open-service lns-url])
		['! | 'local] copy cmd to end (do-local cmd)
		'help (print help-menu)
		end ; empty command line, ignore
	] [
		if-error [
			result: do-service lns-port request
			either all [result/1 = 'done result/3 = 'ok] [
				print ["====" mold/only next result/4]
			] [
				print ["!!!!" mold result]
		] [
			prin form-error/all disarm error