# This is an example of a Tcl script used with mq to edit a large
# (~380) collection of metadata records in batch mode.

# Here's the problem:  Metadata producers, including me, have
# indicated the price of certain USGS products in fixed dollar
# amounts (for example $32.00 per CD-ROM, with $3.50 handling fee).
# But these prices are subject to change, and they change every
# few years.  There isn't a direct linkage that can be used to
# obtain the current pricing information, and that depends also
# on the volume of the order, whether international shipping is
# needed, and so forth.  Consequently it is better to provide a
# WWW address for pricing information rather than a specific
# dollar amount in the Fees element.
#
# Fees occurs singly within Standard_Order_Process, which can
# repeat in Distribution_Information, which can repeat in Metadata.
#
# The value "none" is commonly used in Fees to indicate that a
# data set is available without direct charge through the internet.
# This value need not change.
#
# So technically, the problem is
#   Read the standard configuration file to get extension info
#   Find each metadata record
#     Read the record
#     For each Distribution_Information
#       For each Standard_Order_Process
#         Find Fees element
#         If there is one, and its value is not "none"
#         then replace the value with the internet address
#              of the USGS product pricing page.
#         If you successfully changed the value, remember that
#         this record was changed.
#         Go to the next Standard_Order_Process
#       Go to the next Distribution_Information
#     If any Fees values were changed, rewrite the metadata
#     record using the same disk file name.
#     Go to the next metadata record
#
# The implementation of this method is given below as Tcl code
# that is provided as input to mq.
#
# Peter N. Schweitzer (U.S. Geological Survey, Reston, VA 20192)

package require mq

proc process_directory {long_name short_name} {
    recurse $long_name
    }

proc process_file {long_name short_name} {
    global new_value
    if {[string compare [file extension $short_name] .met] == 0} {
        if {[metadata m -parse $short_name]} {
            puts stdout "$short_name"
            set changed 0
            set p [$m find_first Distribution_Information]
            while {$p} {
                set q [$m find_in $p Standard_Order_Process]
                while {$q} {
                    set r [$m find_in $q Fees]
                    if {$r} {
                        set value [$m value_of $r]
                        puts stdout "  $value"
                        if {[string compare $value none] != 0} {
                            set ok [$m value_set $r $new_value]
                            if {$ok} {
                                set changed 1
                                puts stdout "--OK: value at $r changed"
                                } \
                            else {
                                puts stdout "--ERROR: could not set value at $r"
                                }
                            }
                        }
                    set q [$m find_next $q]
                    }
                set p [$m find_next $p]
                }
            if {$changed} {$m write -format text $short_name}
            $m forget
            }
        }
    }

proc recurse {dir} {
    global root
    foreach long_name [lsort [glob [file join $dir *]]] {
        scan $long_name "$root/%s" short_name
        if {[file isdirectory $long_name]} {
            process_directory $long_name $short_name
            } \
        else {
            process_file $long_name $short_name
            }
        }
    }

config read standard.cfg
set new_value "See http://mapping.usgs.gov/esic/prices/index.html"
set root [pwd]
set short_name .
recurse $root

# end of Tcl code
