# Copyright for the Uniform Repository Service (c) 1995 - 2013,
# by Gerald Banon. All rights reserved.
# Version 2.1
# export.tcl

# exports bibliographic references

# cgi inputs used by Export:
# username
# session

proc Export {} {
if [catch {
	global env
#	global cgi	;# used in CheckUsernamePasswordForm - commented by GJFB in 2014-06-10 - CheckUsernamePasswordForm is not used
	global homePath	;# used in GetSessionList (see CheckSession)
	global serverAddress	;# used in SetFieldValue
	global URLibServiceRepository	;# used in CheckSession
	global localSite	;# used when calling FindSite2
	
	fconfigure stdout -translation binary

	set col ../../../../..
	set URLibServiceRepository $env(URLIB_SERVICE_REP)
	source ../$col/$URLibServiceRepository/doc/utilities1.tcl
	source ../$col/$URLibServiceRepository/doc/cgi/mirrorfind-.tcl
	
	foreach {name value} [split $env(QUERY_STRING) &=] {
		set cgi([DecodeURL $name]) [DecodeURL $value]
	}
	
	CreateCGIArray 1
	
# homePath
	set homePath $env(DOCUMENT_ROOT)
# serverAddress
	set serverAddress [list $env(SERVER_NAME) $env(URLIB_PORT)]
## serverAddressWithIP
#	set serverAddressWithIP [list $env(IP_ADDR) $env(URLIB_PORT)]	;# used in CheckUsernamePasswordForm - commented by GJFB in 2014-06-10 - atually CheckUsernamePasswordForm is not used
# localSite
	set localSite $env(SERVER_NAME):$env(SERVER_PORT)

	set currentRep $cgi(mirrorrepository)
	
# Find the language and the language repository
# use the same languages as used for the local bibliographic mirror
	foreach {language languageRep1 languageRep2 firstLanguageRep \
	submissionFormRep submissionFormLanguage submissionFormLanguageRep} \
	[FindLanguage $currentRep] {break}

	source ../$col/$languageRep2/doc/mirror/${submissionFormLanguage}Submit.tcl
	global "${languageRep2}::Nothing checked"
	global "${languageRep2}::Return"

if 0 {
	puts {Content-Type: text/html}
	puts {}
#	puts [array get cgi]
}
	
# Check session
	if {![info exists cgi(username)] || \
	![info exists cgi(session)] || \
	[CheckSession $cgi(session) $cgi(username)]} {
# no session
		puts {Content-Type: text/html}
		puts {}
		puts "Export: permission denied."
		return
	}
# Check session - end

# Ckeck the form
	if ![info exist cgi(checkeditemlist)] {
		puts {Content-Type: text/html}
		puts {}
		puts "
			<HTML>
				<HEAD>
					<TITLE>Exportação de referências bibliográficas referentes ao ano de currentYear</TITLE>
					<link rel=\"stylesheet\" type=\"text/css\" href=\"col/$languageRep2/doc/mirrorStandard.css\">
				</HEAD>
				<BODY>
					<BR><BR>
					<P ALIGN=\"CENTER\"><!-- Export: -->${Nothing checked}.</P>
					<BR>
					<TABLE ALIGN=CENTER><TR><TD WIDTH=200><HR></TD></TR></TABLE>
					<FORM>
						<P ALIGN=\"CENTER\">
							<INPUT style=\"font-family: Verdana; font-size: 11px; color: #444444; width: auto\" TYPE=\"button\" VALUE=\"$Return\" ONCLICK=\"history.back()\">
						</P>
					</FORM>
				</BODY>
			</HTML>
		"
		return
	}
# Ckeck the form - end

	set wantedRepository $cgi(conversionrepository)	;# example: Lattes to URLib metadata conversion repository 
#	set siteContainingConversionProcedure [ReturnHTTPHost [lindex [FindSite2 $wantedRepository] 0]]
	set siteContainingConversionProcedure [lindex [FindSite2 $wantedRepository] 0]
	Source http://$siteContainingConversionProcedure/col/$wantedRepository/doc/makeMetadataConversion.tcl	;# create MakeMetadataConversion
	
# puts $cgi(checkeditemlist)
 
#	puts --[MakeMetadataConversion $cgi(cpf) $cgi(checkeditemlist)]--
	
	set fileContent [MakeMetadataConversion $cgi(cpf) $cgi(checkeditemlist)]

	puts "Content-disposition: attachment; filename=import.xml"
	puts {}
	puts $fileContent

} m] {
	if ![string equal {} $m] {
		puts {Content-Type: text/html}
		puts {}
		puts $m
		if 0 {global errorInfo; puts $errorInfo}
	}
}
}
