#!/bin/sh
# \
exec oagwish "$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)]
APSDebugPath
set CVSRevisionAuthor "\$Author: emery $"

APSApplication . -name SRMakeStandardizeFile \
  -overview "SRMakeStandardizeFile converts a SCR file to a standardization file,
and allows installation of lattice defaults."


proc MakeInputFrame {widget args} {
    global initialSCRSystem system oagDataDir
    global outputFileDir outputFileRoot outputFile 
    global beamline searchString daysPast

    set parent ""

    set arrayName ""
    set daysPast 7
# SCRFiles is an array, but the variable SCRFile will be
# a regular variable for the procedure KnLValues
    set arrayName SCRFiles
    set searchString *
    APSStrictParseArguments {parent}

    APSFrame $widget -parent $parent -label "Input parameters" -packOption "-fill both"
    set w $parent$widget.frame

    global $arrayName
    set w $parent$widget.frame

    APSAddSCRDialog .scr -parent $w -system SR -arrayName SRSCRFilename
    
    APSFrame .targetArea -parent $w -label "Target file" \
      -contextHelp "Output standardization file."
    APSLabeledEntry .outputFileDir -parent $w.targetArea.frame -label "directory: " \
      -textVariable outputFileDir -width 70 \
      -contextHelp "Directory for lattice file."
    APSButton .daily -parent $w.targetArea.frame.outputFileDir -packOption "-anchor e" \
      -text "daily" -size small \
      -command {set outputFileDir [APSGoToDailyDirectory]}  \
      -contextHelp "Sets the directory to the user's daily directory."
    APSLabeledEntry .outputFileRoot -parent $w.targetArea.frame -label "root: " \
      -textVariable outputFileRoot -width 70 \
      -contextHelp "Root name for lattice file."
    APSButton .oagArea -parent $w.targetArea.frame -text "Go to oag area" \
      -command "set outputFileDir $oagDataDir" \
      -contextHelp "Set directory to the oag area for SR standardization files."
    return
}

proc InstallDefault {args} {
    global oagDataDir
    APSParseArguments {root}
    cd $oagDataDir
    if {[catch {exec rm default.std} result]} {
        APSSetVarAndUpdate status "Couldn't remove default.std link in $oagDataDir\n$result"
        return -code error
    }
    if {[catch {exec ln -s $root.std default.std} result]} {
        APSSetVarAndUpdate status "Couldn't make link to $root.std in directory $oagDataDir\n$result"
        return -code error
    }
    APSSetVarAndUpdate status "$root.std installed as default standardization file.\nReady." 
    return
}

proc MakeStandardizeFile {args} {
    set SCRFile ""
    set root ""
    set directory ""
    set makeOnly 0
    APSParseArguments {SCRFile root directory makeOnly}
    if ![string length $SCRFile] {
        return -code error "MakeStandardizeFile: No SCRFile given."
    }
    if ![string length $directory] {
        return -code error "MakeStandardizeFile: No directory given."
    }
    if ![string length $root] {
        set root [file root [file tail $SCRFile]]
    }
    APSSetVarAndUpdate status "Creating standardization file $directory/$root.std..."
    exec snap2standardize -snapshot \
      $SCRFile \
      -output $directory/$root.std  \
      -template /home/helios/oagData/sr/magnetConditioning/standardize.config
    if !$makeOnly {
        APSSetVarAndUpdate status "Configuring magnets $directory/$root.std..."
        exec standardize -pend 60 -configure $directory/$root.std
    }
    APSSetVarAndUpdate status "Done."
}

proc SetStatus {text} {
    global status
    set status $text
}

set status "Working."
APSScrolledStatus .status -parent .userFrame -textVariable status -width 90 -packOption "-fill both -expand true"

set oagDataDir /home/helios/oagData/sr/magnetConditioning/lattices/default
set SCRFiles(Filename) ""
set initialSCRSystem SR
set outputFileDir .
set outputFileRoot ""
set beamline SR
set statusCallback ""
set abortVariable ""
MakeInputFrame .input -parent .userFrame
set status "Ready."
update

APSButton .mstandardize -parent .userFrame -text "Make Standardization File" \
  -command \
  { \
      catch {MakeStandardizeFile -SCRFile $SCRFile -makeOnly 1 -directory $outputFileDir \
           } status
      update idletasks 
      APSEnableButton .userFrame.install.button } \
  -contextHelp "Creates a standardization file from the snapshot selected. All magnets are included."

APSButton .mstandardizeAndApply -parent .userFrame -text "Make Standardization File and Apply" \
  -command \
  { \
      catch {MakeStandardizeFile -SCRFile $SCRFile -directory $outputFileDir \
           } status
      update idletasks } \
  -contextHelp "Creates a standardization file from the snapshot selected, then the standardization parameters are sent to the control system. All magnets are included."

APSButton .install -parent .userFrame -text "Install as default" \
  -command \
  { \
      catch {InstallDefault -root $outputFileRoot \
           } status
      update idletasks 
      APSEnableButton .userFrame.install.button } \
  -contextHelp "Install standardization file as default file for default lattice."

APSDisableButton .userFrame.mstandardize.button 
APSDisableButton .userFrame.mstandardizeAndApply.button 
APSDisableButton .userFrame.install.button 

# L. Emery modified the tcl procedure APSAddSCRDialogCallback
# from library mplib/leutl/switch.tcl to
# add actions to the callback. This procedure has to be located
# after the call to MakeInputFrame because the mplib/leutl/switch.tcl 
# file is first sourced during within MakeInputFrame.
proc APSAddSCRDialogCallback {arrayName machine} {
    APSSetVarAndUpdate status "APSAddSCRDialogCallback: arrayName=$arrayName, machine=$machine"
    global $arrayName apsSCRSnapDir status  outputFileRoot
    global outputFileRoot
    if [set ${arrayName}(FileNotFound)] {
        APSSetVarAndUpdate status "File not found!"
        return
    }
    set name [set ${arrayName}(Filename)]
    set descrip [set ${arrayName}(Description)]
    set snapDir $apsSCRSnapDir/$machine
    if ![file exists $snapDir/$name] {
        if [file exists $snapDir/${name}.gz] {
            set name ${name}.gz
        } else {
            APSSetVarAndUpdate status "File not found!"
            return
        }
    }
    set ${arrayName}(ShortFilename) $name
    set ${arrayName}(Filename) $snapDir/$name

    set machineToArea(SR) SR
    set machineToArea(Booster) BTS
    set area $machineToArea($machine)
    set outputFileRoot [file root $name]

    global SCRFile 
    set SCRFile $snapDir/$name
    APSSetVarAndUpdate status "File $snapDir/$name selected for $machine"
    APSEnableButton .userFrame.mstandardize.button 
    APSEnableButton .userFrame.mstandardizeAndApply.button 
}

