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

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)]

proc CheckoutAndBuildSDDS {args} {
    global env tcl_platform
    set env(CVSROOT) /net/phoebus/epicsmgr/cvsroot
    exec rm -rf /tmp/oagsoftware
    exec mkdir /tmp/oagsoftware
    cd /tmp/oagsoftware
    
    if {[catch {exec cvs -Q checkout -P epics/extensions/configure} results]} {
        puts stderr "error: $results"
        exit
    }
    exec mkdir -p /tmp/oagsoftware/epics/extensions/src
    cd /tmp/oagsoftware/epics/extensions/src
    if {[catch {exec svn -q checkout https://svn.aps.anl.gov/AOP/SDDS/trunk SDDS} results]} {
        puts stderr "error: $results"
        exit
    }
    if {[catch {exec svn -q checkout https://svn.aps.anl.gov/AOP/oagca/trunk oagca} results]} {
        puts stderr "error: $results"
        exit
    }
    if {[catch {exec svn -q checkout https://svn.aps.anl.gov/AOP/SDDSepics/trunk SDDSepics} results]} {
        puts stderr "error: $results"
        exit
    }
    
    cd /tmp/oagsoftware/epics/extensions/configure
    
    exec echo EPICS_BASE=/usr/local/iocapps/R3.14.12.2/base/3-14-12-2-asd1 >> RELEASE
    exec echo CROSS_COMPILER_TARGET_ARCHS = >> CONFIG
    exec echo SHARED_LIBRARIES=NO >> CONFIG

    if 0 {
    if {$env(EPICS_HOST_ARCH) != "linux-x86_64"} {
      exec echo STATIC_BUILD=YES >> CONFIG
    }
    exec echo STATIC_LDLIBS_YES= -lc >> CONFIG
    exec echo STATIC_LDFLAGS_YES= -Wl,-Bstatic -static-libgcc >> CONFIG
    exec echo LDLIBS_READLINE = -lreadline -lcurses >> CONFIG
    if {$env(EPICS_HOST_ARCH) == "linux-x86_64"} {
        #THIS IS ONLY NEEDED ON FEDORA 8 AS FAR AS I CAN TELL
        #exec echo POSIX_LDLIBS = -Wl,-Bdynamic -lpthread -Wl,-Bstatic >> CONFIG
    }
    }

    exec make
    
    cd /tmp/oagsoftware/epics/extensions/src/SDDS/SDDSaps/sddsplots

    if 0 {
    if {$env(EPICS_HOST_ARCH) == "linux-x86"} {
            exec echo STATIC_BUILD=NO > Makefile.OAG.tmp
            exec cat Makefile.OAG >> Makefile.OAG.tmp
            exec cp -f Makefile.OAG.tmp Makefile.OAG
    }
    }

    cd /tmp/oagsoftware/epics/extensions/src/SDDS
    
    catch {exec make -ks}
    
    cd /tmp/oagsoftware/epics/extensions/src/oagca
    
    catch {exec make -ks}
    
    cd /tmp/oagsoftware/epics/extensions/src/SDDSepics
    
    catch {exec make -ks}
}

proc CppatchNewSDDSFiles {args} {
    global env
    cd /tmp/oagsoftware/epics/extensions/bin/$env(EPICS_HOST_ARCH)
    
    set files [glob *]
    
    set fileList ""

    foreach f $files {
        set update 0
        set f1 /home/helios2/oag/apps/bin/solaris/$f
        if {[file exists $f1]} {
            set t1 [file mtime $f1]
            set f2 /home/helios2/oag/apps/bin/$env(EPICS_HOST_ARCH)/$f
            set f3 /net/phoebus/oagmgr/oag/apps/bin/$env(EPICS_HOST_ARCH)/$f
            if {[file exists $f2]} {
                set t2 [file mtime $f2]
                if {$t1 > $t2} {
                    set update 1
                }
            } else {
                set update 1
            }
            if {[file exists $f3]} {
                set t3 [file mtime $f3]
                if {$t1 > $t3} {
                    set update 1
                }
            } else {
                set update 1
            }
        }
        if {$update} {
            append fileList "$f "
        }
    }
    if {[llength $fileList]} {
        eval exec cppatch -noemail $fileList
    }
}

proc CheckoutAndBuildOAG {args} {
    global env
    cd /tmp/oagsoftware
    
    if {[catch {exec svn -q checkout https://svn.aps.anl.gov/AOP/oag/trunk oag --depth immediates} results]} {
        puts stderr "error: $results"
        exit
    }
    if {[catch {exec svn -q checkout https://svn.aps.anl.gov/AOP/oag/trunk/apps oag/apps --depth immediates} results]} {
        puts stderr "error: $results"
        exit
    }
    if {[catch {exec svn -q checkout https://svn.aps.anl.gov/AOP/oag/trunk/apps/configure oag/apps/configure --depth infinity} results]} {
        puts stderr "error: $results"
        exit
    }
    if {[catch {exec svn -q checkout https://svn.aps.anl.gov/AOP/oag/trunk/apps/config oag/apps/config --depth infinity} results]} {
        puts stderr "error: $results"
        exit
    }
    if {[catch {exec svn -q checkout https://svn.aps.anl.gov/AOP/oag/trunk/apps/src oag/apps/src --depth immediates} results]} {
        puts stderr "error: $results"
        exit
    }
    
    cd /tmp/oagsoftware/oag/apps/src

    if {[catch {exec svn -q update --set-depth exclude elegantTestSet} results]} {
        puts stderr "error: $results"
        exit
    }
    if {[catch {eval exec svn -q update --set-depth infinity [glob *]} results]} {
        puts stderr "error: $results"
        exit
    }
    
    cd /tmp/oagsoftware/oag/apps/configure
        
    exec make
    
    cd /tmp/oagsoftware/oag/apps/src
    
    catch {exec make -ks}
    
}

proc CppatchNewOAGFiles {args} {
    global env
    cd /tmp/oagsoftware/oag/apps/lib/$env(EPICS_HOST_ARCH)
    
    set files [glob *]
    
    set fileList ""

    foreach f $files {
        if {$f == "tclIndex"} { continue }
        if {$f == "working.au"} { continue }
        if {$f == "emergency.au"} { continue }
        if {$f == "alert.au"} { continue }
        set update 0
        set f1 /home/helios2/oag/apps/lib/solaris/$f
        if {[file exists $f1]} {
            set t1 [file mtime $f1]
            set f2 /home/helios2/oag/apps/lib/$env(EPICS_HOST_ARCH)/$f
            set f3 /net/phoebus/oagmgr/oag/apps/lib/$env(EPICS_HOST_ARCH)/$f
            if {[file exists $f2]} {
                set t2 [file mtime $f2]
                if {$t1 > $t2} {
                    set update 1
                }
            } else {
                set update 1
            }
            if {[file exists $f3]} {
                set t3 [file mtime $f3]
                if {$t1 > $t3} {
                    set update 1
                }
            } else {
                set update 1
            }
        }
        if {$update} {
            append fileList "$f "
        }
    }
    if {[llength $fileList]} {
        eval exec cppatch -noemail $fileList
    }


    cd /tmp/oagsoftware/oag/apps/bin/$env(EPICS_HOST_ARCH)
    
    set files [glob *]
    
    set fileList ""

    foreach f $files {
        if {[file extension $f] == ".au"} { continue }
        set update 0
        set f1 /home/helios2/oag/apps/bin/solaris/$f
        if {[file exists $f1]} {
            set t1 [file mtime $f1]
            set f2 /home/helios2/oag/apps/bin/$env(EPICS_HOST_ARCH)/$f
            set f3 /net/phoebus/oagmgr/oag/apps/bin/$env(EPICS_HOST_ARCH)/$f
            if {[file exists $f2]} {
                set t2 [file mtime $f2]
                if {$t1 > $t2} {
                    set update 1
                }
            } else {
                set update 1
            }
            if {[file exists $f3]} {
                set t3 [file mtime $f3]
                if {$t1 > $t3} {
                    set update 1
                }
            } else {
                set update 1
            }
        }
        if {$update} {
            append fileList "$f "
        }
    }
    if {[llength $fileList]} {
        eval exec cppatch -noemail $fileList
    }
}

proc UpdateTclExtensions {args} {
    cd /tmp/oagsoftware/oag/apps/src/tcltkinterp/extensions
    
    foreach extension "ca os sdds rpn" so "libtclCa.so libtclOS.so libtclSDDS.so libtclRPN.so" {
        set files "${extension}/pkgIndex.tcl ${extension}/O.linux-x86/$so"
        set files2 "pkgIndex.tcl $so"

        foreach f $files f0 $files2 {
            set f1 /home/helios2/oag/lib/${extension}/$f0
            if {[file exists $f1]} {
                set t1 [file mtime $f1]
                set f2 /home/helios2/oag/Linux/lib/${extension}/$f0
                set f3 /net/phoebus/oagmgr/oag/Linux/lib/${extension}/$f0
                if {[file exists $f2]} {
                    set t2 [file mtime $f2]
                    if {$t1 > $t2} {
                        file copy -force $f $f2
                        exec chmod g+w $f2
                    }
                } else {
                    file copy -force $f $f2
                    exec chmod g+w $f2
                }
                if {[file exists $f3]} {
                    set t3 [file mtime $f3]
                    if {$t1 > $t3} {
                        file copy -force $f $f3
                        exec chmod g+w $f3
                    }
                } else {
                    file copy -force $f $f3
                    exec chmod g+w $f3
                }
            }
        }
    }
}

proc CleanUpFiles {args} {
    cd /tmp
    exec rm -rf /tmp/oagsoftware
}

if {($tcl_platform(os) != "Linux")} {
    puts "You must run this from a oxygen or helios Linux client"
    exit
}
if {![file exists /home/helios2/oag]} {
    puts "This workstation does not have /home/helios2 mounted"
    exit
}
if {![file exists /net/phoebus/oagmgr/oag]} {
    puts "This workstation does not have /net/phoebus/oagmgr mounted"
    exit
}

set env(EPICS_HOST_ARCH) linux-x86_64

CheckoutAndBuildSDDS
CppatchNewSDDSFiles
CheckoutAndBuildOAG
CppatchNewOAGFiles
#UpdateTclExtensions
CleanUpFiles

if 0 {
set env(EPICS_HOST_ARCH) linux-x86

CheckoutAndBuildSDDS
CppatchNewSDDSFiles
CheckoutAndBuildOAG
CppatchNewOAGFiles
#UpdateTclExtensions
CleanUpFiles
}
