#
# Copyright for the Uniform Repository Service (c) 1995 - 2000, 2021, 2023, 2024, 2025
# by Gerald Banon. All rights reserved.
# Version 2.1
# from.tcl
#
# The document is searched within the collection defined by the local bibliographic mirror
# (unless the call is done from another mirror). If it is not found the it is search im the whole URLib collection.
# The document corresponding to the official version or the most recent
# metadata last update is returned.
#
# Example of:
# /col/inpe.br/gb/1998/08.02.08.56/doc/goto-/inpe.br/gb/1998/08.08.09.10
# PATH_INFO = /inpe.br/gb/1998/08.02.08.56/to-/inpe.br/gb/1998/08.08.09.10

# see reserved file names in iconet.com.br/banon/2008/12.11.01.03 (Testando nome de arquivo e sequência de pastas/arquivo reservados)

proc From {} {
if [catch {
## http://mtc-m12.sid.inpe.br/col/urlib.net/www/2023/06.03.21.17/doc/upn:4CR88AP:8JMKD3MGP3W34R/44C25PS
## /urlib.net/www/2023/06.03.21.17/to/upn:4CR88AP/8JMKD3MGP3W34R/44C25PS
# http://mtc-m12.sid.inpe.br/col/urlib.net/www/2023/06.03.21.17/doc/urn:doi:10.17487/RFC8141
# /urlib.net/www/2023/06.03.21.17/to/urn:doi/10.17487/RFC8141

## http://gjfb:1905/col/iconet.com.br/banon/2001/04.28.19.50/doc/urlib:4CR88AP:QABCDSTQQW/4BT93R5?forcehistorybackflag=1
## /iconet.com.br/banon/2001/04.28.19.50/to/urlib:4CR88AP/QABCDSTQQW/4BT93R5?forcehistorybackflag=1
# http://gjfb0520.sid.inpe.br/col/urlib.net/www/2023/11.16.13.37/doc/urn:doi:10.1590/0001-3765202320210807
# /urlib.net/www/2023/11.16.13.37/to/urn:doi/10.1590/0001-3765202320210807

	global env

# ibiResolver (local site)	
	set ibiResolver $env(SERVER_NAME):$env(SERVER_PORT)
	
	if ![regexp {/([^/]*/[^/]*/[^/]*/[^/]*)/to/([^/]*)/([^/]*/[^/]*/[^/]*/[^/]*.*)$} $env(PATH_INFO) m source resolverID destination] {
		regexp {/([^/]*/[^/]*/[^/]*/[^/]*)/to/([^/]*)/([^/]*/[^/]*.*)$} $env(PATH_INFO) m source resolverID destination	;# added by GJFB in 2021-11-17 to resolve urn
	}
	
if 0 {
	puts {Content-Type: text/html}
	puts {}
	puts $env(PATH_INFO)
	puts $resolverID
}
##	regsub {goto} $resolverID {upn:4CR88AP} resolverID	;# nameSpaceID value is upn:4CR88AP- or upn:4CR88AP
#	regsub {goto} $resolverID {ibi} resolverID	;# URI prefix value is ibi- or ibi
##	regsub {^ibi(-?)$} $resolverID {upn:4CR88AP\1} resolverID	;# nameSpaceID value is upn:4CR88AP- or upn:4CR88AP

	if {[info exists env(QUERY_STRING)] && [string compare {} $env(QUERY_STRING)] != 0} {
#		set queryString ?$env(QUERY_STRING)	;# commented by GJFB in 2021-10-08
#		set queryString ?$env(QUERY_STRING)&linktype=relative	;# added by GJFB in 2021-10-08 - linktype is used in ReturnURLPropertyList2	- commented by GJFB in 2024-01-04
		set queryString ?$env(QUERY_STRING)&ibiurl.clientinformation.citingitem=$source&linktype=relative	;# added by GJFB in 2021-10-08 - linktype is used in ReturnURLPropertyList2	- added by GJFB in 2024-01-04
	} else {
#		set queryString {}	;# commented by GJFB in 2021-10-08
#		set queryString ?linktype=relative	;# added by GJFB in 2021-10-08 - linktype is used in ReturnURLPropertyList2	- commented by GJFB in 2024-01-04
		set queryString ?ibiurl.clientinformation.citingitem=$source&linktype=relative	;# added by GJFB in 2021-10-08 - linktype is used in ReturnURLPropertyList2	- added by GJFB in 2024-01-04
	}

	if [regsub {goto} $resolverID {ibi} resolverID] {	;# resolverID value is ibi- or ibi
		set link $ibiResolver/$resolverID/$destination$queryString	;# traditional link
	} else {
		set link $ibiResolver/$resolverID:$destination$queryString	;# to be solved in 'resolve'
	}
	
#	puts "Location: http://mtc-m12.sid.inpe.br/urn:doi:10.17487/RFC8141	;# to be solved in 'resolve'
#	puts "Location: http://gjfb0520.sid.inpe.br/10.1590/urn:doi:0001-3765202320210807?	; to be solved in 'resolve'
	puts "Location: http://$link"
	puts ""
# => Location: http://doi.org/10.1590/0001-3765202320210807?ibiurl.clientinformation.citingitem=urlib.net/www/2023/11.16.13.37&linktype=relative 
## => Location: http://gjfb:1905/upn:4CR88AP:urlib.net/www/2024/08.26.03.05?forcehistorybackflag=1&ibiurl.clientinformation.citingitem=iconet.com.br/banon/2001/04.28.19.50&linktype=relative
## => Location: http://gjfb:1905/upn:4CR88AP:QABCDSTQQW/4BT93R5?forcehistorybackflag=1&ibiurl.clientinformation.citingitem=iconet.com.br/banon/2001/04.28.19.50&linktype=relative
## => Location: http://gjfb:1905/upn:4CR88AP-:QABCDSTQQW/4BT93R5?forcehistorybackflag=1&ibiurl.clientinformation.citingitem=iconet.com.br/banon/2001/04.28.19.50&linktype=relative
} m] {
	if ![string equal {} $m] {
		puts {Content-Type: text/html}
		puts {}
		puts "
From: $m
" if 0 {global errorInfo; puts
$errorInfo
} } } } #