# Copyright for the Uniform Repository Service (c) 1995 - 2023, 2026
# by Gerald Banon. All rights reserved.
# Version 2.1
# createpage.tcl
# creates a dynamic HTML page
# to see the page deposited in the repository iconet.com.br/banon/2003/01.02.18.11:
# http://gjfb:1905/iconet.com.br/banon/2003/01.02.18.11
# http://gjfb:1905/ibi/iconet.com.br/banon/2003/01.02.18.11
# call to this script is mounted in the CreateAbsolutePath procedure
# http://gjfb:1905/createpage.cgi/iconet.com.br/banon/2003/01.02.18.11/doc/tclPage.htm
# loCoInRep is a special case:
# http://gjfb:1905/dpi.inpe.br/banon/1999/01.09.22.14 - there is a redirection to col/urlib.net/www/2014/03.16.03.40/auxdoc/cgi/script.cgi by mean of a ScriptAlias and no updating
# http://gjfb:1905/ibi/dpi.inpe.br/banon/1999/01.09.22.14 - there is no redirection and updating works
# http://gjfb:1905/createpage.cgi/dpi.inpe.br/banon/1999/01.09.22.14/doc/index.html - updating works

proc CreatePage {} {
if [catch {
	global env
	global currentRep	;# set in this proc
	global language languageRep1 languageRep2
	global storeTclPage	;# set by TestForTclPageUpdate
	global cgi
	global homePath	;# used in SynchronizeRepository (sourceDisplayControl), TestForTclPageUpdate, CreateAlternatePath and CreateOutput (página dos clippings - nenhum clipping foi encontrada))
	global sourceDisplayControl	;# used by start.html in some submission form repositories (e.g., INPE submission forms, Claiming papers (see iconet.com.br/banon/2007/02.25.17.01/doc/enClaimPage.html))
	global col	;# used by start.html in some submission form repositories
	global mirrorHomePageRepository	;# used by start.html in some submission form repositories (e.g., INPE submission forms, Claiming papers)
	global submissionFormRep	;# used by start.html in some submission form repositories (e.g., INPE submission forms, Claiming papers)
	global submissionFormLanguageRep
	global cssFileURL
	global thisRepository	;# used in CreateTclPageFile, DisplaySearch and DisplayMultipleSearch
	global URLibServiceRepository ;# used in SynchronizeRepository (sourceDisplayControl), CreateTclPageFile
	global targetFile 	;# used in CreateTclPageFile
	global targetFileDirname	;# used in CreateTclPage (needed when using DisplayNumberOfEntries)
	global targetFileRootName	;# used in CreateTclPage (needed when using DisplayNumberOfEntries)
	global targetFileExtension	;# used in CreateTclPage (needed when using DisplayNumberOfEntries)
	global targetFileType 	;# used in CreateTclPageFile and DisplaySearch
	global tcl_platform
	global serverAddress ;# used in CreateTclPage
	global serverAddressWithIP ;# used in CreateTclPage and CreateTclPageFile
	global conferenceAcronym	;# set by sourceDisplayControl; used by Sending author notification, through CreateTclPage
	global defaultTable	;# set by sourceDisplayControl; used by Sending author notification, through CreateTclPage
	global programCommitteeChairEMailAddressTable	;# set by sourceDisplayControl; used by Sending author notification, through CreateTclPage
	global cameraReadySubmissionDeadlineTable	;# set by sourceDisplayControl; used by Sending author notification, through CreateTclPage
	global conferenceHomePage	;# set by sourceDisplayControl; used by Sending author notification, through CreateTclPage
	global SMTPServer	;# set by sourceDisplayControl; used by Sending author notification, through CreateTclPage
	global numberOfReviewersPerWorkTable	;# set by sourceDisplayControl; used by Sending author notification, through CreateTclPage
	global claimingDeadlineTable	;# set by sourceDisplayControl; used by Reviewer page for claiming papers, through CreateTclPage
	global numberOfWorksPerReviewerTable	;# set by sourceDisplayControl; used by Reviewer page for claiming papers, through CreateTclPage
	global alternateUserTable	;# set by sourceDisplayControl; used by Reviewer page for claiming papers, through CreateTclPage
	global localSite	;# used by CreateTclPageFile
	global serverAddressWithIP	;# used by SynchronizeRepository (sourceDisplayControl)
	global loCoInRep	;# used by SynchronizeRepository (sourceDisplayControl)
	global loBiMiRep	;# used while executing SynchronizeRepository (sourceDisplayControl)
	global clientServerAddressWithIP	;# used in CreateTclPage
	global progressKey	;# used in CreateTclPage

	set col ../../../../..
	set URLibServiceRepository $env(URLIB_SERVICE_REP)
	source ../$col/$URLibServiceRepository/doc/utilities1.tcl	;# set sourceDisplayControl
	source ../$col/$URLibServiceRepository/doc/cgi/mirrorfind-.tcl

if 0 {
	puts {Content-Type: text/html}
	puts {}
	puts OK
}

# mirrorHomePageRepository
# used in sourceDisplayControl
	set mirrorHomePageRepository dpi.inpe.br/banon/2000/01.23.20.24

# homePath (used in CreateOutput and FindLanguage)
	set homePath $env(DOCUMENT_ROOT)
# serverAddress (used in CreateTclPage only))
	set serverAddress [list $env(SERVER_NAME) $env(URLIB_PORT)]
# serverAddressWithIP (used in CreateTclPage, CreateTclPageFile and SynchronizeRepository only)
	set serverAddressWithIP [list $env(IP_ADDR) $env(URLIB_PORT)]
# localSite
#	set localSite $env(SERVER_NAME):$env(SERVER_PORT)	;# commented by GJFB on 2015-08-31
	set localSite [ReturnHTTPHost]	;# added by GJFB on 2015-08-31 to be like in Submit (see cgi/submit.tcl)
# loCoInRep
	set loCoInRep $env(LOCOINREP)	;# used in displayControl.tcl
# loCoInId
	ConditionalSet loCoInId env(LOCOINID) {}	;# used in this procedure only - old site might have a Local Index Collection without ibip or ibin
# loBiMiRep
	set loBiMiRep $env(LOBIMIREP)
# thisRepository
	regsub -all { } [lrange [file split $env(PATH_INFO)] 1 4] {/} thisRepository
# puts $thisRepository

# Capture URL
	set q_u_e_r_y {}
	if {[info exists env(QUERY_STRING)] && ![string equal {} $env(QUERY_STRING)]} {
# GET style
# puts $env(REQUEST_URI)
if 0 {
## added by GJFB on 2017-10-06 to allow urlib.net to access the archive services through their respective IBI
# commented by GJFB on 2023-03-07 otherwise CreateTclPageFile below is not executed and the local index cannot be updated based on the schedule (@schedule.tcl)
#		if [regexp /createpage.cgi/($loCoInRep|$loCoInId) $env(REQUEST_URI)] #
		if {[regexp /createpage.cgi/$loCoInRep $env(REQUEST_URI)] || (![string equal {} $loCoInId] && [regexp /createpage.cgi/$loCoInId $env(REQUEST_URI)])} {
# || added by GJFB on 2017-10-06 to allow the use of IBIp or IBIn when accesssing the archive service
			puts "Location: http://$localSite/col/urlib.net/www/2014/03.16.03.40/doc/script.cgi?$env(QUERY_STRING)"
			puts ""
			return
		}
}
		lappend q_u_e_r_y $env(QUERY_STRING)
	} else {
if 0 {
# Page by page
		if [regexp /createpage.cgi/($loCoInRep|$loCoInId) $env(REQUEST_URI)] {	;# added by GJFB on 2017-10-06 to allow the use of IBIp or IBIn to accesss the archive service
			puts "Location: http://$localSite/rep-/$loCoInRep/index1.html"
			puts ""
			return
		}
} else {
# All in one page
}
	}
	if [info exists env(CONTENT_LENGTH)] {
# POST style
		lappend q_u_e_r_y [string trim [read stdin $env(CONTENT_LENGTH)] \n]
	}
	set q_u_e_r_y [join $q_u_e_r_y &]
	foreach {name value} [split $q_u_e_r_y &=] {
		set cgi([DecodeURL $name]) [DecodeURL $value]
	}
# Capture URL - end

#	ConditionalSet currentRep cgi(mirror) $loBiMiRep
	ConditionalSet currentRep cgi(requiredmirror) $loBiMiRep
	if ![file isdirectory $homePath/col/$currentRep] {set currentRep $loBiMiRep}	;# cgi(requiredmirror) may be from another site - added by GJFB on 2012-01-19

# Find the language and the language repository
	foreach {language languageRep1 languageRep2 firstLanguageRep \
	submissionFormRep submissionFormLanguage submissionFormLanguageRep} \
	[FindLanguage $currentRep] {break}
# Find the language and the language repository - end

# cssFileURL
#	ConditionalSet cssFileURL cgi(cssfileurl) http://$localSite/col/$submissionFormLanguageRep/doc/mirrorStandard.css
	ConditionalSet cssFileURL cgi(cssfileurl) http://$localSite/col/$languageRep1/doc/mirrorStandard.css
# clientServerAddressWithIP
#	ConditionalSet clientServerAddressWithIP cgi(clientserveraddresswithip) {}	;# commented by GJFB on 2022-06-12 - the Execute button on the above horizontal menu doesn't work because it doesn't provide cgi(clientserveraddresswithip)
	ConditionalSet clientServerAddressWithIP cgi(clientserveraddresswithip) $serverAddressWithIP	;# added by GJFB on 2022-06-12 - required to get the Execute button on the above horizontal menu working
# progressKey
	ConditionalSet progressKey cgi(progresskey) {}

if 0 {
	puts {Content-Type: text/html}
	puts {}
	puts [info exists env(CONTENT_LENGTH)]
	puts $q_u_e_r_y
	puts --$clientServerAddressWithIP--
	puts $language
	return
}
# set xxx "CreatePage: language = $language"
# Store xxx C:/tmp/bbb.txt binary 0 a

# set xxx "CreatePage: cgi(languagebutton) = $cgi(languagebutton)"
# Store xxx C:/tmp/bbb.txt binary 0 a

#	source ../$col/$languageRep2/doc/mirror/${language}FieldName.tcl	;# for full format
# now within slave interperter

# Source displayControl.tcl
	set enableOutput 1
	eval $sourceDisplayControl
# Source displayControl.tcl - end

# targetFile
#	set targetFile [join [lrange [file split $env(PATH_INFO)] 6 end] {/}]	;# commented by GJFB on 2015-01-23 - may not be the target file when in case like http://gjfb.home/createpage.cgi/urlib.net/www/2015/01.23.02.17/doc/2015/tclPage.txt
	set thisRepositoryMetadataRep [Execute $serverAddressWithIP [list FindMetadataRep $thisRepository]]
	SetFieldValue $serverAddressWithIP $thisRepositoryMetadataRep-0 {targetfile}
	set targetFile $targetfile
# puts --$targetFile-- 
	set targetFileDirname [file dirname $targetFile]	;# used in CreateTclPage (needed when using DisplayNumberOfEntries)
	set targegFileTail [file tail $targetFile]
	set targetFileRootName [file rootname $targegFileTail]	;# used in CreateTclPage (needed when using DisplayNumberOfEntries)
	set targetFileExtension [file extension $targegFileTail]	;# used in CreateTclPage (needed when using DisplayNumberOfEntries)
# targetFileType
	set targetFileType [string trimleft $targetFileExtension .]	;# (e.g., tex) used in CreateTclPageFile and DisplaySearch
	set path $homePath/col$env(PATH_INFO)
	set path [encoding convertfrom utf-8 $path]	;# encoding convertfrom utf-8 {a b Ã§ d @} -> a b ç d @
	set alternatePath [CreateAlternatePath $path]
	set targetFileAbsolutePath $homePath/col/$thisRepository/doc/$targetFile
	
# example: running http://gjfb.home/createpage.cgi/urlib.net/www/2015/01.23.02.17/doc/2014/tclPage.html results in:
# path
# => c:/users/gerald banon/urlib 2/col/urlib.net/www/2015/01.23.02.17/doc/2014/tclPage.html
# targetFileAbsolutePath
# => c:/users/gerald banon/urlib 2/col/urlib.net/www/2015/01.23.02.17/doc/tclPage.html
	
#	if {[TestForTclPageUpdate $thisRepository $path $alternatePath] || ![file exists $alternatePath]} #
	if {[TestForTclPageUpdate $thisRepository $targetFileAbsolutePath $alternatePath] || ![file exists $alternatePath]} {
		if {$storeTclPage || [regexp -nocase {tex} $targetFileType]} {
# store
# CREATE PAGE
# puts {creating page ...}
			CreateTclPageFile $path $alternatePath $language $languageRep2
		} else {
# don't store
			set fileContent [CreateTclPageFile $path $alternatePath $language $languageRep2]
			puts {Content-Type: text/html}
			puts {}
# puts "$path $alternatePath $language $languageRep2"
# puts OK2
			puts $fileContent
			return
		}
	}
# puts $env(PATH_INFO)
	set alternatePathInfo [CreateAlternatePath [encoding convertfrom utf-8 $env(PATH_INFO)]]
# puts $alternatePathInfo
# => /urlib.net/www/2010/09.29.20.13/doc/@indice.htm
# the test below is needed in case of a path with some accents (e.g., RX_Coração_artigo2/radiografia_doença_cardíaca.tex)
	if {$tcl_platform(platform) == "unix"} {
		puts "Location: [ConvertURLToHexadecimal http://$env(SERVER_NAME):$env(SERVER_PORT)/col$alternatePathInfo]"
	} else {
#		puts "Location: http://$env(SERVER_NAME):$env(SERVER_PORT)/col$alternatePathInfo"	;# commented by GJFB on 2015-01-24
#		puts "Location: http://$env(HTTP_HOST):$env(SERVER_PORT)/col$alternatePathInfo"	;# added by GJFB on 2015-01-24 - HTTP_HOST == gjfb.home and SERVER_NAME == gjfb (the latter doesn't work with http::geturl in running Source)
		puts "Location: http://$env(SERVER_NAME):$env(SERVER_PORT)/col$alternatePathInfo"	;# added by GJFB on 2016-04-09 - HTTP_HOST = gjfb.home:1905 and SERVER_NAME = gjfb.home
	}
	puts ""
} m] {
	if {[string compare {} $m] != 0} {
		puts {Content-Type: text/html}
		puts {}
		puts <PRE>
		puts $m
		if 0 {global errorInfo; puts $errorInfo}
		puts </PRE>
	}
}

}
