#!/bin/sh
# \
  exec oagtclsh "$0" "$@"

#
# Tcl script to copy files to appropriate patch directories
#

set auto_path [linsert $auto_path 0  /usr/local/oag/apps/lib/$env(HOST_ARCH)]
set auto_path [linsert $auto_path 0 /usr/local/oag/lib_patch/$env(HOST_ARCH)]

set mkindexpatch 0

proc aps_auto_mkindex {dir args} {
    global errorCode errorInfo

    if {[interp issafe]} {
        error "can't generate index within safe interpreter"
    }

    set oldDir [pwd]
    cd $dir
    set dir [pwd]

    append index "# Tcl autoload index file, version 2.0\n"
    append index "# This file is generated by the \"auto_mkindex\" command\n"
    append index "# and sourced to set up indexing information for one or\n"
    append index "# more commands.  Typically each line is a command that\n"
    append index "# sets an element in the auto_index array, where the\n"
    append index "# element name is the name of a command and the value is\n"
    append index "# a script that loads the command.\n\n"
    if {$args == ""} {
        set args *.tcl
    }

    auto_mkindex_parser::init
    foreach file [eval glob $args] {
        if {[catch {auto_mkindex_parser::mkindex $file} msg] == 0} {
            append index $msg
        } else {
            set code $errorCode
            set info $errorInfo
            cd $oldDir
            error $msg $info $code
        }
    }
    auto_mkindex_parser::cleanup

    set fid [open "tclIndex.new" w]
    puts -nonewline $fid $index
    close $fid
    cd $oldDir
}


# **************************** copyall *********************************
proc copyall {args} {
    set type ""
    set file ""
    set location ""
    set log 0
    set email 0
    APSStrictParseArguments {type file location log email}

    global lDir bDir pempatch mkindexpatch libpatchDir binpatchDir forceBin
    global beta comment commentArray heliosAndPhoebus heliosPrefix phoebusPrefix
    if {$beta} {
        set b "-beta"
    } else {
        set b ""
    }
    if {$forceBin} {
        set type reg
    }

    switch -exact $type {
        lib {
            if {$beta} {
                puts " *** Error: Beta version are not allowed for Tcl files: $file"
                return
            }
            if {$heliosAndPhoebus} {
                safecopy -from $file -to ${heliosPrefix}${lDir}/$file -log $log -email $email -type tcl
                safecopy -from $file -to ${phoebusPrefix}${lDir}/$file -log $log -email 0 -type tcl
                if {([info exists pempatch]) && ([file exists $pempatch])} {
                    safecopy -from $file -to $pempatch/$file -log 0 -email 0 -type tcl
                }
                if {[info exists libpatchDir]} {
                    if {[file exists ${heliosPrefix}${libpatchDir}/$file]} {
                        set mkindexpatch 1
                        if {[catch {exec rm -f ${heliosPrefix}${libpatchDir}/$file} errormsg]} {
                            puts " *** Error: $errormsg"
                        }
                    }
                    if {[file exists ${phoebusPrefix}${libpatchDir}/$file]} {
                        set mkindexpatch 1
                        if {[catch {exec rm -f ${phoebusPrefix}${libpatchDir}/$file} errormsg]} {
                            puts " *** Error: $errormsg"
                        }
                    }
                }
            } else {
                safecopy -from $file -to ${lDir}/$file -log 0 -email 0 -type tcl
            }
        }
        py {
            if {$beta} {
                puts " *** Error: Beta version are not allowed for Python files: $file"
                return
            }
            if {$heliosAndPhoebus} {
                safecopy -from $file -to ${heliosPrefix}${lDir}/$file -log $log -email $email -type py
                safecopy -from $file -to ${phoebusPrefix}${lDir}/$file -log $log -email 0 -type py
            } else {
                safecopy -from $file -to ${lDir}/$file -log 0 -email 0 -type py
            }

        }
        sdds {
            if {$beta} {
                puts " *** Error: Beta version are not allowed for SDDS files: $file"
                return
            }
            if {[catch {exec sdds2stream $file -parameter=InstallLocation -page=1} installLocation]} {
                puts " *** Error: $file does not have an InstallLocation parameter"
                return
            }
            if {[string length $comment] == 0} {
                if {[info exists commentArray([file tail $file])]} {
                    set commentArray([file tail $installLocation]) $commentArray([file tail $file])
                } else {
                    set commentArray([file tail $installLocation]) ""
                }
            }
            if {$heliosAndPhoebus} {
                safecopy -from $file -to $installLocation -log $log -email $email -type sdds
            } else {
                safecopy -from $file -to $installLocation -log 0 -email 0 -type sdds
            }
        }
        reg {
            if {$heliosAndPhoebus} {
                safecopy -from $file -to ${heliosPrefix}${bDir}/${file}$b -log $log -email $email -type program
                safecopy -from $file -to ${phoebusPrefix}${bDir}/${file}$b -log $log -email 0 -type program
                if {[info exists binpatchDir]} {
                    if {[file exists ${heliosPrefix}${binpatchDir}/${file}$b]} {
                        if {[catch {exec rm -f ${heliosPrefix}${binpatchDir}/${file}$b} errormsg]} {
                            puts " *** Error: $errormsg"
                        }
                    }
                    if {[file exists ${phoebusPrefix}${binpatchDir}/${file}$b]} {
                        if {[catch {exec rm -f ${phoebusPrefix}${binpatchDir}/${file}$b} errormsg]} {
                            puts " *** Error: $errormsg"
                        }
                    }
                }
            } else {
                safecopy -from $file -to ${bDir}/${file}$b -log 0 -email 0 -type program
            }
        }
        ascii {
            if {$beta} {
                puts " *** Error: Beta version are not allowed for Ascii files: $file"
                return
            }
            if {[string length $comment] == 0} {
                if {[info exists commentArray([file tail $file])]} {
                    set commentArray([file tail $location]) $commentArray([file tail $file])
                } else {
                    set commentArray([file tail $location]) ""
                }
            }
            if {$heliosAndPhoebus} {
                safecopy -from $file -to $location -log $log -email $email -type ascii
            } else {
                safecopy -from $file -to $location -log 0 -email 0 -type ascii
            }
        }
    }
}
# **************************** safecopy ********************************
proc safecopy {args} {
    global tcl_platform
    set from ""
    set to ""
    set log ""
    set email ""
    set type ""
    APSStrictParseArguments {from to log email type}
    set pwd [exec pwd]

    if {[file exists $from]} {
        set mtimefrom [file mtime $from]
    } else {
        puts " *** Error: $from does not exist"
        return
    }

    set applicationName unknown
    set previousVersion unknown
    set newVersion unknown

    if {[file exists $to]} {
        if {[file type $to] == "link"} {
            set linkFile [file join [file dirname $to] [file tail [file readlink $to]]]
            set directory [file dirname $linkFile]
            if {$directory == "."} {
                set directory [file dirname $to]
            }
            set name [file tail $linkFile] 
            set linkname $to
            if {[string last . $name] > [string last - $name]} {
                set separator "."
            } else {
                set separator "-"
            }
            set mtimeto [file mtime $linkFile]
            set previousVersion $name
        } else {
            set directory [file dirname $to]
            set name [file tail $to].00001 
            set separator "."
            set mtimeto [file mtime $to]
        }
    } else {
        set directory [file dirname $to]
        set name [file tail $to].00001 
        set separator "."
        set mtimeto -1
    }
    if [catch {APSNextGenerationedName \
                 -name $name -separator $separator \
                 -newFile 1 -directory $directory} genName] {
        puts "$genName"
        puts " *** Error: File not commited."
        return
    } else {
        set to_link $directory/$genName
    }
    if {$mtimeto < $mtimefrom} {
        # Delete any old one first
        if {[file exists $to]} {
            set err [catch "eval exec rm -f $to" errormsg]
            if {$err} {
                puts " *** Error: $errormsg"
                return
            }
        }
        # Copy new one
        if {$tcl_platform(os) == "Linux"} {
            set err [catch "eval exec cp --preserve=timestamp $from $to_link" errormsg]
        } else {
            set err [catch "eval exec cp -p $from $to_link" errormsg]
        }
        if {$err} {
            puts " *** Error: $errormsg"
            return
        } else {
            puts "  Copied $from -> $to_link"
            # Change permission
            set err1 [catch "eval exec chmod g+w $to_link" errormsg1]
            if {$err1} {
                puts "  (Could not change permissions to group write)"
                return
            }
            # Change group
            set err1 [catch "eval exec chgrp oagmgr $to_link" errormsg1]
            if {$err1} {
                puts "  (Could not change group to oagmgr)"
                return
            }
        }
        cd $directory
        if [catch {exec ln -s [file tail $to_link] $to} errormsg] {
            puts " *** Error: $errormsg"
            return
        }
        cd $pwd
    } elseif {$mtimeto == $mtimefrom} {
        puts "  Same age: $to"
        return
    } else {
        puts "  File: $to is newer than: $from"
        puts "  File information:" 
        puts "  [eval exec ls -la $from]"
        puts "  [eval exec ls -la $to]"
        puts "  Copy anyway \[y/N\]? "
        gets stdin response
        if {![string compare $response "y"] || ![string compare $response "Y"]} {
            # Delete any old one first
            if {[file exists $to]} {
                set err [catch "eval exec rm -f $to" errormsg]
                if {$err} {
                    puts " *** Error: $errormsg"
                    return
                }
            }
            if {$tcl_platform(os) == "Linux"} {
                set err [catch "eval exec cp --preserve=timestamp $from $to_link" errormsg]
            } else {
                set err [catch "eval exec cp -p $from $to_link" errormsg]
            }
            if {$err} {
                puts " *** Error: $errormsg"
                return
            } else {
                puts "  Copied $from -> $to_link"
                # Change permission
                set err1 [catch "eval exec chmod g+w $to_link" errormsg1]
                if {$err1} {
                    puts "  (Could not change permissions to group write)"
                    return
                }
                # Change group
                set err1 [catch "eval exec chgrp oagmgr $to_link" errormsg1]
                if {$err1} {
                    puts "  (Could not change group to oagmgr)"
                    return
                }
            }
            cd $directory
            if [catch {exec ln -s [file tail $to_link] $to} errormsg] {
                puts " *** Error: $errormsg"
                return
            }
            cd $pwd
        } else {
            return
        }
    }
    if {$log} {
        logPatch -applicationName [file tail $to] \
          -previousVersion $previousVersion \
          -newVersion $to_link \
          -type $type
    }
    if {$email} {
        prepareEmail -applicationName [file tail $to] \
          -previousVersion $previousVersion \
          -newVersion $to_link
    }
}

set email(addresses) "soliday borland gfystro lemery shang flood sajaev lberkland asdops"
set email(message) ""
proc prepareEmail {args} {
    set applicationName unknown
    set previousVersion unknown
    set newVersion unknown
    APSStrictParseArguments {applicationName previousVersion newVersion}
    global env comment commentArray email

    if {[string length $comment] == 0} {
        set c [string trim $commentArray($applicationName)]
    } else {
        set c [string trim $comment]
    }
    if {[string length $c] == 0} {
        set c "no comment"
    }
    append email(message) "${applicationName}:\n Previous Version: ${previousVersion}\n New Version: ${newVersion}\n Updated By: ${env(USER)}\n Comment: ${c}\n\n"
}

proc sendEmail {args} {
    global email noemail
    if {$noemail == 0} {
        if {[string length $email(message)] > 0} {
            foreach address $email(addresses) {
                APSSendEMail -address $address -message "$email(message)" -subject "cppatch info"
            }
        }
    }
}

proc logPatch {args} {
    set applicationName unknown
    set previousVersion unknown
    set newVersion unknown
    set type program
    APSStrictParseArguments {applicationName previousVersion newVersion type}
    global env comment commentArray

    if {[string length $comment] == 0} {
        set c [string trim $commentArray($applicationName)]
    } else {
        set c [string trim $comment]
    }
    if {[string length $c] == 0} {
        set c "no comment"
    }
    if {[catch {exec logMessage \
                  -sourceId=appVersionUpdate \
                  -tag=applicationName $applicationName \
                  -tag=user $env(USER) \
                  -tag=applicationArch $env(EPICS_HOST_ARCH) \
                  -tag=previousVersion $previousVersion \
                  -tag=newVersion $newVersion \
                  -tag=type $type \
                  -tag=comment $c} res]} {
        puts stderr " *** Error: Can't log activity: $res"
    } 
}

# **********************************************************************
# **************************** main ************************************
# **********************************************************************
set heliosOrOxygenLinuxHost 0
#Check if on csr3
set hostname [exec hostname]
set domainname [exec domainname]
set force 0
if {$hostname == "max1"} {
    set libDir /data/lib
    set binDir /data/bin
    set commentNeeded 0
    set checkInstallLocations 0
    set heliosAndPhoebus 0
    set noemail 1
    set log 0
    set mail 0
} elseif {($hostname == "head.cluster") || ($hostname == "head2.cluster") || ($hostname == "head3.cluster")} {
    set libDir /lustre/oagsoftware/lib
    set binDir /lustre/oagsoftware/bin
    set commentNeeded 0
    set checkInstallLocations 1
    set heliosAndPhoebus 0
    set noemail 1
    set log 0
    set mail 0
} elseif {$hostname == "smaug.aps.anl.gov"} {
    set libDir /usr/local/oag/apps/lib/darwin-ppc
    set binDir /usr/local/oag/apps/bin/darwin-ppc
    set commentNeeded 0
    set checkInstallLocations 0
    set heliosAndPhoebus 0
    set noemail 1
    set log 0
    set mail 0
} elseif {$hostname == "ctlmacdev.aps.anl.gov"} {
    set heliosPrefix /usr/local
    set phoebusPrefix /net/phoebus/oagmgr
    set libDir /oag/apps/lib/$env(EPICS_HOST_ARCH)
    set binDir /oag/apps/bin/$env(EPICS_HOST_ARCH)
    set commentNeeded 0
    set checkInstallLocations 0
    set heliosAndPhoebus 1
    set noemail 1
    set log 0
    set mail 0
} elseif {($domainname == "xdiv.netw0rk") || ($domainname == "xor.aps.anl.gov")} {
    set libDir /usr/local/oag/apps/lib/$env(EPICS_HOST_ARCH)
    set binDir /usr/local/oag/apps/bin/$env(EPICS_HOST_ARCH)
    set commentNeeded 0
    set checkInstallLocations 0
    set heliosAndPhoebus 0
    set noemail 1
    set log 0
    set mail 0
} elseif {(($domainname == "aps4.anl.gov") || ($domainname == "aps.anl.gov") || ($domainname == "accel.ntw0rk") || ($domainname == "cntrl.ntw0rk") || ($domainname == "(none)")) && ($tcl_platform(os) == "Linux") && ([file exists /usr/local]) && ([file exists /net/phoebus/oagmgr])} {
    
    set heliosPrefix /usr/local
    set phoebusPrefix /net/phoebus/oagmgr
    set libDir /oag/apps/lib/$env(EPICS_HOST_ARCH)
    set binDir /oag/apps/bin/$env(EPICS_HOST_ARCH)
    set commentNeeded 1
    set checkInstallLocations 1
    set heliosAndPhoebus 1
    set noemail 0
    set log 1
    set mail 1
    set heliosOrOxygenLinuxHost 1
} else {
    set heliosPrefix /net/helios/usr/local-sun4u
    set phoebusPrefix /net/phoebus/usr/local-sun4u

    set libDir /oag/apps/lib/$env(EPICS_HOST_ARCH)
    set binDir /oag/apps/bin/$env(EPICS_HOST_ARCH)
    
    set libpatchDir /oag/lib_patch/$env(EPICS_HOST_ARCH)
    set binpatchDir /oag/bin_patch/$env(EPICS_HOST_ARCH)
    
    set pempatch /home/oxygen/OAG/oagWWW/pem
    set commentNeeded 1
    set checkInstallLocations 1
    set heliosAndPhoebus 1
    set noemail 0
    set log 1
    set mail 1
}

# Usage
set usage \
  "Usage: cppatch \[-beta\] \[-noemail\] \[-force\] \[-bin\] -comment <string> <files>

This routine will copy the specified files to the appropriate patch
directories on all known systems.  Files with extension .tcl with be copied to
/usr/local${libDir} and auto_mkindex will be run on those
directories.  Other files will be copied to
/usr/local${binDir}.

This routine should be run from the directory in the the OAG build
tree that has the Makefile for the specified files.  If the file
exists in that directory, it will be copied from that directory.  If
the file does not exist in that directory, then presumably it is a
binary executable, and it will be copied from the O.$env(EPICS_HOST_ARCH)
subdirectory.
"

# Print usage
if {!$argc} {
    puts $usage
    exit 1
}

set args $argv
set comment ""
APSParseArguments {comment}
set argv $args

set beta 0
set bIndex [lsearch $argv "-beta"]
if {$bIndex != -1} {
    set argv [lreplace $argv $bIndex $bIndex]
    set beta 1
}
set forceBin 0
set bIndex [lsearch $argv "-bin"]
if {$bIndex != -1} {
    set argv [lreplace $argv $bIndex $bIndex]
    set forceBin 1
}
set emailIndex [lsearch $argv "-noemail"]
if {$emailIndex != -1} {
    set argv [lreplace $argv $emailIndex $emailIndex]
    set noemail 1
    if {$heliosOrOxygenLinuxHost} {
        set commentNeeded 0
        set log 0
        set mail 0
    }
}
set forceIndex [lsearch $argv "-force"]
if {$forceIndex != -1} {
    set argv [lreplace $argv $forceIndex $forceIndex]
    set force 1
}
set lDir $libDir
set bDir $binDir

proc GetComment {args} {
    global usage
    set file ""
    APSStrictParseArguments {file}
    if {[catch {exec svn log $file} results]} {
        puts $usage
        exit 1
    }
    set lines [split $results \n]

    set section 0
    set comment ""
    foreach line $lines {
        if {$section == 0} {
            if {[string first "-------------" $line ] == 0} {
                set section 1
            }
        } elseif {$section == 1} {
            #set sec [clock scan [lrange $line 4 6]]
            set sec [clock scan [lrange [lindex [split $line \| ] 2] 0 2]]

            if {[expr {[clock seconds] - 600}] > $sec} {
                puts "Please supply a comment for $file."
                puts "SVN comment is more then 10 minutes old."
                exit
            }
            set section 2
        } elseif {$section == 2} {
            if {[string first "-------------" $line ] == 0} {
                break
            }
            append comment "$line "
        }
    }
    return $comment
}
if {($commentNeeded) && ([string length $comment] == 0)} {
    foreach file "$argv" {
        set commentArray([file tail $file]) [GetComment -file $file]
        puts "[file tail $file] comment: $commentArray([file tail $file])"
    }
}

if {$checkInstallLocations} {
    if {[file exists cppatchInstallLocations.sdds]} {
        set locationsFile cppatchInstallLocations.sdds
    } else {
        set locationsFile /home/helios/oagData/textData/cppatchInstallLocations.sdds
    }
    if {[catch {exec sdds2stream $locationsFile -col=TextFile,InstallLocation} asciiConfig]} {
        puts "error: $asciiConfig"
        exit
    }
}

if {$force == 0} {
    foreach file "$argv" {
        if {[file exists $file]} {
            if {[catch {exec grep /home/oxygen $file} result]} {
                #Pass
            } else {
                puts "error: /home/oxygen found in $file"
                puts "       Use the -force option to install this file if this is not going to be a problem"
                exit
            }
            if {[catch {exec grep /home/phoebus $file} result]} {
                #Pass
            } else {
                puts "error: /home/phoebus found in $file"
                puts "       Use the -force option to install this file if this is not going to be a problem"
                exit
            }
            if {[catch {exec strings $file | grep ^GLIBC_2.29} result]} {
                #Pass
            } else {
                puts "error: $file will not run on RHEL7 and RHEL8 machines"
                puts "       Use the -force option to install this file if this is not going to be a problem"
                exit
            }
            if {[catch {exec strings $file | grep ^GLIBC_2.23} result]} {
                #Pass
            } else {
                puts "error: $file will not run on RHEL7 machines"
                puts "       Use the -force option to install this file if this is not going to be a problem"
                exit
            }
        }
    }
}

set mkindex 0
# Loop over files
foreach file "$argv" {
    set found 0
    puts "Processing: $file"
    if {[file exists $file]} {
        set found 1
        if {![string compare [file extension $file] ".tcl"]} {
            # Tcl Library
            set mkindex 1
            copyall -type lib -file $file -log $log -email $mail
        } elseif {![string compare [file extension $file] ".py"]} {
            # Python library
            copyall -type py -file $file -log $log -email $mail
        } elseif {[APSCheckSDDSFile -fileName $file]} {
            if {$checkInstallLocations} {
                if {$file == "cppatchInstallLocations.sdds"} {
                    if {[catch {exec sdds2stream cppatchInstallLocations.sdds -col=TextFile,InstallLocation} results]} {
                        puts "error: $results"
                        exit
                    }
                    foreach "textfile location" $results {
                        if {[string index $textfile 0] != "/"} {
                            global env
                            set textfile [file join $env(HOME) $textfile]
                        }
                        if {![file exists $textfile]} {
                            puts "warning: TextFile $textfile in cppatchInstallLocations.sdds does not exist"
                        }
                        if {![file isdirectory [file dirname $location]]} {
                            puts "warning: InstallLocation directory [file dirname $location] in cppatchInstallLocations.sdds does not exist"
                        }
                    }
                }
            }
            copyall -type sdds -file $file -log $log -email $mail
        } else {
            # Assumed to be a script
            set ascii 0
            if {$checkInstallLocations} {
                foreach "textfile location" $asciiConfig {
                    set len [string length $textfile]
                    set f [file join [pwd] $file]
                    set f2 [file split $f]
                    if {[string compare -length 6 [lindex $f2 2] "helios"] == 0} {
                        set f [eval file join [lreplace $f2 2 2 "helios"]]
                    } elseif {[string compare -length 6 [lindex $f2 2] "oxygen"] == 0} {
                        set f [eval file join [lreplace $f2 2 2 "oxygen"]]
                    } elseif {[string compare -length 7 [lindex $f2 2] "phoebus"] == 0} {
                        set f [eval file join [lreplace $f2 2 2 "phoebus"]]
                    }
                    if {[string range $f [expr [string length $f] - $len] end] == $textfile} {
                        #                        if {$tcl_platform(os) == "Linux"} {
                        #                            puts "error: ascii files listed in cppatchInstallLocations.sdds can only be installed from Solaris."
                        #                            exit
                        #                        }
                        copyall -type ascii -file $file -location $location -log $log -email $mail
                        set ascii 1
                    }
                }
            }
            if {$ascii == 0} {
                copyall -type reg -file $file -log $log -email $mail
            }
        }
    }
    if {!$found} {
        puts "  $file does not exist"
    }
}

# Do auto_mkindex
if {$mkindex} {
    puts "Updating tclIndex:"
    if {$heliosAndPhoebus} {
        if {$mkindexpatch} {
            if {([info exists pempatch]) && ([file exists $pempatch])} {
                set libraries "${heliosPrefix}${lDir} ${phoebusPrefix}${lDir} ${heliosPrefix}${libpatchDir} ${phoebusPrefix}${libpatchDir} $pempatch"
            } else {
                set libraries "${heliosPrefix}${lDir} ${phoebusPrefix}${lDir} ${heliosPrefix}${libpatchDir} ${phoebusPrefix}${libpatchDir}"
            }
        } else {
            if {([info exists pempatch]) && ([file exists $pempatch])} {
                set libraries "${heliosPrefix}${lDir} ${phoebusPrefix}${lDir} $pempatch"
            } else {
                set libraries "${heliosPrefix}${lDir} ${phoebusPrefix}${lDir}"
            }
        }
    } else {
        set libraries "${lDir}"
    }
    foreach library $libraries {
        # Delete tclIndex
        if [catch {file copy -force $library/tclIndex $library/tclIndex.previous
            exec chgrp oagmgr $library/tclIndex.previous 
            exec chmod g+w $library/tclIndex.previous} result] {
            puts " *** Warning ($library): $result"
            continue
        }
        if [catch {file delete -force $library/tclIndex.new} err] {
            puts " *** Error ($library): $err"
        }
        # Re-create tclIndex
        if [catch {aps_auto_mkindex $library *.tcl} err] {
            puts " *** Error ($library): $err"
            if [catch {file copy $library/tclIndex.previous $library/tclIndex} result] {
                puts " *** Couldn't restore previous tclIndex!"
                exit 1
            } else {
                puts " *** Previous tclIndex restored."
            }
        } else {
            puts "  Auto path updated for $library"
        }
        if {[catch {file rename -force $library/tclIndex.new $library/tclIndex} result]} {
            if [catch {file copy $library/tclIndex.previous $library/tclIndex} result] {
                puts " *** Couldn't restore previous tclIndex!"
                exit 1
            } else {
                puts " *** Previous tclIndex restored."
            }            
        }
        # Change permissions and primary group membership
        if [catch {exec chmod g+w $library/tclIndex} errormsg1] {
            puts " *** Error ($library): chmod g+w: $errormsg1"
        }
        if [catch {exec chgrp oagmgr $library/tclIndex} errormsg2] {
            puts " *** Error ($library): chgrp oagmgr: $errormsg2"
        }
    }
}

sendEmail

exit 0

