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

# $Log: not supported by cvs2svn $
# Revision 1.11  2004/03/23 00:37:34  emery
# Updated the list of allowed users.
#
# Revision 1.10  2001/06/08 19:06:51  emery
# Improved context help descriptions and change the name of the
# reset button.
#
# Revision 1.9  2000/11/09 16:20:05  emery
# Added makeLSFiles in the "SetDefaultLinks" button.
#
# Revision 1.8  2000/11/09 16:17:55  emery
# Replaced command makeLSFiles with path
# /home/helios/oagData/sr/localSteering  with
# makeLSFiles with no path (i.e. get makeLSFiles
# from usual shell PATH variable)
#
# Revision 1.7  2000/10/23 18:49:00  emery
# Restored the ../ relative path to the P0s and P1s
# link. It was a mistake to remove them earlier.
#
# Revision 1.6  2000/10/23 18:26:46  emery
# Removed dependence on cd commands inside procedures.
# Put cd /home/helios/oagData/sr/localSteering/lattices/default
# from the start.
#
# Revision 1.5  2000/10/23 17:34:06  emery
# Remove the call to APSSRGetInstalledBPMList which depend on
# the old "installation phases" of the bpms.
# Use configuration data from /home/helios/oagData/sr/BPMStatus instead.
#

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

set CVSRevisionAuthor "\$Revision: 1.12 $ \$Author: soliday $"

APSApplication . -name SRIDdirectoryLinks -version $CVSRevisionAuthor \
  -overview "SRIDdirectoryLinks allows one to switch between P0 and P1 pairs of bpms for ID steering. This is done by changing the directory links of the ID local steering matrices in the oag data area.\n\nBe warned that not all ID straight section have actual P0 bpms working even though a correction matrix may have been prepared. An error will occur at the time of correction if a switch is made to non-existing bpms.\n\nA button is provided to set the links according to the data in the bpm status management configuration file.\n\nThe SRIDSteering application will have to be re-started after making a steering switch."

set status ""
APSScrolledStatus .status -parent .userFrame -width 50 \
  -textVariable status -withButtons 1

proc SetStatus {text} {
    global status
    set status "[exec date +%H:%M:%S] $text"
    update
}

proc CheckAccess {} {
    set access 0
    set userName [exec whoami]
    set srGroup "oag borland lemery cyao sajaev decker asdops sr shang soliday "
    foreach user $srGroup {
        if {[string compare $user $userName] == 0} {
            set access 1
        }
    }
    if !$access {
        return -code error "You do not have permission to run this  \
          application. Please, send e-mail to \"borland\" or \"emery\" to \
          be put on the permission list."
    }
}

proc MakeIDsFrame {} {

    set yList ""
    for {set i 1} {$i <= 20} {incr i} {
        lappend yList y$i
    }

    if [catch {ReadCurrentLinks} result] {
        SetStatus "ReadCurrentLinks: $result"
        return
    }
    
    APSFrame .base -parent .userFrame -label "Active BPMs for the list of IDs" \
      -orientation vertical
    APSFrameGrid .baseGrid -parent .userFrame.base.frame -xList {x1 x2 x3} -height 2 \
      -yList $yList -width 70 -bd 0 -contextHelp "Set of IDs and related active BPMs. \
        Click on a radiobutton to change BPM."

    set w .userFrame.base.frame.baseGrid
    for {set i 1} {$i <= 40} {incr i} {
        global id$i
        if $i<=20 {
            set x x1; set y y$i
        } else {
            set x x3; set y y[expr $i - 20]
        }
        APSRadioButtonFrame .id$i -parent $w.$x.$y -label "ID[format %02ld $i]" \
          -variable id$i -buttonList {P0 P1} -valueList [list P0 P1] \
          -orientation horizontal -commandList [list "ChangeLink $i P0" "ChangeLink $i P1"]
    }
    APSButton .deflink -parent .userFrame -text "Read BPM status and redo links" -command SetDefaultLinks \
      -contextHelp "This button redoes the links according the bpms that are good in the BPM status management file." \
      -packOption "-anchor c"
}

proc ReadCurrentLinks {} {
    for {set i 1} {$i <= 40} {incr i} {
        global id$i
        set id IDs/[format %02ld $i]ID
        set idLink [file readlink $id] 
        if [regexp P0 $idLink] {
            set id$i P0
        } else {
            set id$i P1
        } 
    } 
}

proc ChangeLink {id bpm} {
    set oldDir [pwd]
    set dirName IDs/[format %02ld $id]ID
    if [catch {file delete $dirName} result] {
        SetStatus "ChangeLink (1): $result"
        return
    }

    switch $bpm {
        P0 {if [catch {exec ln -s ../P0s/[format %02ld $id]P0 $dirName \
                     } result] {
            SetStatus "ChangeLink (2): $result"
            cd $oldDir
            return
        }
            SetStatus "Directory link for [format %02ld $id]ID was changed to: P0s/[format %02ld $id]P0/"
            bell
        }
        P1 {if [catch {exec ln -s ../P1s/[format %02ld $id]P1/ $dirName \
                     } result] {
            SetStatus "ChangeLink (3): $result"
            cd $oldDir
            return
        }
            SetStatus "Directory link for [format %02ld $id]ID was changed to: P1s/[format %02ld $id]P1/"
            bell
        }
        default {SetStatus "Wrong BPM name $bpm."; bell; return}
    }
    SetStatus "Re-generating light source configuration files..."
    if [catch {exec makeLSFiles -useDirectoryLinks 1 \
             } result] {
        SetStatus $result
        return
    }
    SetStatus "Done."
}


proc SetDefaultLinks {} {
    # make soft links of directories
    # /home/helios/oagData/sr/localSteering/lattices/default/IDs/<nn>IDs
    # to directories
    # /home/helios/oagData/sr/localSteering/lattices/default/P0s and
    # /home/helios/oagData/sr/localSteering/lattices/default/P1s
    # according to the installation phase of the bpms.

    SetStatus "Settting default links..."

    # list of bpms used for steering
    if [catch {APSSRGetWorkingP0SectorsList} P0List] {
        SetStatus "SetDefaultLinks (1): Problem getting working P0s: $P0List"
        return
    }
    
    for {set i 1} {$i < 41} {incr i} {
    	  set i1 [expr $i + 1]
    	  if [expr $i1 == 41] {
            set i1 1
    	  }
        # remove link
    	  set dirName IDs/[format %02ld $i]ID
    	  file delete $dirName

    	  if {[lsearch -exact $P0List $i] > -1} {
            if [catch {exec ln -s ../P0s/[format %02ld $i]P0/ $dirName} result] {
                SetStatus "SetDefaultLinks (2): $result"
                return
            }
    	  } else {
            if [catch {exec ln -s ../P1s/[format %02ld $i]P1/ $dirName} result] {
                SetStatus "SetDefaultLinks (3): $result"
                cd $oldDir
                return
            }
    	  }
    }
    ReadCurrentLinks
    SetStatus "Re-generating light source configuration files..."
    if [catch {exec makeLSFiles} result] {
        SetStatus $result
        return
    }
    SetStatus "Done."
    bell
}

if [catch {CheckAccess} result] {
    SetStatus "$result"
    bell
    return
}

set status Ready.

set baseDir /home/helios/oagData/sr/localSteering/lattices/default
cd $baseDir
MakeIDsFrame
