#
# Copyright for the scripts in this file (c) 2001
# by Gerald Banon. All rights reserved.

# BibPessoal2Refer converts BibPessoal format to refer/BiblX format (see EndNote)
# input and output are in txt format

package provide iconet.com.br/banon/2001/11.24.15.12 2.1

namespace eval iconet.com.br/banon/2001/11.24.15.12 {

# >

# ----------------------------------------------------------------------
# BibPessoal2Refer
# convert values are 0 or 1
# 0 means to do not convert
# 1 means to convert

proc BibPessoal2Refer {fileContent convert} {
#	global parsingRepository
	global bibpessoal2referRepository
	global col

# puts [info exists "${bibpessoal2referRepository}::b2r(Periódico (Artigo))"]
#	if ![info exists "${bibpessoal2referRepository}::b2r(Periódico (Artigo))"] {
#		source $col/$bibpessoal2referRepository/doc/bibTables.tcl
#	}
# puts [info exists "${bibpessoal2referRepository}::b2r(Periódico (Artigo))"]

#	if ![info exists dpi.inpe.br/banon/1999/08.08.19.14::orderingTable] {
#		source $col/dpi.inpe.br/banon/1999/08.08.19.14/doc/referTables.tcl
## doesn't work because source is executed within a namespace
#	}

#	if {[string compare {} [info procs ${parsingRepository}::ParseFullName]] == 0} {
#		source $col/$parsingRepository/doc/Parsing.tcl
#	}

	global ${bibpessoal2referRepository}::b2r
	global ${bibpessoal2referRepository}::b2rTable

# puts 1-$fileContent

#	regsub -all {Periądico} $fileContent {Periódico} fileContent

	if {[string compare {} [info procs iconet.com.br/banon/2001/12.08.15.34::ConvertEncodings]] == 0} {
		source $col/iconet.com.br/banon/2001/12.08.15.34/doc/Converting.tcl
	}

	if $convert {set fileContent [iconet.com.br/banon/2001/12.08.15.34::ConvertEncodings $fileContent 0]}

# puts 2-$fileContent

	set fileContent [string trim $fileContent \n]
	set entryList [split $fileContent \n]
	set columnNames [lindex $entryList 0]
	set columnNames [split $columnNames \t]
	set numberOfColumns [llength $columnNames]
# puts $columnNames
# puts $numberOfColumns
# Drop returns between quotes
	set flag 0
	foreach character [split $fileContent {}] {
		if $flag {
			if {[string compare \n $character] == 0} {continue}
			if {[string compare {"} $character] == 0} {set flag 0}
		} else {
			if {[string compare {"} $character] == 0} {set flag 1}
		}
		append newFileContent $character
	}
# Drop returns between quotes - end
	set cellList [split $newFileContent \t\n]
# puts [llength $cellList]
	set refer {}
	set index 0
	foreach $columnNames [lrange $cellList $numberOfColumns end] {
		set x 0; after 1 {set x 1}; vwait x	;# nice procedure
		incr index
		set referEntry {}
		lappend referEntry "%@index $index"
# puts --$Tipos--
		regsub {[12]$} $b2r($Tipos) {} referenceType
#		lappend referEntry "%0 $b2r($Tipos)"
		lappend referEntry "%0 $referenceType"
		foreach fieldName $columnNames {
			set fieldName2 "{$fieldName}"
			set fieldValue [subst $$fieldName2]
# puts [list $fieldName = $fieldValue
			if {[string compare {Tipos} $fieldName] == 0} {continue}
			if {[string compare {Extra} $fieldName] == 0} {continue}
			if {[string compare {CDD} $fieldName] == 0} {continue}
			if {[string compare {NormPers} $fieldName] == 0} {continue}
			if {[string compare {Autores} $fieldName] == 0} {
				regsub -all {"} $fieldValue {} fieldValue
# puts $fieldValue
				foreach name [split $fieldValue \;] {
					regsub {([a-z][a-z\)])\.$} $name {\1} name	;# drop trailing point (unless it is point after a capital letter, eg., L.S.
					regsub {$} $name {,} name	;# add comma
					lappend referEntry \
						"$b2rTable($Tipos,$fieldName) [string trim $name]"
				}
			} elseif {[string compare {PCS} $fieldName] == 0} {
				if {[string compare {} $fieldValue] == 0} {
					set keyWords {}
				} else {
					set keyWords $fieldValue.
				}
				lappend referEntry \
					"$b2rTable($Tipos,$fieldName) $keyWords"
			} else {
				if ![info exists b2rTable($Tipos,$fieldName)] {continue}
				lappend referEntry \
					"$b2rTable($Tipos,$fieldName) $fieldValue"
			}
		}
# puts $referEntry
		lappend refer [join \
			[lsort -command FieldCompare $referEntry] \n]
		lappend refer ""			
	}
	regsub -all {\\"} $refer {"} refer
	return [join $refer \n]
}

# BibPessoal2Refer - end
# ----------------------------------------------------------------------
# FieldCompare

proc FieldCompare {a b} {
	global dpi.inpe.br/banon/1999/08.08.19.14::orderingTable
	set a0 [lindex $a 0]	;# doesn't work with "2000, {K}orea" in $a (list element in braces followed by "orea" instead of space)
	set b0 [lindex $b 0]
#	regexp "%\[^ \]*" $a a0
#	regexp "%\[^ \]*" $b b0
	if {[info exists orderingTable($a0)] && \
		[info exists orderingTable($b0)]} {
		return [expr $orderingTable($a0)<=$orderingTable($b0)?-1:1]
	}
	if {[info exists orderingTable($a0)] && \
		! [info exists orderingTable($b0)]} {
		return -1
	}
	if {! [info exists orderingTable($a0)] && \
		[info exists orderingTable($b0)]} {
		return 1
	}
	if {! [info exists orderingTable($a0)] && \
		! [info exists orderingTable($b0)]} {
		return [string compare $a0 $b0]
	}
}

# FieldCompare - end
# ----------------------------------------------------------------------

# <

}

# Example:
# cd ../../../../../iconet.com.br/banon/2001/11.24.15.12/doc
# source bibpessoal2refer.tcl
#
# console show
# set parsingRepository iconet.com.br/banon/2001/11.24.08.30
# set bibpessoal2referRepository iconet.com.br/banon/2001/11.24.15.12
# set col ../../../../..
# source bibTables.tcl
# source ../../../../../dpi.inpe.br/banon/1999/08.08.19.14/doc/referTables.tcl
# set fileId [open c:/usuario/gerald/URLib/col/iconet.com.br/banon/2001/11.24.15.58/doc/@reference.bp1 r]
# set fileId [open c:/usuario/gerald/URLib/col/iconet.com.br/banon/2001/12.08.21.43/doc/@reference.bp2 r]
# fconfigure $fileId -translation binary	;# only with .bp2
# set fileContent [read $fileId]
# close $fileId
# puts [${bibpessoal2referRepository}::BibPessoal2Refer $fileContent 0]	;# with .bp1
# puts [${bibpessoal2referRepository}::BibPessoal2Refer $fileContent 1]	;# with .bp2

#