#!/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
    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

    exec make
    
    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 $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 }
        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 $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 $fileList
    }
}

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

if {($tcl_platform(os) != "Darwin")} {
    puts "You must run this from a oxygen or helios darwin 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
}
CheckoutAndBuildSDDS
CppatchNewSDDSFiles
CheckoutAndBuildOAG
CppatchNewOAGFiles
CleanUpFiles

