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

# $Log: not supported by cvs2svn $
# Revision 1.7  2011/03/10 20:35:32  shang
# added tcledit editor
#
# Revision 1.6  2009/03/11 15:12:17  shang
# modified to use APSGotoDailyDirectory creating the notebook file.
#
# Revision 1.5  2002/11/18 15:04:49  soliday
# Fixed the backwards month and day.
#
# Revision 1.4  2002/10/07 16:59:32  soliday
# Added the ability to pre-select the system
#
# Revision 1.3  2002/09/27 14:25:00  soliday
# Added the ability to edit an existing file without adding a new header.
#
# Revision 1.2  2002/09/26 13:50:05  soliday
# Fixed a bug
#
# Revision 1.1  2002/09/25 22:13:26  soliday
# First version. This will be used to make notebook entries more standardized.
#
#

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

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

proc CreateNewKeyword {args} {
    global data
    if {[winfo exists .dialog2]} {
        destroy .dialog2
    }
    set data(newKeyword) ""
    APSDialogBox .dialog2 \
        -name "New Keyword" \
        -okCommand "AddKeywords"
    APSLabeledEntry .keyword -parent .dialog2.userFrame \
        -label "New Keyword:" \
        -textVariable data(newKeyword)
}

proc ListKeywords {args} {
    global data
    if {[winfo exists .dialog]} {
        destroy .dialog
    }
    global test
    APSScrolledListWindow .dialog  \
        -label "Select the notebook keywords" \
        -clearButton 1 \
        -listvar data(keywordList) \
        -selectionVar data(keywords) \
        -name Keywords
    APSDialogBoxAddButton .add -parent .dialog \
        -text "Create New Keyword" \
        -command CreateNewKeyword
}

proc AddKeywords {args} {
    global data
    lappend data(keywordList) $data(newKeyword)
    lappend data(newKeywords) $data(newKeyword)
}

proc GetFileName {args} {
    global data dateDay dateMonth dateYear dateHour
    if {[llength $data(system)] != 1} {
        set data(file) ""
        return
    }
    set dir [APSGoToDailyDirectory -account $data(system)]
    set data(file) $dir/Notebook
    
}

proc EditFile {args} {
    global data
    if {[llength $data(system)] != 1} {
        APSSetVarAndUpdate status "Please select the system"
        bell
        return
    }
    if {[llength $data(editor)] != 1} {
        APSSetVarAndUpdate status "Please select your editor"
        bell
        return
    }
    if {!$data(NoUpdateKeywords) && [llength $data(newKeywords)]} {
        if {[catch {sdds load /home/helios/oagData/WWWNotebooks/keywords.sdds sddsdata} results]} {
        } else {
            set data(keywordList) "[lindex $sddsdata(Column.Keywords) 0] $data(newKeywords)"
            set data(keywordList) [lsort -unique $data(keywordList)]
            set sddsdata(Column.Keywords) [list $data(keywordList)]
            catch {sdds save /home/helios/oagData/WWWNotebooks/keywords.sdds sddsdata}
        }
        set data(newKeywords) ""
    }
    if {[file exists [file dirname $data(file)]] == 0} {
        if {[catch {file mkdir [file dirname $data(file)]} results]} {
            APSSetVarAndUpdate status "error: $results"
            bell
            return
        }
    }
    set mode 2
    if {[file exists $data(file)]} {
        set mode [tk_dialog .dialog "Warning Message" \
                      "The file already exists" \
                      warning 3 Append Edit Overwrite Cancel]
        if {$mode == 3} {
            return
        }
    }
    if {$mode == 0} {
        if {[catch {open $data(file) "a"} fID]} {
            APSSetVarAndUpdate status "error: $fID"
            bell
            return
        }
    } elseif {$mode == 2} {
        if {[catch {open $data(file) "w"} fID]} {
            APSSetVarAndUpdate status "error: $fID"
            bell
            return
        }
    }
    if {$mode != 1} {
        set header "[clock format [clock seconds] -format {%m/%d/%y %H:%M:%S}]\n"
        append header "             Topic: $data(topic)\n"
        append header "Personnel Involved: $data(personnel)\n"
        append header "          Keywords: $data(keywords)\n"
        append header "      Studies Plan: $data(plan)\n"
        append header "   Studies Summary: $data(summary)\n"
        append header "Studies Chronology: $data(chronology)\n"
        puts $fID $header
        catch {close $fID}
    }
    if {($data(editor) == "pico") ||
        ($data(editor) == "nano") ||
        ($data(editor) == "vi") ||
        ($data(editor) == "vim")} {
        global tcl_platform
        if {$tcl_platform(os) == "Linux"} {
            exec xterm -bg black -fg white -geometry 92x30 -fn 9x15bold -fb 9x15 -j -sb -sl 4096 -e $data(editor) $data(file) &
        } else {
            exec /usr/dt/bin/dtterm -bg black -fg white -geometry 92x30 -fn 9x15bold -fb 9x15 -j -sb -sl 4096 -xrm Dtterm*menuBar:False -e $data(editor) $data(file) &
        }
    } else {
        exec $data(editor) $data(file) &
    }
}


APSApplication . -name UniformNotebook -version $CVSRevisionAuthor

set status Ready...
APSScrolledStatus .status -parent .userFrame \
    -textVariable status
pack configure .userFrame.status -expand true -fill x

APSRadioButtonFrame .system -parent .userFrame \
    -label "System:" \
    -variable data(system) \
    -valueList "BOOSTER FEEDBACK IDGROUP1 LASER LEUTL LINAC PAR SR" \
    -buttonList "{BOOSTER } {FEEDBACK } {IDGROUP1 } {LASER } {LEUTL } {LINAC } {PAR } SR" \
    -orientation "horizontal" \
    -commandList "GetFileName GetFileName GetFileName GetFileName GetFileName GetFileName GetFileName GetFileName"

if {$tcl_platform(os) == "Linux"} {
    APSRadioButtonFrame .editor -parent .userFrame \
      -label "Editor:" \
      -variable data(editor) \
      -valueList "dtpad emacs nedit nano textedit vi vim xedit xemacs" \
      -buttonList "dtpad emacs nedit nano textedit vi vim xedit xemacs" \
      -orientation "horizontal"
} else {
    APSRadioButtonFrame .editor -parent .userFrame \
      -label "Editor:" \
      -variable data(editor) \
      -valueList "dtpad emacs nedit pico textedit vi vim xedit xemacs" \
      -buttonList "dtpad emacs nedit pico textedit vi vim xedit xemacs" \
      -orientation "horizontal"
}

APSLabeledEntry .topic -parent .userFrame \
    -label "             Topic:" \
    -textVariable data(topic) \
    -width 60
pack configure .userFrame.topic.entry -expand true

APSLabeledEntry .personnel -parent .userFrame \
    -label "Personnel Involved:" \
    -textVariable data(personnel) \
    -width 60
pack configure .userFrame.personnel.entry -expand true

APSLabeledEntry .keywords -parent .userFrame \
    -label "          Keywords:" \
    -textVariable data(keywords) \
    -width 60
pack configure .userFrame.keywords.entry -expand true -side left
pack [button .userFrame.keywords.k -text K -command ListKeywords] -side right -padx 0 -pady 0

APSLabeledEntry .plan -parent .userFrame \
    -label "      Studies Plan:" \
    -textVariable data(plan) \
    -width 60
pack configure .userFrame.plan.entry -expand true

APSLabeledEntry .summary -parent .userFrame \
    -label "   Studies Summary:" \
    -textVariable data(summary) \
    -width 60
pack configure .userFrame.summary.entry -expand true

APSLabeledEntry .chronology -parent .userFrame \
    -label "Studies Chronology:" \
    -textVariable data(chronology) \
    -width 60
pack configure .userFrame.chronology.entry -expand true

APSDateTimeEntry .date -parent .userFrame \
    -dayVariable dateDay \
    -monthVariable dateMonth \
    -yearVariable dateYear \
    -hourVariable dateHour \
    -twoDigitYear 0

APSLabeledEntry .file -parent .userFrame \
    -label "              File:" \
    -textVariable data(file) \
    -width 60
pack configure .userFrame.file.entry -expand true

APSButton .edit -parent .userFrame \
    -text "Edit" \
    -command "EditFile"

set args $argv
set system ""
APSStrictParseArguments {system}
set data(system) $system

GetFileName

set data(keywordList) ""
set data(NoUpdateKeywords) 0
if {[catch {sdds load /home/helios/oagData/WWWNotebooks/keywords.sdds sddsdata} results]} {
    set data(NoUpdateKeywords) 1
    set status "error: $results"
} else { 
    set data(keywordList) [lindex $sddsdata(Column.Keywords) 0]
    set data(newKeywords) ""
}
