# <PRE>
# Copyright for the Uniform Repository Service (c) 1995 - 2025
# by Gerald Banon. All rights reserved.
# Version 2.1
# solve.tcl
#
# If the document is in the current local collection, it is returned,
# otherwise it is searched within the scope of the whole URLib
# collection and of the local bibliographic mirror
# (unless the call is done from another mirror) and the
# document corresponding to the official version or the most recent
# metadata last update is returned.
#
# Example:
# http://mtc-m12.sid.inpe.br/urn:doi:10.17487/RFC8141?ibiurl.clientinformation.citingitem=urlib.net/www/2023/06.03.21.17
# http://gjfb0520.sid.inpe.br/urn:doi:10.1590/0001-3765202320210807?ibiurl.clientinformation.citingitem=urlib.net/www/2023/11.16.13.37
# this sort of URL must call the solve cgi script

proc Solve {} {
if [catch {
	global env
	
	set ibiResolver $env(SERVER_NAME):$env(SERVER_PORT)
	set nidXresolverArray(upn:4CR88AP) $ibiResolver	;# added by GJFB in 2025-06-11
	set nidXresolverArray(upn:4CR88AP-) $ibiResolver	;# added by GJFB in 2025-06-11
	set nidXresolverArray(urn:doi) doi.org	;# added by GJFB in 2023-12-26
	set nidXresolverArray(purl) purl.org
	
	regexp {^/(.+:.+):(.+)$} $env(PATH_INFO) m resolverID destination
	
	if {[info exists env(QUERY_STRING)] && [string compare {} $env(QUERY_STRING)] != 0} {
		set queryString ?$env(QUERY_STRING)
	} else {
		set queryString {}
	}

if 0 {	 	
	puts {Content-Type: text/html}
	puts {}
	puts $env(PATH_INFO)	;# => /urn:doi:10.17487/RFC8141

	puts <BR>
	puts $resolverID		;# => urn:doi
	puts <BR>
	puts $destination		;# 10.17487/RFC8141
	
	puts <BR>
	puts --$queryString--	;# => --?ibiurl.clientinformation.citingitem=urlib.net/www/2023/06.03.21.17--
}

if [string equal {urn:ibi} $resolverID] {
	puts "Location: http://$ibiResolver/customizeerror.cgi/602"
	puts ""
}

	set nameSpaceID $resolverID	;# added by GJFB in 2025-06-29
	
if 0 {	 	
	puts {Content-Type: text/html}
	puts {}
	puts --$resolverID--
	puts --$nameSpaceID--
# => --urn:doi-- --urn:doi-- Location: http://doi.org/10.1590/0001-3765202320210807?ibiurl.clientinformation.citingitem=urlib.net/www/2023/11.16.13.37&linktype=relative 

}
	
	if [regexp {upn:4CR88AP} $nameSpaceID] {
# upn:4CR88AP
		set link $nidXresolverArray($resolverID)/$nameSpaceID:$destination$queryString
	} elseif {[regexp {urn:doi} $nameSpaceID]} {	;# added by GJFB in 2023-11-11 to display a copy (if any) of the item identified by its doi - added by GJFB in 2025-06-29
# urn:doi
		global serverAddress	;# used by SetFieldValue
		set col ../../../../..
		set URLibServiceRepository $env(URLIB_SERVICE_REP)
		source ../$col/$URLibServiceRepository/doc/utilities1.tcl
		source ../$col/$URLibServiceRepository/doc/utilitiesMirror.tcl
		source ../$col/$URLibServiceRepository/doc/cgi/mirrorfind-.tcl
# serverAddress
		set serverAddress [list $env(SERVER_NAME) $env(URLIB_PORT)]
		
		set resultList [FindMetadataRepositories "doi $destination" 0 {} {} no no 1]
if 0 {	 	
	puts {Content-Type: text/html}
	puts {}
	puts --$resultList--
}		
		if [string equal {} $resultList] {
# no copies of the item identified by its doi were found
			set link $nidXresolverArray($resolverID)/$destination$queryString	;# use doi
		} else {
# a copy was found
			package require http	;# added by GJFB in 2025-06-29
			if [catch {http::geturl http://doi.org/} token] {	;# if added by GJFB in 2025-06-29
# site not found - use the copy
				foreach {site rep-i} [lindex $resultList 0] {break}			
#				puts --$site--
#				puts --${rep-i}--
				SetFieldValue $site ${rep-i} identifier
#				puts --$identifier--
				set link $nidXresolverArray(upn:4CR88AP)/upn:4CR88AP:$identifier$queryString	;# use ibi copy
			} else {
# use the original - added by GJFB in 2025-06-29
				http::cleanup $token
				set link $nidXresolverArray($resolverID)/$destination	;# use doi
			}
		}		
	} else {
# ibi-, rep-, 4CR88AP-, purl...
		set link $nidXresolverArray($resolverID)/$destination$queryString
	}

if 0 {
# testing 'Example of robust hypertext and authentic data' (QABCDSTQQW/4AEFPDB)	when GetOptimizedListOfSites was executed using MultipleExecute
	set homePath $env(DOCUMENT_ROOT)
	set col ../../../../..
	set URLibServiceRepository $env(URLIB_SERVICE_REP)
	source ../$col/$URLibServiceRepository/doc/utilities1.tcl
	if [file exists $homePath/fileFlag] {
#		set xWaitQueue 0; after 1000 {set xWaitQueue 1}; vwait xWaitQueue
		set xWaitQueue 0; after 2000 {set xWaitQueue 1}; vwait xWaitQueue	;# OK
	} else {
		set x x
		Store x $homePath/fileFlag
	}
}
	
# location http://doi.org/10.17487/RFC8141 gerado em solve
	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 

	} m] {
	if ![string equal {} $m] {
		puts {Content-Type: text/html}
		puts {}
		puts "<PRE>from: $m</PRE>"
		if 0 {global errorInfo; puts <PRE>$errorInfo</PRE>}
	}
}
}
# </PRE>
