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

# $Log: not supported by cvs2svn $
# Revision 1.9  2007/08/01 21:33:40  shang
# added catch statement to APSSetFACL so that it could continue since script exits if user is the not owner of P0/P1 steering directories.
#
# Revision 1.8  2007/02/08 16:34:16  emery
# Now uses APSSetFACL procedure instead of the script
# ~oag/bin/setFACL-oagPlus
#
# Revision 1.7  2007/01/28 21:10:22  emery
# USe set FACL script that is globally available instead of the one in ~oag/bin
#
# Revision 1.6  2005/09/26 21:48:28  emery
# Added user borland to file permissions.
#
# Revision 1.5  2005/07/07 21:30:16  emery
# Added users to setfcal command.
#
# Revision 1.4  2004/03/23 00:36:24  emery
# Use the /home/helios/OAG/bin/setFACL-oagPlus command instead
# of the setfacl command.
#
# Revision 1.3  2002/10/10 23:15:32  emery
# Added setfacl command to allow asdops account to swap P0 P1 links.
#
# Revision 1.2  2001/06/27 21:33:04  emery
# Added missing usage variable.
#
# Revision 1.1  2000/10/27 02:09:22  emery
# First installation.
#

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

# 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.

set args $argv
set lattice ""
set usage "makeIDSteeringLinks -lattice <lattice>"
APSStrictParseArguments {lattice}

if ![string length $lattice] {
    puts stderr $usage
    exit
}

set baseDir /home/helios/oagData/sr/localSteering/lattices/$lattice
cd $baseDir

set userList "asdops borland sr oag emery sajaev schroedr"
# make list of sectors which has both P0's working
if [catch {APSSRGetWorkingP0SectorsList} P0List] {
    puts stderr "Error (1) for $argv0 in [pwd]: $P0List"
    exit 1
}

if ![file exists IDs] {
    exec mkdir IDs
}

for {set i 1} {$i < 41} {incr i} {
    set i1 [expr $i + 1]
    if [expr $i1 == 41] {
        set i1 1
    }

    set dirName $baseDir/IDs/[format %02ld $i]ID
    # remove the link
    if [catch {file delete -force -- $dirName} result] {
        puts stderr $result
        exit
    }
    # it would be nice if a file command existed to do the soft link
    # below instead of having to do exec's
    if {[lsearch -exact $P0List $i] == -1} {
        exec ln -s ../P1s/[format %02ld $i]P1/ $dirName
    } else {
        exec ln -s ../P0s/[format %02ld $i]P0/ $dirName
    }
    #catch {APSSetFACL -userList $userList -filenameList $dirName}
    
}
exit
