# Copyright for URLibService (c) 1995 - 2024,
# by Gerald Banon. All rights reserved.
# Version 2.1
# post (post the URLib local collection - start the servers)
# post -x (unpost - stop the servers)
# post -r (reset - recreate files like auxdoc/.repositoryProperties.tcl, and post the local collection)
# post -f (force - force migration (donīt try to update the remote collection - used when the remote Arquive is down forever)
# post -p gjfb 800 (set the port entry to {gjfb 800} and post the local collection)
# post -c (update all the cgi scripts)
# post -x -u (unpost and download/extract the last version (if any))
# post -o (open access - used for administrator registration at Archive installation)
# post -s (used when doing post from col/urlib.net/www/2024/02.18.15.39/doc/sentinel.tcl

# within cygwin under Windows one can use the command:
# netstat -ano | grep 'TCP    0.0.0.0:'

# puts $argv
# puts [llength $argv]
 
set URLibServiceRepository dpi.inpe.br/banon/1998/08.02.08.56
set URLibServiceMetadataRepository dpi.inpe.br/banon/1999/04.02.15.49	;# used to update metadata in this file
set devLoCoInRep dpi.inpe.br/banon/1999/01.09.22.14	;# used in StartService and DDRoutine
# set downloadingEnvironmentRepository iconet.com.br/banon/2000/12.30.22.40	;# not used any more after 2020-04
set applicationName post

set errorTrace 0	;# value is 0 or 1 - used in ServeLocalCollection, Execute, Submit, MultipleSubmit and CreateEnvironmentArray
set acceptCommunicationCounter 0	;# used in ServeLocalCollection only

# some global variables:
# accentTable	;# used by SetNoMatch (Search.tcl)
# accentTable2	;# used by SetNoMatch (Search.tcl)
# allowedCommandList
## apacheConfDirectoryPath
# apacheIconDirectoryPath
# apacheLibDirectoryPath
# apacheMagicFilePath
# apacheMimeTypesFilePath
# apachePath
# apacheRepository
# applicationName	;# values are: start or post
# bannerPathArray ;# array of pathes for repositories whose service/type is Banner - indices are banner languages
# bannerRoot	;# path of the directory containing files like nextSite, used in CreateBannerSpace and FindBannerPath
# bannerSequenceRepList	;# list of repositories whose service/type is Banner Sequence
# bib2referRepository
# bibpessoal2referRepository
# citationKeyRepository
# col
# commonWords
# defaultDocRepository
# defaultMetadataRepository
# diskSpeed	;# set and use only in GetSiteInformation
# environmentArray
# homePath
# htpasswdPath
# inverseTable
# ipChangeFlag - set in post and used just in InformURLibSystem
# apacheVersion
# linuxDistribution
# loBiMiRep
# localURLibClientSocketId
# loCoInRep
# log
# maximumNumberOfEntries	;# used by RemoveMetadata and by CreateRepArray (to compute mostRecentReferences and mostRecentFullTexts)
# metadataArray
# mostRecentReferences	;# contains the most recent references
# mostRecentFullTexts	;# contains the most recent full texts
# officialIconRepList ;# list of repositories whose service/type is Access Icon, but not the default icon
# pwd
# referenceTypeList
# referRepository
# ${referRepository}::conversionTable
# repositoryListForPost	;# set in UpdateRepositoryListForPost
# saveFlag	;# used in SaveMetadata
# saveMetadata
# searchRepository
# serverAddress	;# banon-pc2 19050; banon-pc2:1905 (old usage)
# ipURLibPort	;# 150.163.2.174 19050; 150.163.2.174;1905 (old usage)
# localSite	;# banon-pc2:1905
# startApacheServer
# startApplicationInUse	;# used by start and post
# tclPath
# unZipPath
# unZipRepository
# URLibServiceRepository
# URLibServiceMetadataRepository
# urlibServiceVersion ;# extracted from the history of the URlibService Repository; set in StartService
# zipPath
# zipRepository

# set processID [list [clock seconds] 0]

# homePath
if ![regexp "(^.*)/col/$URLibServiceRepository/doc" [pwd] m homePath] {
# starting from home
	cd col/$URLibServiceRepository/doc
	regexp "(^.*)/col/$URLibServiceRepository/doc" [pwd] m homePath
}
# homePath, ex: /home/URLib

set applicationNameForReverseEngineering post	;# for reverse engineering only
set applicationRuningTime [clock format [clock seconds] -format %Y:%m.%d.%H.%M.%S]	;# for reverse engineering
set applicationFileName "'$homePath/col/$URLibServiceRepository/doc/post'"	;# for reverse engineering only

# pwd
set pwd [pwd]

# serverRoot and bannerRoot
# bannerRoot is used in CreateEnvironmentArray
if [file exist $homePath/readOnlySite] {
	set serverRoot C:/tmp/URLibService/serverDir 
	file mkdir $serverRoot
	set bannerRoot C:/tmp/URLibService/bannerDir 
	file mkdir $bannerRoot
} else {
	set serverRoot $homePath/col/$URLibServiceRepository/auxdoc/serverDir
	set bannerRoot $homePath/col/$URLibServiceRepository/auxdoc/bannerDir
	file mkdir $bannerRoot
}

package require http	;# needed with tcl 8.5
lappend auto_path $pwd
package require $URLibServiceRepository

if [catch {

# StoreLog {notice} {post (0)} {post is running}	;# added by GJFB in 2020-08-06 to trace unpost-kill-post under infinite loop

# portEntry (ex: gjfb 800)
if ![regexp -- {-[pP] (.*)} $argv m portEntry] {set portEntry {}}
# puts --$portEntry--
 
# Load global variables
LoadGlobalVariables	$portEntry	;# sources environmentArray - set installInitialCollection
# Load global variables - end

# puts >>>$environmentArray(ipAddress)
# puts >>>$serverAddress
# puts >>>$serverAddressWithIP

if ![regexp -- {-([sS]|[uU])} $argv] {	;# added by GJFB in 2024-03-08 - sentinelPID is set in col/urlib.net/www/2024/02.18.15.39/doc/sentinel.tcl
# post is not turned on from sentinel.tcl (-s) or from kill-post (-s) when a 'infinite loop' error is detected or from updateURLibService (-u) - the sentinel.tcl process (if running) must be turned off
	if {$tcl_platform(os) == "Linux"} {
		Load $homePath/col/$URLibServiceRepository/auxdoc/sentinelPID sentinelPID
#		catch {exec kill -9 $sentinelPID}	;# turn off the sentinel.tcl process if running - commented by GJFB in 2024-03-10 - post is killed when using tcl 8.5.7 - works with tcl 8.6.10
		if ![string equal {} $sentinelPID] {catch {exec kill -9 $sentinelPID}}	;# turn off the sentinel.tcl process if running - added by GJFB in 2024-03-10
#		file delete $homePath/col/$URLibServiceRepository/auxdoc/sentinelPID	;# commented by GJFB in 2025-02-16
	}
}

# puts --$zipPath--
if ![regexp -- {-[xX]} $argv] {
# not unpost
	file delete $homePath/col/$URLibServiceRepository/auxdoc/saveAllDataMessageForSentinel	;# added by GJFB in 2024-12-05 - set in post and used in sentinel.tcl (used in Linux) to avoid to trigger the kill process during the execution of the SaveAllData procedure because MultipleExecute in sentinel.tcl may not result in an OK during the execution of the SaveAllData
	file delete $homePath/col/$URLibServiceRepository/auxdoc/infiniteLoopMessageForSentinel	;# added by GJFB in 2024-12-21 - set in sentinel.tcl (used in Linux) and must be deleted at post to avoid running again 'post' immetiately after a 'post' when executing sentinel.tcl
	StoreLog {notice} {post (1b)} {starting URLibService}
	file delete $homePath/col/$URLibServiceRepository/auxdoc/pingIPMessage
#	file delete $homePath/col/$URLibServiceRepository/auxdoc/sessionList	;# added by GJFB in 2023-04-15 for some reason might be corrupted (might result in error while executing OpenSession called by GetURLPropertyList at post) and must be removed - commented by GJFB in 2024-09-13
	if [catch {GetSessionList}] {	;# catch added by GJFB in 2024-09-13 - sessionList should be preserved when it is not corrupted (useful when the ListRecords and ListIdentifiers of the OAI-PMH are in use)
		file delete $homePath/col/$URLibServiceRepository/auxdoc/sessionList	;# for some reason sessionList might be corrupted
	}
	file delete $homePath/col/$URLibServiceRepository/auxdoc/symmetricKeySessionList	;# added by GJFB in 2023-04-15 - There is no need to keep old session list
	
	if {$tcl_platform(platform) == "unix"} {
		if [file owned $homePath/@errorLog] {
			exec chmod g+w $homePath/@errorLog	;# needed by some cgi scripts
		}
		file mkdir $homePath/clipboard3
		if [file owned $homePath/clipboard3] {	;# if added by GJFB in 2020-12-13
			exec chmod g+w $homePath/clipboard3	;# added by GJFB in 2018-03-30 to allow storing returnPathArray.tcl and missingNextHigherUnitIBIList (see Get)
		}
		file mkdir $homePath/clipboard3/=/=/=/=/doc	;# added by GJFB in 2021-02-16 - used in administrator page Script
		if [file owned $homePath/clipboard3/=/=/=/=/doc] {	;# if added by GJFB in 2021-02-16
			exec chmod g+w $homePath/clipboard3/=/=/=/=/doc	;# added by GJFB in 2021-02-16 to allow storing xxx (see Expose coded administrator paswword within the administrator page Script)
		}
	}
	FindInternetAddress	;# updates environmentArray, serverAddress and serverAddressWithIP - the domain name might be wrong and is fixed using InformURLibSystem return
# ipChangeFlag - used in InformURLibSystem only - added by GJFB in 2017-04-28
	set ip [lindex $serverAddressWithIP 0]
	if [file exists $homePath/@previousIP] {
		Load $homePath/@previousIP previousIP
		set ipChangeFlag [expr ![string equal $previousIP $ip]]
	} else {
		set ipChangeFlag 1
	}
	Store ip $homePath/@previousIP	;# added by GJFB in 2017-04-28 - used at next post - useful to maintain the value of staticFlag even when urlib.net server is down
} else {	;# added by GJFB in 2020-02-03 - FindInternetAddress is needed at unpost to update serverAddressWithIP after a change of IP because of a change of Wi-Fi, otherwise unpost fails (see post (14))
# unpost
	FindInternetAddress	;# updates environmentArray, serverAddress and serverAddressWithIP
}

# puts >>>$environmentArray(ipAddress)
# puts >>>$serverAddressWithIP

# pid
# used by Set, Unset, Lappend, Array, Get and ServeLocalCollection
if ![regexp -- {-[xX]} $argv] {	;# added by GJFB in 2020-01-31 - pid is used at post only - it must not be changed at unpost
# not unpost
	set pid [pid]
	Store pid ../auxdoc/pid
}

if {$tcl_platform(os) == "Linux"} {
	if [string equal {iconet.com.br/banon/2005/12.06.21.35} $apacheRepository] {
# Apache 1.3.33 coming from Mandrake at dpi.inpe.br
		if ![file executable $apachePath] {
			if [file owned $apachePath] {
				exec chmod 754 $apachePath
			}
		}
		if ![file executable $htpasswdPath] {
			if [file owned $htpasswdPath] {
				exec chmod 754 $htpasswdPath
			}
		}
	}
}

puts {}

set localSite [ReturnHTTPHost]	;# banon-pc2.dpi.inpe.br or www.urlib.net - uses serverAddress set in LoadGlobalVariables
puts "This is Archive $localSite"

# puts $serverAddressWithIP
# => 192.168.0.112 19050
set localSiteWithIP [ReturnHTTPHost $serverAddressWithIP]
# puts $localSiteWithIP
# => 192.168.0.112:1905
 
set urlibPort [lindex $serverAddress end]

Store urlibPort  $homePath/@urlibPort	;# used in GetServerAddressFromHTTPHost only - added by GJFB in 2014-06-20 - reintroduced by GJFB in 2019-02-22

puts {}

if 0 {
# commented by GJFB in 2020-05-28
# serverRoot2ExistenceFlag - just used in this file
set serverRoot2ExistenceFlag [file exists $homePath/@serverRoot2]	;# added by GJFB in 2016-04-28 - after an installation @serverRoot2 doesn't exist yet
# set xxx $serverRoot2ExistenceFlag
# Store xxx C:/tmp/bbb auto 0 a
}

if {!$installInitialCollection && ![regexp -- {-[xX]} $argv]} {	;# just this line changed by GJFB in 2016-04-28 - [regexp -- {-[xX]} $argv] is to avoid creating @serverRoot2 at unpost after installation

	puts "\[$applicationRuningTime\]"
	
	puts "post pid = $pid"	;# added by GJFB in 2025-04-18 just to display the post process id
#	Store pid $homePath/@postPID

	if [file exists $homePath/col/$loCoInRep/auxdoc/uriToResolverURLArray.tcl] {
		SourceWithBackup $homePath/col/$loCoInRep/auxdoc/uriToResolverURLArray.tcl uriToResolverURLArray 1	;# array set uriToResolverURLArray - added by GJFB in 2025-12-22
	}
		
# Create @serverRoot2 and @serverAddressWithIP
	set convertedURL [ConvertURLToHexadecimal http://$localSiteWithIP/@serverRoot2]
# here, a command like http::geturl $convertedURL -command FindServerRoot -timeout 5000
# is useless - the option -timeout is ignored when the server is off
#	regsub {.$} $urlibPort {0} port	;# 804 -> 800	;# the first ./post command must be the one using port 800 - commented by GJFB in 2021-11-29
	regsub {.$} $urlibPort {} port	;# 804 -> 80	;# added by GJFB in 2021-11-29 - now the ./post commands can executed in any order 
	if ![regexp "0.0.0.0:$port" [exec netstat -an]] {
#		puts "URLibService at <$serverAddress> is not running"	;# commented by GJFB in 2021-11-29
		puts "no URLibService related to port <$port> is running"	;# added by GJFB in 2021-11-29
		if [file exists $homePath/@serverRoot2] {
			Load $homePath/@serverRoot2 serverRoot2
			file delete $serverRoot2/logs/httpd.pid	;# added by GJFB in 2021-11-29 - required in case of unexpected shut down
		}
		set serverRoot2 $serverRoot
		Store serverAddressWithIP $homePath/@serverAddressWithIP	;# used to restart Apache
	} else {
		if [catch {http::geturl $convertedURL} token] {
# Apache is not running
# http::geturl http://150.163.34.244/@serverRoot2	http::geturl doesn't worl properly with tcl 8.4.11 (na mtc-m15)
# http::geturl http://150.163.34.64/@serverRoot2	http::geturl doesn't worl properly with tcl 8.4.11 (na mtc-m15)
#			puts "Apache is not running: $token"	;# commented by GJFB in 2025-08-21
			puts "Apache is not running or $homePath/@serverRoot2 is outdated: $token"	;# added by GJFB in 2025-08-21
			set serverRoot2 $serverRoot
			Store serverAddressWithIP $homePath/@serverAddressWithIP	;# used to restart Apache
		} else {
# Apache is running
			puts "Apache is running at $localSiteWithIP"
			set serverRoot2 [string trim [http::data $token] \n]
			http::cleanup $token
			set convertedURL [ConvertURLToHexadecimal http://$localSiteWithIP/@serverAddressWithIP]
			set token [http::geturl $convertedURL]
			set remoteServerAddressWithIP [string trim [http::data $token] \n]
			http::cleanup $token
			Store remoteServerAddressWithIP $homePath/@serverAddressWithIP	;# used to restart Apache
		}
	}
	Store serverRoot2 $homePath/@serverRoot2
# Create @serverRoot2 and @serverAddressWithIP - end

}

set log .window.main.output.log

source accentTables.tcl	;# used by SetNoMatch (Search.tcl)

SourceLanguage environmentArray

Load ../auxdoc/messageForPost messageForPost	;# added by GJFB in 2020-05-28

if [regexp -- {-[xX]} $argv] {
# unpost
	if [string equal {} $messageForPost] {	;# 'if' added by GJFB in 2020-05-29 to test if the 'start main procedure' is or not installing the inicial ccollection
# not installing the inicial collection
		set applicationName start	;# added by GJFB in 2020-05-29 to let Info using socket
		if ![catch {Info exists startApplicationInUse} flag] {
# flag value may be 0, 1 (Start Application is running) or empty (URLib port in use)
			if {[string equal {} $flag] || $flag} {
# URLib port in use or startApplicationInUse exists
				set message {cannot unpost - Start Application is running or URLib port is in use}
#				puts $message
				puts [StoreLog {alert} {post (2)} $message]
				if [info exists delay5000] {unset delay5000}
				after 5000 {set delay5000 1}; vwait delay5000	;# wait for 5 seconds - required to let the message be displayed
				exit
			}
		}
		set applicationName post	;# added by GJFB in 2020-05-29
	}
	puts {unposting the URLib local collection...}
} else {
# not unpost
	if [string equal {} $zipPath] {
		puts [StoreLog {alert} {post (1a)} {zipPath is empty - zip should be installed - zip is required in MakeDownloadFile (remotely called from SynchronizeRepository through ImportRepository)}]	;# added by GJFB in 2024-10-11 to alert about the lack of zip which could interrupt the submit process
	}
# Testing lastRepTimeInFractionOfSecond
# same code as in CreateIBI
	if {[info tclversion] > 8.4} {
		set currentTimeInFractionOfSecond [expr [clock microseconds] / 1000000.]	;# ct
		Load ../auxdoc/lastRepTimeInFractionOfSecond lastRepTimeInFractionOfSecond
		if [string equal {} $lastRepTimeInFractionOfSecond] {
			set lastRepTimeInFractionOfSecond [expr (entier($currentTimeInFractionOfSecond / $temporalResolution)) / (1 / $temporalResolution) - $temporalResolution]	;# [i] = 1, lrt == rct - 1, therefore s == nrt == rnrt == rct
		}
# same code as in CreateSuffix
		set roundOffCurrentTimeInFractionOfSecond [expr (entier($currentTimeInFractionOfSecond / $temporalResolution)) / (1 / $temporalResolution)]
		set lastRepTimeInFractionOfSecond [expr (entier($lastRepTimeInFractionOfSecond / $temporalResolution)) / (1 / $temporalResolution)]	;# useful because temporalResolution may have changed and its value increased (e.g., from 0.001 to 0.1)
		set numberOfFractionalDigits [expr int(log10(1 / $temporalResolution))]
# MAX
		set newRepTimeInFractionOfSecond [Max [format %.${numberOfFractionalDigits}f [expr $lastRepTimeInFractionOfSecond + $temporalResolution]] $roundOffCurrentTimeInFractionOfSecond]	;# t'
# puts $newRepTimeInFractionOfSecond	;# t'
		set delay [expr $newRepTimeInFractionOfSecond - $currentTimeInFractionOfSecond]
		set delayInMillisecond [expr entier($delay * 1000)]
# AFTER
		if {$delayInMillisecond > 0} {
			puts "posting should be done after $delayInMillisecond milliseconds"
			set x2 0; after 5000 {set x2 1}; vwait x2	;# wait 5 seconds
#			return
			exit	;# added by GJFB in 2022-11-09 - post is the main program
		}
	}
# Testing lastRepTimeInFractionOfSecond - end

#	puts "posting the URLib local collection at <$serverAddress>..."
#	puts "\[$applicationRuningTime\]"
# {} -> serverAddressWithIP (in LoadGlobalVariables with installInitialCollection == 1)
# environmentArray(ipAddress) -> serverAddressWithIP (in FindInternetAddress by calling [GetServerAddress 1])
	file delete ../auxdoc/apacheVersion
	puts "posting the URLib local collection at <$serverAddressWithIP>..."
	if [catch {exec $apachePath -v > ../auxdoc/apacheVersion &} message] {
		puts [StoreLog {alert} {post (3a)} "Apache version not found: $message"]
		puts "to see detailed error message, try to execute the command line: $apachePath -v"
		exit
	}
	set x2 0; after 1000 {set x2 1}; vwait x2	;# wait 1 seconds
	Load ../auxdoc/apacheVersion fileContent	;# ../auxdoc/apacheVersion must not be read immediatly

# Set apacheVersion and linuxDistribution

# puts --$fileContent--
# fileContent =>

# vaio
# Server version: Apache/1.3.27 (Win32)
# Server built:   Oct  3 2002 18:55:42

# banon-pc2
# Server version: Apache/2.0.54
# Server built:   Apr 16 2005 14:25:31

# Server version: Apache/2.2.13 (Unix)
# Server built:   Aug 10 2009 14:15:43

# Server version: Apache/2.2.16 (Unix)
# Server built:   Aug 24 2010 17:05:58

# Server version: Apache/2.2.19 (Unix)
# Server built:   May 25 2011 14:54:12

# Server version: Apache/2.2.22 (Unix)
# Server built:   Feb  7 2012 11:12:33

# md-m09.sid.inpe.br
# plutao.sid.inpe.br
# mtc-m21.sid.inpe.br
# Server version: Apache/2.2.15 (Unix)
# Server built:   Aug 13 2013 17:29:28

# mtc-m12.sid.inpe.br/
# Server version: Apache/2.4.6 (CentOS)
# Server built:   Jul 23 2014 14:48:00

# urlibservice.eng.registro.br
# Server version: Apache/2.4.18 (Ubuntu)	<<< last Apache version compatible with URLibService
# Server built:   2018-06-07T19:43:03

# urlibservice.eng.registro.br
# Server version: Apache/2.4.18 (Ubuntu)
# Server built:   2019-09-16T13:13:53

# urlibservice.eng.registro.br
# Server version: Apache/2.4.18 (Ubuntu)
# Server built:   2020-08-12T21:35:50

# bibdigital.sid.inpe.br
# Server version: Apache/2.2.15 (Unix)
# Server built:   Oct 16 2014 14:48:21

if 0 {
# commented by GJFB in 2021-12-19
#	set apacheVersion {}
	if ![regexp {^Server version: Apache[^/]*/ *([^ \n]*)} $fileContent m apacheVersion] {
		error "CreateConfigurationFiles: --$fileContent-- is an unexpected content for the auxdoc/apacheVersion file - if empty content under Windows 7, it may be required to restart the computer"
	}
# Store apacheVersion C:/tmp/aaa auto 0 a
} else {
# added by GJFB in 2021-12-19
	regexp {Apache/(\d+.\d+)} [lindex $fileContent 2] m apacheVersion
	if {$tcl_platform(os) == "Linux"} {
		regexp {\((.*)\)} [lindex $fileContent 3] m linuxDistribution
	}
	if ![info exists apacheVersion] {
		puts [StoreLog {alert} {post (3b)} "\nunknown Apache version"]
		set x3 0; after 8000 {set x3 1}; vwait x3	;# wait 8 seconds
		exit
	}
}
# puts --$apacheVersion--
# Set apacheVersion and linuxDistribution - end

# not unpost - end
}

# postEnvironmentArray
if [file exists ../auxdoc/.postEnvironmentArray.tcl] {
	catch {source ../auxdoc/.postEnvironmentArray.tcl}
}

if [file exists $homePath/newVersion] {
	if [info exists postEnvironmentArray(urlibServerAddress)] {
		unset postEnvironmentArray(urlibServerAddress)
	}
	if [info exists postEnvironmentArray(urlibadEMailAddress)] {
		unset postEnvironmentArray(urlibadEMailAddress)
	}
	if [info exists postEnvironmentArray(regionalURLibServerAddress)] {
		unset postEnvironmentArray(regionalURLibServerAddress)
	}
	if [info exists postEnvironmentArray(regionalURLibAdEMailAddress)] {
		unset postEnvironmentArray(regionalURLibAdEMailAddress)
	}
	if {[info exists loCoInRep] && $loCoInRep != "$devLoCoInRep"} {
		if [file exist ../auxdoc/.urlibServiceVersion.tcl] {
			source ../auxdoc/.urlibServiceVersion.tcl	;# set urlibServiceVersion
		} else {
			set urlibServiceVersion 0
		}
		source $homePath/newVersion	;# set updateLevel 
		if {[string compare $updateLevel(1) $urlibServiceVersion] > 0} {
# old version - delete textlog
# must be deleted now because start may be used after post
# (for example when using updateURLibService)
			file delete ../auxdoc/textLog
		}
		if {[string compare $updateLevel(2) $urlibServiceVersion] > 0} {
# old version - reload the whole collection
			file delete ../auxdoc/.repositoryProperties.tcl
			file delete ../auxdoc/.referenceTable.tcl
			file delete ../auxdoc/.wordOccurrenceArray.tcl
			file delete ../auxdoc/.keyRepositoryList.tcl
		}

		if {[string compare $updateLevel(8) $urlibServiceVersion] > 0} {
# old version - reload metadataRep
			file delete ../auxdoc/.wordOccurrenceArray.tcl
		}

		if {[string compare $updateLevel(12) $urlibServiceVersion] > 0} {
# old version - reload repositoryProperties
			file delete ../auxdoc/.repositoryProperties.tcl
		}
	}
}

if [file exists ../auxdoc/.postEnvironmentArray.tcl] {
#	set installInitialCollection 0	;# commented by GJFB in 2021-12-10 - already set in LoadGlobalVariables
# Clean the clipboards and progressDir
	if ![regexp -- {-[xX]} $argv] {
# not unpost
		file delete -force $homePath/clipboard
		file mkdir $homePath/clipboard
		file delete -force $homePath/clipboard2	;# same code is now in PutDocumentOnClipboard
		file mkdir $homePath/clipboard2
		file delete -force $homePath/col/$URLibServiceRepository/doc/progressDir
		file mkdir $homePath/col/$URLibServiceRepository/doc/progressDir
	}
# Clean the clipboards and progressDir - end
} else {
#	set installInitialCollection 1	 ;# install - commented by GJFB in 2021-12-10 - already set in LoadGlobalVariables
	MakeAllCgiScripts 0
}

# Set urlibServerAddress
if ![info exists postEnvironmentArray(urlibServerAddress)] {
	set postEnvironmentArray(urlibServerAddress) {33 {0 0 0} {1 0 0} {2 0 0} {3 0 0} {4 0 0} {5 0 0} {6 0 0} {7 0 0} {8 0 0} {9 0 0} {10 0 0} {11 0 0} {12 0 0} {13 3 {4 8 10}} {14 3 {4 8 10}} {15 5 {3 15}} {16 8 {1 5 11}} {17 9 9} {18 10 7} {19 11 12} {20 12 2} {21 13 6} {22 13 6} {23 14 14} {24 14 14} {25 15 13} {26 15 13} {27 15 13} {28 15 13} {29 15 13} {30 15 13} {31 15 13} {32 15 13} {33 15 13} {34 15 13} {35 15 13} {36 15 13} {37 15 13} {38 15 13} {39 15 13} {40 15 13} {41 15 13} {42 15 13} {43 15 13} {44 15 13} {45 15 13} {46 15 13} {47 15 13} {48 15 13} {49 15 13} {50 15 13} {51 15 13} {52 15 13} {53 15 13} {54 15 13} {55 15 13} {56 15 13} {57 15 13} {58 15 13} {59 15 13} {60 15 13} {61 15 13} {62 15 13} {63 15 13} {64 15 13} {65 15 13} {66 15 13} {67 15 13} {68 15 13} {69 15 13} {70 15 13} {71 15 13} {72 15 13} {73 15 13} {74 15 13} {75 15 13} {76 15 13} {77 15 13} {78 15 13} {79 15 13} {80 15 13} {81 15 13} {82 15 13} {83 15 13} {84 15 13} {85 15 13} {86 15 13} {87 15 13} {88 15 13} {89 15 13} {90 15 13} {91 15 13} {92 15 13} {93 15 13}}
}
# Set urlibServerAddress - end

# Set sampledDocumentDBServerAddress
if ![info exists postEnvironmentArray(sampledDocumentDBServerAddress)] {
	set postEnvironmentArray(sampledDocumentDBServerAddress) $postEnvironmentArray(urlibServerAddress)
}
# Set sampledDocumentDBServerAddress - end

if ![info exists postEnvironmentArray(urlibadEMailAddress)] {
## banon@iconet.com.br
#	set postEnvironmentArray(urlibadEMailAddress) {33 {0 0 0} {1 0 0} {2 0 0} {3 0 0} {4 0 0} {5 0 0} {6 0 0} {7 0 0} {8 0 0} {9 0 0} {10 0 0} {11 0 0} {12 0 0} {13 2 {13 17}} {14 2 {13 17}} {15 2 {13 17}} {16 2 {13 17}} {17 2 {13 17}} {18 2 {13 17}} {19 2 {13 17}} {20 2 {13 17}} {21 2 {13 17}} {22 2 {13 17}} {23 2 {13 17}} {24 2 {13 17}} {25 2 {13 17}} {26 2 {13 17}} {27 2 {13 17}} {28 2 {13 17}} {29 2 {13 17}} {30 2 {13 17}} {31 3 6} {32 3 6} {33 3 6} {34 3 6} {35 3 6} {36 3 6} {37 3 6} {38 3 6} {39 3 6} {40 3 6} {41 3 6} {42 3 6} {43 3 6} {44 3 6} {45 3 6} {46 3 6} {47 3 6} {48 3 6} {49 3 6} {50 3 6} {51 3 6} {52 3 6} {53 3 6} {54 3 6} {55 3 6} {56 3 6} {57 3 6} {58 3 6} {59 3 6} {60 3 6} {61 3 6} {62 3 6} {63 3 6} {64 4 2} {65 6 {1 18}} {66 8 {8 14}} {67 8 {8 14}} {68 9 11} {69 9 11} {70 9 11} {71 9 11} {72 10 7} {73 10 7} {74 10 7} {75 10 7} {76 11 16} {77 14 {3 5 10}} {78 17 {4 9 15}} {79 17 {4 9 15}} {80 17 {4 9 15}} {81 18 19} {82 18 19} {83 19 12} {84 19 12} {85 19 12} {86 19 12} {87 19 12} {88 19 12} {89 19 12} {90 19 12} {91 19 12} {92 19 12} {93 19 12}}
# urlibservice@gmail.com
	set postEnvironmentArray(urlibadEMailAddress) {33 {0 0 0} {1 0 0} {2 0 0} {3 0 0} {4 0 0} {5 0 0} {6 0 0} {7 0 0} {8 0 0} {9 0 0} {10 0 0} {11 0 0} {12 0 0} {13 1 19} {14 1 19} {15 1 19} {16 1 19} {17 1 19} {18 1 19} {19 1 19} {20 1 19} {21 1 19} {22 1 19} {23 1 19} {24 1 19} {25 1 19} {26 1 19} {27 1 19} {28 1 19} {29 1 19} {30 1 19} {31 2 13} {32 2 13} {33 2 13} {34 2 13} {35 2 13} {36 2 13} {37 2 13} {38 2 13} {39 2 13} {40 2 13} {41 2 13} {42 2 13} {43 2 13} {44 2 13} {45 2 13} {46 2 13} {47 2 13} {48 2 13} {49 2 13} {50 2 13} {51 2 13} {52 2 13} {53 2 13} {54 2 13} {55 2 13} {56 2 13} {57 2 13} {58 2 13} {59 2 13} {60 2 13} {61 2 13} {62 2 13} {63 2 13} {64 3 16} {65 4 5} {66 6 {11 20}} {67 6 {11 20}} {68 8 {7 12}} {69 8 {7 12}} {70 9 14} {71 9 14} {72 12 {4 10 17}} {73 12 {4 10 17}} {74 12 {4 10 17}} {75 14 {3 18}} {76 16 {15 22}} {77 16 {15 22}} {78 17 21} {79 17 21} {80 17 21} {81 19 {2 8}} {82 20 6} {83 20 6} {84 21 1} {85 22 9} {86 22 9} {87 22 9} {88 22 9} {89 22 9} {90 22 9} {91 22 9} {92 22 9} {93 22 9}}
}

# regionalURLibServerAddress - not used for the moment (should be used with banners)
if ![info exists postEnvironmentArray(regionalURLibServerAddress)] {
	set postEnvironmentArray(regionalURLibServerAddress) $postEnvironmentArray(urlibServerAddress)
}

# sampledDocumentRegionalDBServerAddress - not used for the moment
if ![info exists postEnvironmentArray(sampledDocumentRegionalDBServerAddress)] {
	set postEnvironmentArray(sampledDocumentRegionalDBServerAddress) $postEnvironmentArray(urlibServerAddress)
}

if ![info exists postEnvironmentArray(regionalURLibAdEMailAddress)] {
	set postEnvironmentArray(regionalURLibAdEMailAddress) $postEnvironmentArray(urlibadEMailAddress)
}

# SAVE
	StoreArray postEnvironmentArray ../auxdoc/.postEnvironmentArray.tcl
# SAVE - end

if {[regexp -- {-[rR]} $argv] || $installInitialCollection} {
# reset (may be necessarily after a "disk quota exceeded")
# no reset is done if there is an integrity alert
# to force reset after an integrity alert and clean up once and for all the integrity alert, $homePath/@missingDirectoryList must be removed
	if {[file exists $homePath/@incompleteMetadataList] || [file exists $homePath/@missingDirectoryList]} {
# incompleteMetadataList
		if [file exists $homePath/@incompleteMetadataList] {
			Load $homePath/@incompleteMetadataList incompleteMetadataList 
			set list [join $incompleteMetadataList \n]
#			set message "reset not done; the following metadata are incomplete and should be first fixed:\n$list\notherwise the file $homePath/@incompleteMetadataList should be removed"
			set message "reset not done; the metadata for the following repositories:\n$list\nare incomplete and must first be corrected from a backup.\nAfter correction, the $homePath/@incompleteMetadataList file must be removed before running reset."
			puts [StoreLog {alert} {post (4a)} $message]
		}
# missingDirectoryList
		if [file exists $homePath/@missingDirectoryList] {
			Load $homePath/@missingDirectoryList fileContent 
# examples of fileContent: dir1A and dir1B/dir2A and dir1B/dir2B/dir3A and dir1B/dir2B/dir3B/dir4A
			set missingDirectoryList {}
			foreach dir $fileContent {
				if ![file isdirectory $homePath/col/$dir] {lappend missingDirectoryList $dir}
			}
			if [string equal {} $missingDirectoryList] {
				file delete $homePath/@missingDirectoryList
			} else {
				set list [join $missingDirectoryList \n]
				set message "reset not done; the following repositories are missing and should be first retrieved:\n$list\notherwise the file $homePath/@missingDirectoryList should be removed"
				puts [StoreLog {alert} {post (4b)} $message]
				Store list $homePath/@missingDirectoryList
				exit	;# added by GJFB in 2022-11-09
			}
		}
	} else {
		file delete ../auxdoc/.repositoryProperties.tcl
		file delete ../auxdoc/.referenceTable.tcl
		file delete ../auxdoc/.wordOccurrenceArray.tcl
		file delete ../auxdoc/.keyRepositoryList.tcl
		file delete $homePath/@homePath	;# in order to update the cgi scripts at reset
	}
}

set repositoryListForPost {}
if [file exists ../auxdoc/.repositoryListForPost.tcl] {
	source ../auxdoc/.repositoryListForPost.tcl	;# set the global variable repositoryListForPost
}

# puts [info exists loCoInRep]

if ![regexp -- {-[xX]} $argv] {
# not unpost
# Add migrated repositories
# see "Zip repositories" in the administrator page
	cd $homePath
	set fileList [glob -nocomplain repositoryListForPostFromZipArchive*]
	cd $pwd
	if ![string equal {} $fileList] {
		if {$tcl_platform(platform) == "unix"} {
			foreach {userName groupName} [lrange [exec ls -l | grep post$] 2 3] {break}
if 0 {
# time consuming
			cd $homePath
			exec chmod -R g+w col
			exec chmod -R g+s col
			exec chgrp -R $groupName col
			cd $pwd
}
		}
		set fileContent2 [MakeCgiScript $URLibServiceRepository {} update mirror.tcl {CreateMirror 1} cgi2 0]
		foreach file $fileList {
			Load $homePath/$file fileContent
#			set repositoryList2 [split $fileContent \n]
			regsub {repositoryListForPostFromZipArchive} $file {} archiveName
			Load $homePath/serverAddressWithIPOfZipArchive$archiveName remoteServerAddressWithIP
			set repositoryList2 [split $fileContent \n]
			set repositoryList3 {}	;# list of repositories completely transferred
			set repositoryList4 {}	;# list of repositories not completely transferred
			foreach rep $repositoryList2 {
				if {$tcl_platform(platform) == "unix"} {
					cd $homePath
					exec chmod -R g+w col/$rep
					exec chmod -R g+s col/$rep
					exec chgrp -R $groupName col/$rep
					cd $pwd
				}
#				if ![TestContentType $rep Metadata] #
				Load $homePath/col/$rep/service/type repContentType
				if [regexp {Metadata} $repContentType] {
# metadata repository
					Load $homePath/col/$rep/service/reference repReference
					regsub {../../../../../../col/} $repReference {} repName
					if [info exists metadataRepArray($repName)] {
						lappend metadataRepArray($repName) $rep
					} else {
						set metadataRepArray($repName) $rep
					}
				} else {
# Update cgi2/update
# useful when migrating a repository from one host collection to another using archive.zip created from the administrator page
					set updateFilePath $homePath/col/$rep/auxdoc/cgi2/update
					if [file exists $updateFilePath] {
						Load $updateFilePath fileContent
						if ![string equal $fileContent2 $fileContent]	{
							Store fileContent2 $updateFilePath
							if {$tcl_platform(platform) == "unix"} {
								if [file owned $updateFilePath] {exec chmod 774 $updateFilePath}
							}
						}
					}
# Update cgi2/update - end
					if [regexp -- {-[fF]} $argv] {
# force migration (ignore the remote collection - it might be down forever)
						lappend repositoryList3 $rep
						set transferableFlag 1
						StoreService transferableFlag $rep transferableFlag 1 1 ;# (B)
					} elseif {![GetDocumentState $rep] || ![info exists loCoInRep]} {
# the document is not the original or the variable loCoInRep doesn't exist
#						file delete $homePath/col/$rep/service/transferableFlag
						lappend repositoryList3 $rep
					} else {
# Update remote host collection
# metadataRep
#						set metadataRep [FindMetadataRep $rep]
# registrationPassword
						if [LoadService $rep registrationPassword registrationPassword 1 1] {
# corrupted password
							puts [StoreLog {alert} {post (5)} "$rep has a corrupted registration password"]
							continue
						}
						set transferableFlag 0	;# must remain 0 in A
# Delete the remote host collection field value
# SUBMIT
#						set message [Execute $remoteServerAddressWithIP [list UpdateHostCollectionFieldValue $rep $metadataRep $codedPassword {} $transferableFlag]]
#						set message [Execute $remoteServerAddressWithIP [list UpdateHostCollectionFieldValue $rep $registrationPassword {} $transferableFlag] 0]	;# not async
#						set message [Execute $remoteServerAddressWithIP [list UpdateHostCollectionFieldValue $rep $registrationPassword {} $transferableFlag]]
						set message [Execute $remoteServerAddressWithIP [list UpdateHostCollectionFieldValue $rep $registrationPassword $loCoInRep $transferableFlag]]
# Delete the remote host collection field value - end
						if [string equal {transferable} $message] {
							lappend repositoryList3 $rep
# Create a new registration password
							regsub {0\.} [expr [SortRandomNumber]/double(233280)] {} registrationPassword
							StoreService registrationPassword $rep registrationPassword 1 1
# Create a new registration password - end
							set transferableFlag 1	;# end of transfer; must be 1 in B
							StoreService transferableFlag $rep transferableFlag 1 1
#							set metadataList {}
#							set metadata2List {}
#							UpdateMetadataField $metadataRep transferableflag $transferableFlag metadataList metadata2List 1
#							AddMetadata $metadataList
						} else {
							lappend repositoryList4 $rep
							if [string equal {} $message] {
								puts [StoreLog {alert} {post (6)} "while processing $rep, UpdateHostCollectionFieldValue returned empty"]
							} else {
								puts [StoreLog {alert} {post (7)} "while processing $rep, UpdateHostCollectionFieldValue returned the following message: $message"]
							}
						}
# Update remote host collection - end
					}
				}
			}
			set fullRepositoryList3 $repositoryList3
			foreach rep $repositoryList3 {
				set fullRepositoryList3 [concat $fullRepositoryList3 $metadataRepArray($rep)]	;# adds the metadata repositories
			}
			set repositoryListForPost [concat $repositoryListForPost $fullRepositoryList3]	;# adds list of repositories completely transferred
			set numberOfUncompleteTransfer [llength $repositoryList4]
			if $numberOfUncompleteTransfer {
				puts [StoreLog {alert} {post (8)} "transfer not completed with the archive $archiveName from $remoteServerAddressWithIP: the number of uncomplete transfer is $numberOfUncompleteTransfer"]
				set fullRepositoryList4 $repositoryList4
				foreach rep $repositoryList4 {
					set fullRepositoryList4 [concat $fullRepositoryList4 $metadataRepArray($rep)]	;# adds the metadata repositories
				}
				set fullRepositoryList4 [join $fullRepositoryList4 \n]
				Store fullRepositoryList4 $homePath/$file	;# list of repositories not completely transferred
			} else {
				file delete $homePath/$file
				file delete $homePath/serverAddressWithIPOfZipArchive$archiveName
			}
		}
	}
# Add migrated repositories - end
}

# referenceTable
if [file exists ../auxdoc/.referenceTable.tcl] {
	if [catch {source ../auxdoc/.referenceTable.tcl}] {
# .referenceTable.tcl has been corrupted
		file delete ../auxdoc/.referenceTable.tcl
	}
}
if {![file exists ../auxdoc/.referenceTable.tcl] || ![string equal {} $repositoryListForPost]} {
	if [file exists ../auxdoc/.referenceTable.tcl] {
		foreach rep $repositoryListForPost {
			UpdateReferenceTable $rep
		}
	} else {
		CreateRepositoryList
		foreach rep $repositoryList {
			UpdateReferenceTable $rep 0
		}
	}
#	UpdateReferenceFileForLoCoInRep
#	StoreArray referenceTable ../auxdoc/.referenceTable.tcl
	SaveReferenceTable 1
}

# repositoryProperties
if [file exists ../auxdoc/.repositoryProperties.tcl] {
	catch {source ../auxdoc/.repositoryProperties.tcl}	;# .repositoryProperties.tcl might have been corrupted
}

if ![info exists repositoryProperties($URLibServiceRepository,history)] {
# repositoryProperties has been corrupted
	puts {.repositoryProperties.tcl has been removed or is corrupted}
	puts {.repositoryProperties.tcl will be recreated}
	file delete ../auxdoc/.repositoryProperties.tcl
}

if {![file exists ../auxdoc/.repositoryProperties.tcl] || ![string equal {} $repositoryListForPost]} {
	if [file exists ../auxdoc/.repositoryProperties.tcl] {
		set repositoryList2 $repositoryListForPost
	} else {
		CreateRepositoryList
		set repositoryList2 $repositoryList
	}

	file delete ../auxdoc/.copyOfRepositoryProperties.tcl
	foreach rep $repositoryList2 {
#		if ![regexp {^[^/]+/[^/]+/\d{4,}/\d{2}\.\d{2}\.\d{2}\.\d{2}($|\.\d{2}$|\.\d{2}\.\d{3}$)} $rep] #
		if ![regexp {^[^/]+/[^/]+/\d{4,}/\d{2}\.\d{2}\.\d{2}\.\d{2}($|\.\d{2}$|\.\d{2}\.\d{1,}$)} $rep] {
# not the repository syntax
# ../auxdoc/.repositoryListForPost.tcl might be corrupted
			puts [StoreLog {alert} {post (9)} "--$rep-- has not the repository syntax\nUpdateRepositoryProperties was not executed"]
			if ![file exists ../auxdoc/.copyOfRepositoryProperties.tcl] {
				file copy -force ../auxdoc/.repositoryProperties.tcl ../auxdoc/.copyOfRepositoryProperties.tcl	;# used to manually check corrupted .repositoryListForPost.tcl file
			}
			continue
		}
		UpdateRepositoryProperties $rep
	}
#	StoreArray repositoryProperties ../auxdoc/.repositoryProperties.tcl
	SaveRepositoryProperties 1
}

# loBiMiMetadataRep
# this code must be here because of FindMetadataRep
	if {!$installInitialCollection && ![info exists environmentArray(localBibliographicMirrorMetadataRepository)]} {
# migration - 2016-05-23 - environmentArray of Archive installed before 2016-05-18 doesn't have the entry localBibliographicMirrorMetadataRepository
		set loBiMiMetadataRep [FindMetadataRep $loBiMiRep]
# puts --$loBiMiMetadataRep--
		set environmentArray(localBibliographicMirrorMetadataRepository) $loBiMiMetadataRep	;# used just in LoadGlobalVariables
		StoreArrayWithBackup environmentArray ../auxdoc/.environmentArray.tcl w list
		file delete $homePath/col/$loBiMiRep/service/citingItemList	;# added by GJFB in 2025-06-15 - migration (before 2025-06-15 some default Local Bibliographic Mirror Repository may have a citingItemList file)
	}

if [file exists $homePath/newVersion] {
	if [file exist ../auxdoc/.urlibServiceVersion.tcl] {
		source ../auxdoc/.urlibServiceVersion.tcl	;# set urlibServiceVersion
	} else {
		set urlibServiceVersion 0
	}
	source $homePath/newVersion	;# set updateLevel 
# puts [expr [string compare $updateLevel(3) $urlibServiceVersion] > 0]
	if {[string compare $updateLevel(3) $urlibServiceVersion] > 0} {
# old version - make the cgi scripts
		puts {updating cgi scripts...}
		MakeAllCgiScripts 0
		puts {cgi scripts updated}
	}
}

if !$installInitialCollection {
# Updating the cgi scripts after a home path change
if 0 {
# commented by GJFB in 2012-12-30 - $homePath/@homePath may be correct and the homePath in the cgi script incorrect
	Load $homePath/@homePath fileContent
	if ![string equal $homePath $fileContent] {
# the cgi scripts must be updated
		puts {updating cgi scripts...}
		MakeAllCgiScripts 1
		Store homePath $homePath/@homePath	;# used in post only
		puts {cgi scripts updated}
	}
} else {
	if [regexp -- {-[cC]} $argv] {file delete ../auxdoc/cgi/get}
	Load ../auxdoc/cgi/get fileContent
	set rep $URLibServiceRepository
	set cgiScript [MakeCgiScript $rep $rep get get.tcl Get cgi 0]	;# to force updating remove cgi/get
	if ![string equal $cgiScript $fileContent] {
# the cgi scripts must be updated
		puts {updating cgi scripts...}
		MakeAllCgiScripts 1
		puts {cgi scripts updated}
	}
}
# Updating the cgi scripts after a home path change - end

# Solve the accent problem after migration from a utf-8 local collection to a non utf-8 one, and vice versa
# if [file exists $homePath/col/$URLibServiceRepository/auxdoc/@cgiCallFlagForPost] #
# post has been called from a cgi script (see Administrator page)
#	if ![regexp -- {-[xX]} $argv] #
#		file delete $homePath/col/$URLibServiceRepository/auxdoc/@cgiCallFlagForPost
#	#
# else #
	set encodingSystem [encoding system]
	Load $homePath/@encodingSystem encodingSystem2
	if [string equal {utf-8} $encodingSystem] {
# encoding system is utf-8
		if ![string equal {utf-8} $encodingSystem2] {
# @encodingSystem didn't exist (was not utf-8)
# the service/encodingSystem files must be updated or created
			puts {updating service/encodingSystem files for metadata repositories...}
			CreateRepositoryList	;# set repositoryList
			foreach rep $repositoryList {
				if [TestContentType $rep Metadata] {
# $rep contains a metadata
	 				Store encodingSystem $homePath/col/$rep/service/encodingSystem
				}
			}
			Store encodingSystem $homePath/@encodingSystem	;# used in post only
			puts {service/encodingSystem files updated for metadata repositories}
		}
	} else {
# encoding system is not utf-8
		if [string equal {utf-8} $encodingSystem2] {
# @encodingSystem was utf-8
			puts {removing service/encodingSystem files for metadata repositories...}
			CreateRepositoryList	;# set repositoryList
			foreach rep $repositoryList {
				if [TestContentType $rep Metadata] {
# $rep contains a metadata
					Load $homePath/col/$rep/doc/@metadata.refer fileContent
					set fileContent [encoding convertfrom $encodingSystem2 $fileContent]	;# solves the accent problem when migrating from a collection using utf-8 encoding
					set mtime [file mtime $homePath/col/$rep/doc/@metadata.refer]
					Store fileContent $homePath/col/$rep/doc/@metadata.refer
					if [file owned $homePath/col/$rep/doc/@metadata.refer] {
						file mtime $homePath/col/$rep/doc/@metadata.refer $mtime	;# restore mtime
					}
	 				file delete $homePath/col/$rep/service/encodingSystem
				}
			}
			file delete $homePath/@encodingSystem	;# used in post only
			puts {service/encodingSystem files removed for metadata repositories}
		}
	}
#
# Solve the accent problem after migration from a utf-8 local collection to a non utf-8 one, and vice versa - end
}

# MakeAllCgiScripts

# set urlibServiceSiteWithIP 150.163.2.14:1905	;# site having a copy of the URLibService last version
# set urlibServiceSiteWithIP 150.163.2.175:1905	;# site having a copy of the URLibService last version
# set urlibServiceSiteWithIP 150.163.2.176	;# plutao - site having a copy of the URLibService last version
# set urlibServiceSiteWithIP 150.163.34.239	;# plutao - site having a copy of the URLibService last version
# urlibServiceSiteWithIP - site having a copy of the URLibService last version
# set urlibServiceSiteWithIP [Execute urlib.net GetURLibServiceSiteWithIP]
# urlibServiceSiteWithIP is needed in StartService
if [file exists $homePath/@URLibServiceSiteWithIP.txt] {
# the current site is urlib.net (no other sites should have this file)
	Load $homePath/@URLibServiceSiteWithIP.txt urlibServiceSiteWithIP
} else {
	if [catch {Execute urlib.net GetURLibServiceSiteWithIP} urlibServiceSiteWithIP] {
#		puts [StoreLog {alert} {post (10)} "urlib.net site not found: $urlibServiceSiteWithIP"]
		puts [StoreLog {alert} {post (10)} "urlib.net site not found: computer probably in standalone mode"]
		set urlibServiceSiteWithIP {}
	} else {
		if [string equal {} $urlibServiceSiteWithIP] {
			puts [StoreLog {alert} {post (11)} "urlib.net site not found: computer probably in standalone mode or unfair proxy server (in the latter case, contact the administrator of urlib.net)"]
		}
	}		
}
# puts --$urlibServiceSiteWithIP--	;# 150.163.34.239 (plutao)

# Unpost
if [regexp -- {-[xX]} $argv] {
# unpost
if 0 {
	file mkdir $serverRoot/conf/VirtualHost2
	cd $serverRoot/conf/VirtualHost2
	set virtualHostConfList [glob -nocomplain *]
	cd $pwd
	if {[llength $virtualHostConfList] > 1} {
		set message [list {cannot unpost - more than one local collection are posted}]
		lappend message "the following ports are in use: $virtualHostConfList"
		puts [join $message \n]
		set logx [join [concat {post:} $message] \n]
		Store logx $homePath/@errorLog auto 0 a
		exit
	}
}

# Capture new URLibService version
	if [regexp -- {-[uU]} $argv] {
		puts {looking for new URLibService version...}
		set localLastVersion [GetURLibServiceLastVersion]

		if ![string equal {} $urlibServiceSiteWithIP] {
			set query {list GetURLibServiceLastVersion}
# MULTIPLE SUBMIT
			set lastVersion {}
#			MultipleSubmit {} $query lastVersion 0 [list $urlibServiceSiteWithIP]
			MultipleSubmit {} $query lastVersion 1 [list $urlibServiceSiteWithIP]
			if ![string equal {} $lastVersion] {
				if ![string equal [lindex $localLastVersion 0] $lastVersion] {
# Get the new version
					puts {capturing a new URLibService version...}
					if [file exists $homePath/doc.zip] {
						file rename -force $homePath/doc.zip $homePath/previousDoc.zip
					}
					set fileId [open $homePath/doc.zip w]

proc Progress {token total current} {
	global iProgress
	if ![info exists iProgress] {set iProgress 0}
	incr iProgress
	if {[expr $iProgress % 20] == 0} {
		puts "[expr int(($current / $total.) * 100)]% done"
	}
#	puts -nonewline "."	;# nonewline doesn't work properly with windows 95
}

					set convertedURL [ConvertURLToHexadecimal http://$urlibServiceSiteWithIP/col/$URLibServiceRepository/download/doc.zip]	;# doc.zip is updated at post of urlibServiceSiteWithIP (see # Update URLibService in StartService)
					if [catch {http::geturl $convertedURL -progress Progress -channel $fileId} token] {
						close $fileId
#						puts "post: new URLibService version capture failed: $token"
						puts [StoreLog {alert} {post (12)} "new URLibService version capture failed: $token"]
					} else {
# new URLibService version captured
						close $fileId
						http::cleanup $token
						set updateURLibServiceMessageForScript {the last URLibService version was captured}
						puts $updateURLibServiceMessageForScript
						Store updateURLibServiceMessageForScript $homePath/col/$URLibServiceRepository/auxdoc/@updateURLibServiceMessageForScript	;# used in Script (Administrator page)
if 0 {
# Extract a new version of URLibService
						source ExtractURLibService.tcl
						ExtractURLibService
# Extract a new version of URLibService - end
}
					}
# Get the new version - end
				} else {
					set updateURLibServiceMessageForScript {there is no new URLibService version to capture}
					puts $updateURLibServiceMessageForScript
					Store updateURLibServiceMessageForScript $homePath/col/$URLibServiceRepository/auxdoc/@updateURLibServiceMessageForScript	;# used in Script (Administrator page)
				}
				if [info exists delay1000] {unset delay1000}
				after 1000 {set delay1000 1}; vwait delay1000	;# wait for a second - required to let Script write the update URLibService message
			}
		}
	}
# Capture new URLibService version - end

# Update the index
# must be before Run-exit
	if [string equal {} $messageForPost] {	;# 'if' added by GJFB in 2020-05-28 because at installation Apache is not running
		if [file exists ../auxdoc/.repositoryListForPost.tcl] {
			set mtime [file mtime ../auxdoc/.repositoryListForPost.tcl]
			Load ../auxdoc/previousRepositoryListForPostMtime previousRepositoryListForPostMtime
			if ![string equal $previousRepositoryListForPostMtime $mtime] {
# the collection has been updated - the index must be updated too
# UPDATE INDEX
				if [catch {http::geturl http://$localSite/createindex.cgi} message] {
# catch error like: couln't open socket: host is unreachable - otherwise post ends
					puts [StoreLog {alert} {post (13)} "$message\n    while executing\n\"http::geturl http://$localSite/createindex.cgi\""]
				} else {
					http::cleanup $message
				}
			}
			file delete ../auxdoc/previousRepositoryListForPostMtime
		}
	}
# Update the index - end

if 1 {
	if [catch {StopApacheServer} message] {
		puts [StoreLog {alert} {post (14)} $message]
#		exit
	}
# httpd.pid file deleted
}

#	StoreLog {notice} {post (14b)} {executing Run-exit ...}	;# added by GJFB in 2020-08-06 to trace unpost-kill-post under infinite loop
	Load ../auxdoc/pid pid	;# pid of the URLib server
# puts environmentArray(ipAddress)=$environmentArray(ipAddress)
# puts serverAddressWithIP=$serverAddressWithIP
#	if [catch {Eval Run-exit $pid} message] #
	if [catch {Execute $serverAddressWithIP [list Run-exit $pid]} message] {
		puts [StoreLog {alert} {post (15)} $message]
#		set x3 0; after 5000 {set x3 1}; vwait x3	;# wait 5 seconds
		exit
	}
#	StoreLog {notice} {post (15b)} {Run-exit executed}	;# added by GJFB in 2020-08-06 to trace unpost-kill-post under infinite loop
	
	puts {URLib local collection unposted}
	
# to update three virtual hosts A, B and C
# do (in any order) unpost A, unpost B, unpost C, post/unpost/post A, post/unpost/post B and post C (see auxdoc/serverDir/logs/virtualHostRunning in ExtractURLibService)
# Extract a new version of URLibService
	source ExtractURLibService.tcl
	ExtractURLibService	;# placed here by GJFB in 2010-08-17
# Extract a new version of URLibService - end

if 0 {
# not tested
	if {$tcl_platform(platform) == "unix"} {	;# added by GJFB in 2020-01-31 - at next post URLibService may be very slow unless the previous post ended killing the current pid
		Load $homePath/col/$URLibServiceRepository/auxdoc/pid pid
		file delete $homePath/col/$URLibServiceRepository/auxdoc/pid
		exec kill -9 $pid
	}
}
#		set x3 0; after 5000 {set x3 1}; vwait x3	;# wait 5 seconds
	
	exit	;# end end end
	
}
# Unpost - end

if [regexp -- {-[oO]} $argv] {		;# added by GJFB in 2021-11-16
# open access - allows administrator registration at Archive installation by cancelling the restrictedSubmission value in CreateMirror (see ^Register$)
	set message x	;# could be anything
	Store message ../auxdoc/messageForRegister
} else {
	file delete ../auxdoc/messageForRegister
}

# set installInitialCollection [expr ![info exists environmentArray(localCollectionIndexRepository)]]	;# commented by GJFB in 2021-12-10 - already set in LoadGlobalVariables

# Start the Service
if !$installInitialCollection {StartService}
# Start the Service - end

if [info exists environmentArray] {
	puts "starting the servers..."
}
# Start the URLib local collection server
if [StartLocalURLibServer] {
# URLiService is probably running
	Load ../auxdoc/messageForStart message
	puts --$message--
# => <StartLocalURLibServer2: opening server socket at port 804 failed: couldn't open socket: permission denied>
	if {$tcl_platform(platform) == "windows"} {vwait forever}
	if {$tcl_platform(platform) == "unix"} {exit}
}
# Start the URLib local collection server - end

# puts >>>$environmentArray(ipAddress)
# puts >>>$serverAddressWithIP

set message [CheckURLibServerAccess]	;# CheckURLibServerAccess is in SPDialog.tcl

# puts $message

# if !$installInitialCollection #	;# added by GJFB in 2016-04-28 - at installation serverAddressWithIP is empty - commented by GJFB in 2021-12-10 - now serverAddressWithIP is nonempty at installation
	foreach {serverName urlibPort} [ReturnCommunicationAddress $serverAddress] {break}
	foreach {ip urlibPort} [ReturnCommunicationAddress $serverAddressWithIP] {break}
	if [regexp {access not completed} $message] {
		Store message ../auxdoc/messageForStart
# similar code in start
		set message [list $message {The communication with the URLib local collection server cannot be made.}]
#		lappend message "$serverName was used as server name."
		lappend message "$ip was used as IP."
		lappend message "$urlibPort was used as URLib port."
		lappend message {The URLib local collection has not been posted.}
		puts [StoreLog {alert} {post (16)} [join $message \n]]
		set x3 0; after 60000 {set x3 1}; vwait x3	;# wait 60 seconds
#		puts [StoreLog {alert} {post (16)} [join $message \n]]
#		close $localURLibServer(socketId)	;# added by GJFB in 2016-06-27 - free the URLib port (ex: 19050)
#		source $homePath/col/$URLibServiceRepository/doc/post	;# added by GJFB in 2016-06-27 - run post again
		exit
	}
#

# Create ipListOFibiNet of the IBI net
# added by GJFB in 2020-12-12 to avoid Denial of Service Attack - used in AcceptCommunication only
	if {$tcl_platform(platform) == "unix"} {
		if [string equal {www.urlib.net} $serverName] {
			Load $homePath/col/$resolverMirrorRepository/doc/@siteList.txt fileContent
			set siteList [split $fileContent \n]
		} else {
			if [catch {http::geturl http://urlib.net/col/$resolverMirrorRepository/doc/@siteList.txt} token] {
				puts "http::geturl error: --$token--"	;# error message - --couldn't open socket: connection refused--
				set ipListOFibiNet {}	;# no response
				set siteList {}	;# added by GJFB in 2024-05-25 otherwise one gets the error: can't read "siteList": no such variable, while executing "foreach site $siteList" below
				if {$tcl_platform(platform) == "unix"} {
					puts "ipListOFibiNet is empty - no solution to avoid Denial of Service Attack is enabled"
				}
			} else {
# geturl returned
				if [string equal {404} [::http::ncode  $token]] {
					set ipListOFibiNet {}	;# no response
					set siteList {}	;# added by GJFB in 2024-05-25 otherwise one gets the error: can't read "siteList": no such variable, while executing "foreach site $siteList" below
				} else {
# puts OK
					set siteList [split [http::data $token] \n]
# {mtc-m16d.sid.inpe.br 806} sid.inpe.br/mtc-m19@80/2009/08.21.17.02 150.163.34.248 USP 2020:12.07.22.36.45 bibdigital@inpe.br licuri.ibict.br 1
				}
				http::cleanup $token
#				set ipListOFibiNet [lsort -unique $ipListOFibiNet]
			}
		}
		set ipListOFibiNet [lindex $urlibServerAddressWithIP 0]	;# ip of urlib.net
		lappend ipListOFibiNet 2001:12ff:0:7::168 ;# ipv6 of urlib.net - added by GJFB in 2021-12-07
		lappend ipListOFibiNet $ip	;# ip of the current site
		foreach site $siteList {
			if ![string equal {} [lindex $site 6]] {	;# repetitor
				lappend ipListOFibiNet [lindex $site 2]	;# ip
			}
		}
		set ipListOFibiNet [lsort -unique $ipListOFibiNet]
# puts --$ipListOFibiNet--
	}
# Create ipListOFibiNet of the IBI net - end

# Set unfairAddrList
# added by GJFB in 2020-12-12 to avoid Denial of Service Attack - used in AcceptCommunication only
# on port 80x: 185.158.113.43 (produces search slowness)
# on port 80x: 172.96.172.238 (produces search slowness)
	if {$tcl_platform(platform) == "unix"} {
		if [file exists $homePath/col/$URLibServiceRepository/auxdoc/@unfairAddrList] {
			Load $homePath/col/$URLibServiceRepository/auxdoc/@unfairAddrList unfairAddrList
		} else {
#			set unfairAddrList {185.158.113.43 172.96.172.238 94.232.40.116 94.232.40.115 94.232.40.114}	;# initial value
			set unfairAddrList {185.158.113 172.96.172 94.232.40}	;# initial value
		}
	}
# Set unfairAddrList - end

if 1 {
StopApacheServer	;# it might be running
}

# if {$tcl_platform(platform) == "windows"} #	;# commented by GJFB in 2016-04-28 - ManageLogFile needs loCoInRep which has not been created yet at installation
# if {$tcl_platform(platform) == "windows" && !$installInitialCollection} #	;# commented by GJFB in 2023-03-16
if !$installInitialCollection {	;# added by GJFB in 2023-03-16 - now manage log file at post for all types of platform
# Windows doesn't allow to change .log file name while Apache is running
# managing log file must occur while Apache is stopped
	ManageLogFile $homePath/@errorLog
	ManageLogFile $homePath/col/$URLibServiceRepository/auxdoc/serverDir/logs/access.log
	ManageLogFile $homePath/col/$URLibServiceRepository/auxdoc/serverDir/logs/error.log
}

# set staticIPFlag [InformURLibSystem]	;# added by GJFB in 2017-10-15 - environmentArray(staticIPFlag) set in InformURLibSystem is required at installation - commented by GJFB in 2018-05-25 otherwise might produce the error message: --couldn't open socket: connection refused-- because Apache might not be yet running (see UpdateSiteList)

if $installInitialCollection {
# Start the Service at installation
	set staticIPFlag 0	;# added by GJFB in 2016-04-28 - force dynamic IP is order to be able to use any type of IP at installation
	set environmentArray(staticIPFlag) $staticIPFlag	;# added by GJFB in 2018-05-25
#	exit	;# to simulate an incomplete initial collection installation (no repository created)
	StartService
# Start the Service at installation - end
} else {
# added by GJFB in 2016-04-28 - serverAddress and serverAddressWithIP needed in CreateEnvironmentArray (called by startApacheServer) are unknown at installation because environmentArray is incomplete - one must wait for the execution of InformURLibSystem 
# now here to allow using http protocol
# Start Apache server
	set startApacheServer 1
	StartApacheServer
#	after 1000 {set waitForApache 1}; vwait waitForApache	;# wait 1 s - needed to allow the use of Apache by CreatePermissionList - added by GJFB in 2013-01-13
	after 2000 {set waitForApache 1}; vwait waitForApache	;# wait 2 s - needed to allow the use of Apache by InformURLibSystem - added by GJFB in 2014-08-16
# Start Apache server - end
}

# Inform the URLib system
# InformURLibSystem	;# commented by GJFB in 2010-08-23 - previously in StartService
 set staticIPFlag [InformURLibSystem]	;# added by GJFB in 2010-08-23 - commented by GJFB in 2017-10-15 - now above - uncommented by GJFB in 2018-05-25
# puts --$staticIPFlag--
if [string equal {} $staticIPFlag] {
	puts [StoreLog {alert} {post (17a)} "staticIPFlag value is empty - communication with urlib.net fails (some ports may be closed) or the local collection has not yet been registered - the robustness of some links in the Arrangement field of the full metadata dynamic page might be affected"]
}
set environmentArray(registrationFlag) 1	;# value is 0 or 1; 1 means that the site has been registered at urlib.net; is 0 when communication with urlib.net failed (urlib.net is down) or the site has not been registered at urlib.net or the collection is being unposted
# environmentArray(registrationFlag) is used to enable/disable the DD button and the "Submit a ..." menu in the tool bar
# environmentArray(registrationFlag) is 0 if staticIPFlag is empty (communication with urlib.net failed (urlib.net is down) or the site has not been registered at urlib.net or the collection is beeing unposted)
# environmentArray(registrationFlag) is 1 if staticIPFlag is 0 or 1 (registered collection)
# if [string equal {0} $staticIPFlag] #	;# commented by GJFB in 2020-05-24
if {!$installInitialCollection && [string equal {0} $staticIPFlag]} {	;# added by GJFB in 2020-05-24 - at this point, at INSTALLATION, Apache is not running yet, so .envArray.tcl used below doesn't exist yet
# the current collection is probably using dynamic IP or the Achive doesn't return the 'Archive inclusion confirmation'. In the latter case references/links to the new password service are omitted in the dynamic html page
	puts [StoreLog {alert} {post (17b)} "staticIPFlag value is 0 - the resolver urlib.net didn't receive the Archive inclusion confirmation from $localSite,\nso $localSite is considered not having a fix IP and references/links to the new password service will be omitted."]
	if ![string equal {} $environmentArray(domainName)] {
# the domain name must be fixed
# site example: c010013416.fmvz.local:80
		set hostName $environmentArray(hostName)	;# c010013416 - gjfb
		set domainName $environmentArray(domainName)	;# fmvz.local - Home
		set environmentArray(hostName) [string tolower $hostName.$domainName]	;# c010013416.fmvz.local - gjfb.home
		set environmentArray(domainName) {}
# Update DOMAIN_NAME within .envArray.tcl
# added by GJFB in 2016-05-22
		array set env2 [array get env]
		unset env 
		source ../auxdoc/.envArray.tcl	;# set env
		set env(DOMAIN_NAME) {empty}
		StoreArray env ../auxdoc/.envArray.tcl w list array 1
		array set env [array get env2]
# Update DOMAIN_NAME within .envArray.tcl - end
	}
# elseif {[string equal {} $staticIPFlag]} #	;# commented by GJFB in 2017-04-28
} elseif {[string equal {} $environmentArray(staticIPFlag)]} {	;# added by GJFB in 2017-04-28
# communication with urlib.net failed (urlib.net is down) or the site has not been registered at urlib.net or the collection is being unposted
	set environmentArray(registrationFlag) 0
}

# if $installInitialCollection {	;# added by GJFB in 2016-04-28 - now environmentArray allow to compute serverAddress and serverAddressWithIP
#	set serverAddress [GetServerAddress]	;# banon-pc2.dpi.inpe.br 800
#	set serverAddressWithIP [GetServerAddress 1]
# }
# Inform the URLib system - end

# Create environmentArray(permissionList)
# Create environmentArray(languagePreference)	;# any changes in displayControl.tcl may require two unpost/post
# Create environmentArray(displayReviewButton)
CreatePermissionList $environmentArray(mirrorRepList)	;# CreatePermissionList needs Apache running when sourcing some displayControl.tcl files that use http::geturl
# Create environmentArray(displayReviewButton) - end
# Create environmentArray(languagePreference) - end
# Create environmentArray(permissionList) - end

StoreArrayWithBackup environmentArray ../auxdoc/.environmentArray.tcl w list	;# added by GJFB in 2012-05-28 - used by start

if $installInitialCollection {	;# added by GJFB in 2016-04-28 - at installation oldServerAddress is empty
	set message "URLib local collection posted"	;# message used in start
	Store message ../auxdoc/messageForStart	;# used in PostLocalCollection to exit from PostLocalCollection and continue within start
} else {
# Update metadataArray and repArray with the correct site name
# the domain name may have changed since the last start
	set oldServerAddress [lindex [GetMetadata $URLibServiceMetadataRepository-0,site] end]
# puts "--$serverAddress-- --$oldServerAddress--"

if 0 {
# commented by GJFB in 2020-05-28 - avoid to exit - the code immediately below should be enough
# if [string equal {} $oldServerAddress] #	;# commented by GJFB in 2016-04-28
	if {[string equal {} $oldServerAddress] && $serverRoot2ExistenceFlag} {
# oldServerAddress empty and not the first execution of post after an installation
# .metadataArray.tcl is corrupted - added by GJFB in 2013-07-04
		puts [StoreLog {alert} {post (18)} "the file:\ncol/$URLibServiceRepository/auxdoc/.metadataArray.tcl\nis corrupted and must be deleted before posting again"]
		exit
	}
}
	if ![string equal $serverAddress $oldServerAddress] {
		puts "changing site name $oldServerAddress to $serverAddress into metadataArray and repArray..."
		set metadata2List [GetMetadata *-0,site]
		puts "change expected time: [expr int(0.04 * [llength $metadata2List])] seconds"	;# ~40 milliseconds per record
#		RemoveMetadata $metadata2List	;# commented by GJFB in 2020-08-18
		RemoveMetadata2 $metadata2List	;# added by GJFB in 2020-08-18
		array set localMetadataArray $metadata2List
		foreach i [array names localMetadataArray] {
			set localMetadataArray($i) $serverAddress 
		}
#		AddMetadata [array get localMetadataArray]	;# commented by GJFB in 2020-08-18
		AddMetadata2 [array get localMetadataArray]	;# added by GJFB in 2020-08-18
		puts "site name changed to: $serverAddress into metadataArray and repArray"
	}
# Update metadataArray and repArray with the correct site name - end
}

# Update $homePath/index.html and $homePath/mirror/index.html
	StoreIndex
# Update $homePath/index.html and $homePath/mirror/index.html - end

if [file exists $homePath/readOnlySite] {Link {} $serverAddress}

if [file exists $homePath/readOnlySite] {
	puts {read only site}
}

if [file exists $homePath/newVersion] {
# Migration 26/02/23
# added by GJFB in 2023-02-26 to solve a Google indexing requirement
	if [file exist ../auxdoc/.urlibServiceVersion.tcl] {
		source ../auxdoc/.urlibServiceVersion.tcl	;# set urlibServiceVersion
	} else {
		set urlibServiceVersion 0
	}
	source $homePath/newVersion	;# set updateLevel 
# puts [expr [string compare $updateLevel(17) $urlibServiceVersion] > 0]
	if {[string compare $updateLevel(17) $urlibServiceVersion] > 0} {
# old version - add the files 'thisInformationItemHomePage.html' in the directories of the PDF target files - added by GJFB in 2023-02-26
		puts {add the files 'thisInformationItemHomePage.html' in the directories of the PDF target files...}
		package require http	;# see online manual
		CreateRepositoryList
		set i 0
		foreach rep $repositoryList {
			LoadService $rep targetFile targetFile 0 1
			LoadService $rep type contentType 0 1
			if {[string equal -nocase {.pdf} [file extension $targetFile]] && ![string equal {External Contribution} $contentType]} {
# http://gjfb:1905/rep/urlib.net/www/2022/10.12.22.35
				if [catch {http::geturl http://$localSite/rep/$rep} token] {
					puts "error processing $rep: $token"
				} else {
# geturl returned
					http::cleanup $token
#					puts "$rep OK"
					incr i
					if ![expr $i%50] {puts $i}
				}
			} else {
				file delete $homePath/col/$rep/doc/[file dirname $targetFile]/thisInformationItemHomePage.html
			}
		}
		puts "$i files 'thisInformationItemHomePage.html' added in the directories of the PDF target files"
		puts {}
	}
# Migration 26/02/23 - end

# Migration 05/03/23
# added by GJFB in 2023-03-05 - has never been applied but could be to have the double click button (<<) dislayed when opening the local Collection Index
	if {[string compare $updateLevel(18) $urlibServiceVersion] > 0} {
# old version - add nexthigherunit to loCoInRep 
		puts {add nexthigherunit to loCoInRep...}
		LoadService $loCoInRep metadataRepositoryList loCoInMetadataRep 0 1
		set metadata2List {}
		set loBiMiId [FindIdentifierNameFromIBI $loBiMiRep]
		set metadataList [list $loCoInMetadataRep-0,nexthigherunit $loBiMiId]
		Execute $serverAddressWithIP [list UpdateMetadata $metadata2List $metadataList]
		set loCoInRepositoryList {}
		lappend loCoInRepositoryList $loCoInRep
		lappend loCoInRepositoryList $loCoInMetadataRep
		Execute $serverAddressWithIP [list UpdateRepositoryListForPost $loCoInRepositoryList]
		puts {nexthigherunit added to loCoInRep}
		puts {}
	}
# Migration 05/03/23 - end
}

if {[info exists loCoInRep] && $loCoInRep != "$devLoCoInRep"} {
	file delete $homePath/newVersion
}

set urlibServiceVersion [lindex [GetURLibServiceLastVersion] 0]	;# added by GJFB in 2023-02-28 - before in StartService
StoreList urlibServiceVersion ../auxdoc/.urlibServiceVersion.tcl	;# added by GJFB in 2023-02-28 - before in StartService

if {$tcl_platform(platform) == "unix"} {
#	cd $homePath
#	exec chmod -R g+w col	;# unzip might not preserve write permission for group - time consuming
#	cd $pwd

	if !$installInitialCollection {	;# added by GJFB in 2020-05-31 - at installation serverRoot2 doesn't exist yet
# Create kill
# kill
		set httpdPid {}
		set i 0
		while {[string equal {} $httpdPid]} {
			incr i
			if {$i == 5} {
				puts "\tApache is not starting: probably a configuration error\n\tset the LogLevel directive to debug and see Apache\n\t$serverRoot2/logs/error.log"
				set i 0
			}
#			Load $homePath/col/$URLibServiceRepository/auxdoc/serverDir/logs/httpd.pid httpdPid
			Load $serverRoot2/logs/httpd.pid httpdPid
			set xw 0; after 1000 {set xw 1}; vwait xw
		}
		set kill {}
#		if [file exists $homePath/col/$URLibServiceRepository/auxdoc/serverDir/logs/httpd.pid] #	;# commented by GJFB in 2020-10-05
		if {[file exists $homePath/col/$URLibServiceRepository/auxdoc/serverDir/logs/httpd.pid] && \
		![file exists $homePath/col/$URLibServiceRepository/auxdoc/serverDir/logs/virtualHostRunning]} {	;# added by GJFB in 2020-10-05 - don't kill if some virtual host is running
			append kill "kill -TERM $httpdPid\n"
			append kill "rm -f \"$homePath/col/$URLibServiceRepository/auxdoc/serverDir/logs/httpd.pid\"\n"
		}
		Load $homePath/col/$URLibServiceRepository/auxdoc/pid pid
		append kill "kill -9 $pid"
		append kill "\n"	;# added by GJFB in 2020-03-17
		append kill "rm -f \"$homePath/col/$URLibServiceRepository/auxdoc/pid\"\n"	;# added by GJFB in 2020-02-02 - pid file is not needed anymore
		file delete $homePath/kill	;# otherwise chmod may not work because of a different owner
		Store kill $homePath/kill
#		exec chmod 755 $homePath/kill	;# commented by GJFB in 2020-01-31
		exec chmod 774 $homePath/kill	;# added by GJFB in 2020-01-31
# Create kill - end

#	if !$installInitialCollection #	;# added by GJFB in 2016-04-28 - at installation serverName doesn't exist
		puts "servers at >>> $serverAddress \[$serverName\] started"
	}
	
	Load ../auxdoc/apacheVersion fileContent
	puts $fileContent
	set message "URLib local collection posted at $localSite"	;# message used in start
	puts $message
#	puts {(to unpost, execute the command: post -- -x)}
	puts {(to unpost, execute the command: ./post -x)}

	Store message ../auxdoc/messageForSentinel	;# added by GJFB in 2024-03-05 - messageForSentinel deleted and used in sentinel.tcl only - used to know about the end of the posting process - the of messageForSentinel could be anything

}

# if {$tcl_platform(platform) == "windows"} #
if {$tcl_platform(platform) == "windows" && !$installInitialCollection} {	;# added by GJFB in 2016-04-28 - at installation serverName doesn't exist
	puts "servers at >>> $serverAddress \[$serverName\] started"
	Load ../auxdoc/apacheVersion fileContent
	puts $fileContent
	set message "URLib local collection posted at $localSite"	;# message used in start
	puts $message
	regsub -all {/} $tclPath {\\} path1
	regsub -all {/} "$homePath/col/$URLibServiceRepository/doc" {\\} path2
	puts "(to unpost, execute the command:
$path1 post -x
starting in:
$path2)"

# Open the bibliographic mirror within the navigator
	after 1000 {set wait 1}; vwait wait	;# wait 1 s - needed when opening Firefox (otherwise Fiferox open in offline mode) - added by GJFB in 2010-11-25
	set language $environmentArray(spLanguageEntry)
	Link {} $localSite/col/$loBiMiRep/doc/mirror.cgi?languagebutton=$webLanguageTable($language)
# Open the bibliographic mirror within the navigator - end
}

Store message ../auxdoc/messageForStart	;# used in PostLocalCollection to exit from PostLocalCollection and continue within start

if {$tcl_platform(os) == "Linux"} {
	exec which nslookup > $homePath/col/$URLibServiceRepository/auxdoc/whichMessage
	Load $homePath/col/$URLibServiceRepository/auxdoc/whichMessage message
	if [regexp ^no $message] {puts [StoreLog {alert} {post (19)} $message]}
}

#set body [info body StartService]
#set body2 [info body StartService]
#if {[string compare $body $body2] == 0} {
#	puts {uncorrupted code}
#} else {
#	puts {corrupted code}
#}

# vwait forever

set previousRepositoryListForPostMtime {}

set startingTime 04:00:00	;# for managing log files, testing local collection integrity, saving all data and updating @wordOccurrence
set testingFlag 0	;# 1 is for testing log file managment, local collection integrity test, all data saving and @wordOccurrence update
if $testingFlag {
set timePeriod [expr 30]	;# 1/2 minute - for testing log file managment, local collection integrity test, all data saving and @wordOccurrence update
} else {
set timePeriod [expr 24*60*60]	;# 1 day - for managing log files, testing local collection integrity, saving all data and updating @wordOccurrence
}
# set startingSecond [clock scan $startingTime -base 109306800]	;# for managing log files, testing local collection integrity, saving all data and updating @wordOccurrence - commented by GJFB in 2023-01-14 - now in ComputeFileUpdateFlag

if $testingFlag {set numberOfIterations 5} else {set numberOfIterations 1800}	;# 1800*2 == 3600s == 1h
file delete $homePath/col/$URLibServiceRepository/auxdoc/@updateURLibServiceMessageForPost
if 0 {
# commented by GJFB in 2020-11-19
file delete $homePath/col/$URLibServiceRepository/auxdoc/@unpost-kill-postMessageForPost	;# added by GJFB in 2020-01-22 to do unpost/post when the first error message: 'infinite loop?' is detected in ServeLocalCollection
file delete $homePath/col/$URLibServiceRepository/auxdoc/@kill-postMessageForPost	;# added by GJFB in 2020-10-05 to do kill/post when the first error message: 'infinite loop?' is detected in ServeLocalCollection
}

while 1 {
if 0 {	
#	set forever 0; after 3600000 {set forever 1}; vwait forever	;# wait for 1 hour - works as well
	if [info exists forever] {unset forever}
if $testingFlag {
	after 10000 {set forever 1}; vwait forever	;# wait for 10 seconds - for testing log file managment and testing local collection integrity
} else {
	after 3600000 {set forever 1}; vwait forever	;# wait for 1 hour
}
} else {
	set i 0
	while {$i < $numberOfIterations} {	;# when not in testing mode, while lasts 1 hour
		if [info exists forever] {unset forever}
		after 2000 {set forever 1}; vwait forever	;# wait for 2 seconds
		incr i
		if [file exists $homePath/col/$URLibServiceRepository/auxdoc/@updateURLibServiceMessageForPost] {	;# created in Script (Administrator page)
			exec $homePath/updateURLibService &
			file delete $homePath/col/$URLibServiceRepository/auxdoc/@updateURLibServiceMessageForPost
		}
if 0 {
# commented by GJFB in 2020-10-05 - unpost doesn't work after the first error message: 'infinite loop?' is detected in ServeLocalCollection 
		if [file exists $homePath/col/$URLibServiceRepository/auxdoc/@unpost-kill-postMessageForPost] {	;# created in ServeLocalCollection
			exec $homePath/unpost-kill-post &	;# added by GJFB in 2020-01-22 to do unpost/post when the first error message: 'infinite loop?' is detected in ServeLocalCollection
			file delete $homePath/col/$URLibServiceRepository/auxdoc/@unpost-kill-postMessageForPost
		}
} elseif 1 {
# added by GJFB in 2020-10-05
# commented by GJFB in 2020-11-19
# added by GJFB in 2020-12-24 to post from licuri the rep-univap after a Denial of Service Attack
		if [file exists $homePath/col/$URLibServiceRepository/auxdoc/@kill-postMessageForPost] {	;# created in test2
#			exec $homePath/kill-post &	;# added by GJFB in 2020-10-15 to do kill/post when the first error message: 'infinite loop?' is detected in ServeLocalCollection
			Load $homePath/col/$URLibServiceRepository/auxdoc/@kill-postMessageForPost arg
			file delete $homePath/col/$URLibServiceRepository/auxdoc/@kill-postMessageForPost
			exec $homePath/kill-post $arg &	;# added by GJFB in 2020-12-24 to do kill/post from test2
		}
}
	}
}
# 	set seconds [clock seconds]	;# commented by GJFB in 2023-01-14 - now in ComputeFileUpdateFlag
# set xxx [clock seconds]
# Store xxx C:/tmp/bbb auto 0 a
	
# Create/update Index
	if [file exists $homePath/col/$loCoInRep/doc/index1.html] {
		if [file exists ../auxdoc/.repositoryListForPost.tcl] {
			set mtime [file mtime ../auxdoc/.repositoryListForPost.tcl]
			if ![string equal $previousRepositoryListForPostMtime $mtime] {
# the collection has been updated - the index must be updated too
# UPDATE INDEX
				if [catch {http::geturl http://$localSite/createindex.cgi} message] {
# catch error like: couln't open socket: host is unreachable - otherwise post ends
					puts [StoreLog {alert} {post (20)} "$message\n    while executing\n\"http::geturl http://$localSite/createindex.cgi\""]
				} else {
					http::cleanup $message
					set previousRepositoryListForPostMtime $mtime
					Store previousRepositoryListForPostMtime ../auxdoc/previousRepositoryListForPostMtime	;# useful when unposting URLibService
				}
			}
		}
	} else {
# CREATE INDEX
		if [catch {http::geturl http://$localSite/createindex.cgi} message] {
# catch error like: couln't open socket: host is unreachable - otherwise post ends
			puts [StoreLog {alert} {post (21)} "$message\n    while executing\n\"http::geturl http://$localSite/createindex.cgi\""]
		} else {
			http::cleanup $message
		}
	}
# Create/update Index - end

# Update enAbout page
# added by GJFB in 2021-08-04
	if [catch {http::geturl http://$localSite/col/$loBiMiRep/doc/mirror.cgi/About?languagebutton=en&forceaboutupdating=1} message] {
# catch error like: couln't open socket: host is unreachable - otherwise post ends
		puts [StoreLog {alert} {post (22)} "$message\n    while executing\n\"http://$localSite/col/$loBiMiRep/doc/mirror.cgi/About?languagebutton=en&forceaboutupdating=1\""]
	} else {
		http::cleanup $message
#		puts [StoreLog {notice} {post (22b)} {enAbout page updated}]
	}
# Update enAbout page - end

# Update pt-BRAbout page
# added by GJFB in 2021-08-04
	if [catch {http::geturl http://$localSite/col/$loBiMiRep/doc/mirror.cgi/About?languagebutton=pt-BR&forceaboutupdating=1} message] {
# catch error like: couln't open socket: host is unreachable - otherwise post ends
		puts [StoreLog {alert} {post (23)} "$message\n    while executing\n\"http://$localSite/col/$loBiMiRep/doc/mirror.cgi/About?languagebutton=pt-BR&forceaboutupdating=1\""]
	} else {
		http::cleanup $message
#		puts [StoreLog {notice} {post (23b)} {pt-BRAbout page updated}]
	}
# Update pt-BRAbout page - end

# Manage log files, test local collection integrity, save all data and update @wordOccurrence
if 0 {
# commented by GJFB in 2023-01-14 - now in ComputeFileUpdateFlag
	if [file exists ../auxdoc/@manageLogFile] {
		set mtime [file mtime ../auxdoc/@manageLogFile]
	} else {
		set mtime $startingSecond	;# force manage log files and test local collection integrity
	}
}
# puts "[expr ($seconds - $startingSecond)/$timePeriod] != [expr ($mtime - $startingSecond)/$timePeriod]"
#	if {[expr ($seconds - $startingSecond)/$timePeriod] != [expr ($mtime - $startingSecond)/$timePeriod]} #	;# commented by GJFB in 2023-01-14
## let the transition time set = {starting second + n * time period: n = 0, 1, ...}
## since the last update (mtime) at least one transition time occured
	set filePath ../auxdoc/@manageLogFile	;# added by GJFB in 2023-01-14
	set fileUpdateFlag [ComputeFileUpdateFlag $filePath $timePeriod $startingTime]	;# added by GJFB in 2023-01-14
	if $fileUpdateFlag {	;# added by GJFB in 2023-01-14

		if {$tcl_platform(platform) != "windows"} {
# Windows doesn't allow to change .log file name while Apache is running
# Manage log files
			ManageLogFile $homePath/@errorLog
			ManageLogFile $homePath/col/$URLibServiceRepository/auxdoc/serverDir/logs/access.log
			ManageLogFile $homePath/col/$URLibServiceRepository/auxdoc/serverDir/logs/error.log
# Manage log files - end
		}
				
# Test local collection integrity
#		puts {testing local collection integrity...}
		puts [StoreLog {notice} {post (24)} {testing local collection integrity...}]	;# added by GJFB in 2018-12-15
		Load $homePath/col/$loCoInRep/auxdoc/xxx data binary
		set data [UnShift $data]
		set administratorCodedPassword [lindex $data end]
		set repositoryList1 [Select2 repository {repository, *} $administratorCodedPassword]	;# Select2 extracts data from repArray
		set repositoryList2 [Select2 metadatarepository {repository, *} $administratorCodedPassword]	;# Select2 extracts data from repArray
		set repositoryList3 [list $loCoInRep $loBiMiRep]	;# permanent repositories - should not be removed
		set repositoryList4 [FindMetadataRepList $loCoInRep]	;# permanent metadata repositories - should not be removed
		set repositoryList5 [FindMetadataRepList $loBiMiRep]	;# permanent metadata repositories - should not be removed
# set xxx $repositoryList1
# Store xxx C:/tmp/bbb.txt auto 0 a
		set repositoryList [lsort -unique [concat $repositoryList1 $repositoryList2 $repositoryList3 $repositoryList4 $repositoryList5]]
		
		set incompleteMetadataList {}
		set dirList1 {}
		set dirList2 {}
		set dirList3 {}
		foreach repository $repositoryList {
			if [string equal {{}} $repository] {
				lappend incompleteMetadataList [Select3 repository {repository, *} $administratorCodedPassword]
				lappend incompleteMetadataList [Select3 metadatarepository {repository, *} $administratorCodedPassword]
			} else {
				set splitedRep [file split $repository]
				lappend dirList1 [lindex $splitedRep 0]
				lappend dirList2 [eval file join [lrange $splitedRep 0 1]]
				lappend dirList3 [eval file join [lrange $splitedRep 0 2]]
			}
		}
#		puts [lsort -unique $dirList1]
		set missingDirectoryList {}	;# no directory
		foreach dir [lsort -unique $dirList1] {
			if ![file isdirectory $homePath/col/$dir] {lappend missingDirectoryList $dir}	;# ex: dir1A
		}
		foreach dir [lsort -unique $dirList2] {
			set continueFlag 0
			foreach missingDir $missingDirectoryList {
				if [regexp ^$missingDir/ $dir] {set continueFlag 1; break}
			}
			if $continueFlag {continue}	;# dir is part of missingDir - ex: dir1A/dir2A
			if ![file isdirectory $homePath/col/$dir] {lappend missingDirectoryList $dir}	;# ex: dir1A and dir1B/dir2A
		}
		foreach dir [lsort -unique $dirList3] {
			set continueFlag 0
			foreach missingDir $missingDirectoryList {
				if [regexp ^$missingDir/ $dir] {set continueFlag 1; break}
			}
			if $continueFlag {continue}	;# dir is part of missingDir - ex: dir1A/dir2A/dir3A or dir1B/dir2A/dir3A
			if ![file isdirectory $homePath/col/$dir] {lappend missingDirectoryList $dir}	;# ex: dir1A and dir1B/dir2A and dir1B/dir2B/dir3A
		}
		foreach repository $repositoryList {
			if ![string equal {{}} $repository] {
				set continueFlag 0
				foreach missingDir $missingDirectoryList {
					if [regexp ^$missingDir/ $repository] {set continueFlag 1; break}
				}
				if $continueFlag {continue}	;# repository is part of missingDir - ex: dir1A/dir2A/dir3A/dir4A or dir1B/dir2A/dir3A/dir4A and dir1B/dir2B/dir3A/dir4A
				if ![file isdirectory $homePath/col/$repository] {lappend missingDirectoryList $repository}	;# ex: dir1A and dir1B/dir2A and dir1B/dir2B/dir3A and dir1B/dir2B/dir3B/dir4A
			}
		}
# incompleteMetadataList
		if [string equal {} $incompleteMetadataList] {
			file delete $homePath/@incompleteMetadataList
		} else {
			set list [join $incompleteMetadataList \n]
			set message "incomplete metadata\n$list"
			puts [StoreLog {alert} {post (25a)} $message]
			Store list $homePath/@incompleteMetadataList
		}
# missingDirectoryList
		if [string equal {} $missingDirectoryList] {
			file delete $homePath/@missingDirectoryList
		} else {
			set list [join $missingDirectoryList \n]
			set message "missing directories:\n$list"
			puts [StoreLog {alert} {post (25b)} $message]
			Store list $homePath/@missingDirectoryList
		}
		puts [StoreLog {notice} {post (25)} {local collection integrity tested}]
# Test local collection integrity - end

# Save All Data
		set message {saving all data...}
		puts [StoreLog {notice} {post (26)} $message]	;# added by GJFB in 2020-09-10
		Store message ../auxdoc/saveAllDataMessageForSentinel	;# added by GJFB in 2024-12-05 to avoid to trigger the kill process during the execution of the SaveAllData procedure because MultipleExecute in sentinel.tcl may not result in an OK during the execution of the SaveAllData
		SaveAllData
		file delete ../auxdoc/saveAllDataMessageForSentinel	;# added by GJFB in 2024-12-05
		puts [StoreLog {notice} {post (27)} {all data saved}]	;# added by GJFB in 2020-09-10
# Save All Data - end

# Update @wordOccurrence
# similar code as in getWordOccurrence.tcl
		puts [StoreLog {notice} {post (28)} {updating @wordOccurrence...}]	;# added by GJFB in 2020-09-10
# maximumNumberOfWords
# empty maximumNumberOfWords means to return all the keywords
if 0 {
# commented by GJFB on 2026-02-14
		if ![file exists $homePath/col/$loBiMiRep/doc/@maximumNumberOfWords] {	;# added by GJFB in 2021-06-02 - required at installation
			set fileContent {# empty maximumNumberOfWords means to return all the keywords
# set maximumNumberOfWords 30
set maximumNumberOfWords {}}
			Store fileContent $homePath/col/$loBiMiRep/doc/@maximumNumberOfWords
		}
} else {
# added by GJFB on 2026-02-14 - maximumNumberOfWords is now updated daily according to the code below
		set fileContent {# empty maximumNumberOfWords means to return all the keywords
# set maximumNumberOfWords 30
# set maximumNumberOfWords {}	;# commented by GJFB on 2026-02-14 to avoid Uncaught SyntaxError: missing name after . operator, when loading large bibliographic mirror (urlib.net or bibdigital) - this error inhibits the functioning of the cut-and-paste with easy-autocomplete
set maximumNumberOfWords 1000}	;# added by GJFB on 2026-02-14 to limit the number of keywords when loading large bibliographic mirror
		Store fileContent $homePath/col/$loBiMiRep/doc/@maximumNumberOfWords
}
		source $homePath/col/$loBiMiRep/doc/@maximumNumberOfWords	;# set maximumNumberOfWords
		if [catch {GetWordOccurrenceList $loBiMiRep $maximumNumberOfWords} wordOccurrenceList] {
			set message $errorInfo
			Store message $homePath/col/$loBiMiRep/doc/@log auto 0 a
			set message {warning: the word ocurrence list has not been updated}
			Store message $homePath/col/$loBiMiRep/doc/@log auto 0 a
		} else {	;# else added by GJFB in 2023-02-08 - otherwise 'Store wordOccurrenceList' below might store an error message like 'invalid bareword "rep"' and result to an error while executing GetWordOccurrenceList the next day
			if ![string equal {} $wordOccurrenceList] {
				Store wordOccurrenceList $homePath/col/$loBiMiRep/doc/@wordOccurrence auto 0 w 0 iso8859-1	;# solves the accent problem at mtc-m12 (with utf-8 encoding system) - added by GJFB in 2016-08-06
			} else {
				set message {$wordOccurrenceList is empty}
				Store message $homePath/col/$loBiMiRep/doc/@log auto 0 a
				set message {warning: the word ocurrence list has not been updated}
				Store message $homePath/col/$loBiMiRep/doc/@log auto 0 a
			}
		}
		set metadataRep [FindMetadataRep $loBiMiRep]
		regsub {@.*$} $environmentArray(spMailEntry) {} administratorUserName
		TestUpdateLastUpdate $loBiMiRep $metadataRep 1 $administratorUserName	;# because of the new @wordOccurrence file
		puts [StoreLog {notice} {post (29)} {@wordOccurrence updated}]	;# added by GJFB in 2020-09-10
# Update @wordOccurrence - end

# Save and Unset unfairAddrList
		if {$tcl_platform(platform) == "unix"} {
# unfairAddrList is updated in AcceptCommunication
			if [info exists unfairAddrList] {	
				Store unfairAddrList $homePath/col/$URLibServiceRepository/auxdoc/@unfairAddrList
				puts [StoreLog {notice} {post (30)} "the unfairAddrList was $unfairAddrList\nunfairAddrList unset"]	;# added by GJFB in 2020-12-12
				unset unfairAddrList
			}
		}
# Save and Unset unfairAddrList - end

# Update col/$loCoInRep/doc/@index.hml (All in one page)
# added by GJFB in 2023-03-10
# http://gjfb:1905/ibi/dpi.inpe.br/banon/1999/01.09.22.14
		if [catch {http::geturl http://$localSite/ibi/$loCoInRep} message] {
# catch error like: couln't open socket: host is unreachable - otherwise post ends
			puts [StoreLog {alert} {post (31)} "$message\n    while executing\n\"http://$localSite/ibi/$loCoInRep\""]
		} else {
			http::cleanup $message
#			puts [StoreLog {notice} {post (31b)} {@index.hml (All in one page) updated}]
		}
# Update col/$loCoInRep/doc/@index.hml (All in one page) - end

		set fileContent {could be anything}
		Store fileContent ../auxdoc/@manageLogFile
	}
# Manage log files, test local collection integrity, save all data and update @wordOccurrence - end

} ;# end of while

} m] {
#	set log "post (23): $errorInfo"
#	Store log $homePath/@errorLog auto 0 a
#	puts $log	;# useful with unix
	puts [StoreLog {error} {post (32)} $errorInfo]
	set x4 0; after 40000 {set x4 1}; vwait x4	;# wait 40 seconds
}
