#
# Copyright for the scripts in this file (c) 2005 - 2021
# by Gerald Banon - INPE. All rights reserved.

# This script processes the INPE Clipping submitted files

if 0 {
source ../../../../../iconet.com.br/banon/2005/12.29.23.16/doc/script.tcl
set homePath {C:/Gerald/URLib 2}
set URLibServiceRepository dpi.inpe.br/banon/1998/08.02.08.56
set pwd {C:/Gerald/URLib 2/col/dpi.inpe.br/banon/1998/08.02.08.56/doc}
source utilities1.tcl
source cgi/mirrorfind-.tcl
}

namespace eval iconet.com.br/banon/2005/12.29.23.16 {

# >
# ----------------------------------------------------------------------
# Script
# reference example:
# {%0 Newspaper Article} {%B O Globo} {%3 clipping.html} {%T Exemplo}
# Script is sourced ONCE (at the first use) in RunPostSubmissionScript (see DDDialog.tcl) unless the line 'for testing Script' were uncommented in RunPostSubmissionScript
# Script is executed in RunPostSubmissionScript (see DDDialog.tcl) at submission and update (no file upload is required)
# the appropriate displayControl.tcl must contain the line script:
# array set postSubmissionProcessTable {{Newspaper Article} {iconet.com.br/banon/2005/12.29.23.16}}

proc Script {rep reference} {
# runs with post
	global homePath
	global pwd
#	upvar 0 rep currentRep	;# used by GetFrameName
	set thisRepository iconet.com.br/banon/2005/12.29.23.16
	set docPath $homePath/col/$rep/doc
# set docPath $rep
#	set xxx OK
#	Store xxx $docPath/clipping.html
	set newspaper {}
	set pages {}
	set issuedate {}
	foreach field $reference {
		if {[string compare {%B} [lindex $field 0]] == 0} {set newspaper [lrange $field 1 end]}
		if {[string compare {%P} [lindex $field 0]] == 0} {set pages [lrange $field 1 end]}
		if {[string compare {%8} [lindex $field 0]] == 0} {set issuedate [lrange $field 1 end]}
	}
	if [regexp {[-,;]} $pages] {
		set pageFieldName Páginas
	} else {
		set pageFieldName Página
	}
	if [regexp {^(\d{4})-(\d{2})} $issuedate m year month] {
		if [regexp {^\d{4}-\d{2}-(\d{2})$} $issuedate m day] {
			set issuedate $day-$month-$year
		} else {
			set issuedate $month-$year
		}
	}

# id 6qtX3pFwXQZ34L4SeY/R7iKE		
# -rw-rw-r--. 1 root       urlib 82439 Aug 10  2007 delega?ao da nasa.pdf
# -rw-rw-r--. 1 bibdigital urlib 82438 Jul 24 20:48 delegaçao da nasa - Copia.pdf
# [root@md-m09 URLibFOTO]# /usr/bin/tclsh
# % encoding system
# utf-8
# % set x0 [lindex [glob del*] 0]
# delegaçao da nasa.pdf
# % set x1 [lindex [glob del*] 1]
# delegaçao da nasa - Copia.pdf
# % file exists $x0
# 0
# % file exists $x1
# 1
# % encoding system iso8859-1
# % file exists $x0
# 1
# % file exists $x1
# 0
# % info patchlevel
# 8.5.7
# % encoding system utf-8; set x0 [lindex [glob del*] 0]; encoding system iso8859-1; file exists $x0
# 1
# this line fails:
# % encoding system utf-8; set x0 [lindex [glob del*] 0]; file exists $x0; encoding system iso8859-1; file exists $x0
# 0
 
	cd $docPath
	set fileList [glob -nocomplain {*.[jJ][pP][gG]}]
	set fileList [concat $fileList [glob -nocomplain {*.[pP][dD][fF]}]]
	set encodingSystem [encoding system]	;# to preserve the current encoding system
	encoding system iso8859-1	;# this line and the four below must be place here just after glob otherwise the 'file exists' just below doesn't work properly for an unknown reason
	set flagList {}
	foreach file $fileList {
		lappend flagList [file exists $file]	;# added by GJFB in 2021-07-24 - the 'file exists' command result depends on the encoding system - required to reproduce the successful coding in CreateDirectoryContentList
	}
	encoding system $encodingSystem	;# to preserve the current encoding system	
	set clippingList [glob -nocomplain {clipping*.html}]
	if {[llength $clippingList] != [llength $fileList]} {
		foreach clipping $clippingList {file delete $clipping}
	}
	set headerList [glob -nocomplain {header*.html}]
	if {[llength $headerList] != [llength $fileList]} {
		foreach header $headerList {file delete $header}
	}
	cd $pwd
# puts $fileList
	Load $homePath/col/$thisRepository/doc/ClippingModel.tcl clippingFileContent
	Load $homePath/col/$thisRepository/doc/HeaderModel.tcl headerFileContent
#	Load $homePath/col/$thisRepository/doc/PageModel.tcl pageFileContent	;# for use with iframe - add one more scroll bar
	if {[llength $fileList] == 1} {
		set cuts {}
		set height 95
		set headerName header1.html
		set fileName [join [lindex $fileList 0]]
		set convertToUTF8 [expr [string equal {utf-8} $encodingSystem] && ![lindex $flagList 0]]	;#  solves the accent problem when file names are coded differently (iso and utf) from one directory to another (in consequence of a migration between different operating systems (iso and utf)) - added by GJFB in 2021-07-24 - similar to what was done in 2013-09-01 (see CreateDirectoryContentList)
		set fileName [ConvertURLToHexadecimal $fileName $convertToUTF8]	;# added by GJFB in 2021-07-22 - solves the accent problem (ex: id 8JMKD3MGPGW/3U9N76B - m09)

		if [regexp {\.jpg$} $fileName] {
			Load $homePath/col/$thisRepository/doc/BodyModel.tcl bodyFileContent
			set image $fileName
			set fileContent [subst $bodyFileContent]
			Store fileContent $docPath/body1.html
			set fileName body1.html
			set openComment {}
			set closeComment {}
		} else {
# .pdf
			set openComment {}
		}
#		set fileContent [subst $pageFileContent]
		set fileContent [subst $clippingFileContent]
		Store fileContent $docPath/clipping1.html
		set fileContent [subst $headerFileContent]
#		Store fileContent $docPath/header1.html	;# commented by GJFB in 2015-12-15
		Store fileContent $docPath/header1.html binary	;# added by GJFB in 2015-12-15 - binary solves the accent problem
	} else {
		set height 130
		set i 1
		foreach file $fileList {
			lappend fullPageList $i
			incr i
		}
		set i 1
		foreach file $fileList {
			set j 1
			set cutList {}
			foreach file $fileList {
				if {$i == $j} {
					lappend cutList $j
				} else {
					lappend cutList "$j"
				}
				incr j
			}
			set cuts "Veja os recortes: [join $cutList { | }] 

" set headerName header$i.html set fileName [join [lindex $fileList [expr $i - 1]]] set flag [lindex $flagList [expr $i - 1]] set convertToUTF8 [expr [string equal {utf-8} $encodingSystem] && !$flag] ;# solves the accent problem when file names are coded differently (iso and utf) from one directory to another (in consequence of a migration between different operating systems (iso and utf)) - added by GJFB in 2021-07-24 - similar to what was done in 2013-09-01 (see CreateDirectoryContentList) set fileName [ConvertURLToHexadecimal $fileName $convertToUTF8] ;# added by GJFB in 2021-07-22 - solves the accent problem (ex: id 8JMKD3MGPGW/3U9N76B - m09) if [regexp {\.jpg$} $fileName] { Load $homePath/col/$thisRepository/doc/BodyModel.tcl bodyFileContent set image $fileName set fileContent [subst $bodyFileContent] Store fileContent $docPath/body$i.html set fileName body$i.html set openComment {} set closeComment {} } else { # .pdf set openComment {} } # set fileContent [subst $pageFileContent] set fileContent [subst $clippingFileContent] Store fileContent $docPath/clipping$i.html set fileContent [subst $headerFileContent] # Store fileContent $docPath/header$i.html ;# commented by GJFB in 2015-12-1 Store fileContent $docPath/header$i.html binary ;# added by GJFB in 2015-12-15 - binary solves the accent problem incr i } } return clipping1.html ;# target file name } # Script - end # ---------------------------------------------------------------------- # < } if 0 { set reference {{%0 Newspaper Article} {%B O Globo} {%3 clipping.html} {%T Exemplo}} set rep iconet.com.br/banon/2005/12.29.22.47 iconet.com.br/banon/2005/12.29.23.16::Script $rep $reference } #