#
# Copyright for URLibService (c) 1995 - 2020,
# by Gerald Banon. All rights reserved.
# Extracts a new version of URLibService

# ----------------------------------------------------------------------
# ExtractURLibService
# used by post and start

proc ExtractURLibService {} {
	global homePath
	global tcl_platform
#	global unZipExec
	global unZipRepository	;# set in this procedure and used in InstallRepository and ControlBCButtonState
	global unZipPath	;# set in LoadGlobalVariables
	global URLibServiceRepository
#	global downloadingEnvironmentRepository	;# not used any more after 2020-04

	if {$tcl_platform(platform) == "windows"} {
		set unZipExec unzip
		set unZipRepository dpi.inpe.br/banon/2000/08.09.16.24
# ...		set unZipRepository dpi.inpe.br/banon-pc2@1905/2005/05.06.20.41
	}
	if {$tcl_platform(platform) == "unix"} {
		if {$tcl_platform(os) == "SunOS"} {
			if [file exists /usr/bin/unzip] {
#				set unZipPath /usr/bin/unzip	;# now in knownPathArray
				set unZipRepository {}
			} else {
				set unZipExec unzip
				set unZipRepository dpi.inpe.br/banon/2000/08.09.17.10
			}
		} elseif {$tcl_platform(os) == "Linux"} {
#			set unZipPath /usr/bin/unzip	;# now in knownPathArray
			set unZipRepository {}
		} elseif {$tcl_platform(os) == "FreeBSD"} {
#			set unZipPath /usr/local/bin/unzip	;# now in knownPathArray
			set unZipRepository {}
		}
	}
## unZipPath
	if [file isdirectory $homePath/col/$unZipRepository/doc] {
#		set unZipPath $homePath/col/$unZipRepository/doc/$unZipExec	;# must be homePath - now in knownPathArray
		if {$tcl_platform(os) == "SunOS"} {
			if ![file executable $homePath/col/$unZipRepository/doc/$unZipExec] {
				if [file owned $homePath/col/$unZipRepository/doc/$unZipExec] {
					exec chmod 775 $homePath/col/$unZipRepository/doc/$unZipExec
				}
			}
		}
	}

	if {[file isdirectory $homePath/col/$URLibServiceRepository/auxdoc] && \
	[file exists $homePath/doc.zip] && \
	![file exists ../auxdoc/serverDir/logs/virtualHostRunning]} {
# doc.zip exists and the virtual host is not running
# UNZIP - EXTRACT packedRepositoryVersion
#		if ![catch {exec $unZipPath -o\
#		$homePath/doc.zip packedRepository packedRepositoryVersion -d \
#		$homePath/col/$URLibServiceRepository/auxdoc}] #	;# commented by GJFB in 2020-05-026
		if ![catch {exec $unZipPath -o\
		$homePath/doc.zip packedRepository packedRepositoryVersion -d $homePath}] {	;# added by GJFB in 2020-05-026 - simplification
# packedRepository packedRepositoryVersion extracted
#			SimpleLoad $homePath/col/$URLibServiceRepository/auxdoc/packedRepository packedRepository	;# commented by GJFB in 2020-05-026
#			SimpleLoad $homePath/col/$URLibServiceRepository/auxdoc/packedRepositoryVersion packedRepositoryVersion	;# commented by GJFB in 2020-05-026
			SimpleLoad $homePath/packedRepository packedRepository	;# added by GJFB in 2020-05-026 - simplification
			SimpleLoad $homePath/packedRepositoryVersion packedRepositoryVersion	;# added by GJFB in 2020-05-026 - simplification
#			if {[string equal $URLibServiceRepository $packedRepository] || \
#			[string equal $downloadingEnvironmentRepository $packedRepository]} #
## doc.zip contains URLibServiceRepository or downloadingEnvironmentRepository
			if [string equal $URLibServiceRepository $packedRepository] {
# doc.zip contains URLibServiceRepository
				SimpleLoad $homePath/col/$packedRepository/service/history history
				set versionStamp [lindex $history end]	;# GetVersionStamp cannot be used here because at installation the global variable repositoryProperties doesn't exist yet
				if ![string equal $versionStamp $packedRepositoryVersion] {
# doc.zip contains a different version
# UNZIP
					puts "extracting $packedRepository..."
					if [catch {exec $unZipPath -ao $homePath/doc.zip -d $homePath} m] {
						puts "$packedRepository not extracted: $m"
					} else {
						puts "$packedRepository extracted"
						MakeDownloadFile $URLibServiceRepository	;# added by GJFB in 2020-04-22 - doc.zip (if any) must be updated here in order to get the right size of it in the 'Downloading the URLibService Environment' pages
					}
					if {$tcl_platform(platform) == "windows"} {
						set delay 0; after 2000 {set delay 1}; vwait delay	;# wait to provide a delay to display the above messages
					}
					if {$tcl_platform(platform) == "unix"} {
						exec chmod +x $homePath/col/$URLibServiceRepository/doc/start
						exec chmod +x $homePath/col/$URLibServiceRepository/doc/post
						if [file exists $homePath/tempDoc.zip] {
# tempDoc may be created by post -- -x -u
# restore doc.zip
							file rename -force $homePath/tempDoc.zip $homePath/doc.zip
							file delete $homePath/tempDoc.zip
						}
					}
				}
			}
		}
	}
}

# ExtractURLibService - end
# ----------------------------------------------------------------------
# SimpleLoad

proc SimpleLoad {filePath varName} {
	upvar $varName var
	if [catch {open $filePath r} fileId] {
		set var {}
	} else {
		set var [string trim [read $fileId] \n]
		close $fileId
	}
}

# SimpleLoad - end
# ----------------------------------------------------------------------