#!/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 "\$Revision: 1.1 $ \$Author: sereno $"

set status "Please, enter a text filename and the filename delimiter. \nReady..."
set delimiter ","
set inputFile ""
set outputFile ""

# Set up the labelled input and convert widgets.

APSApplication . -name txt2Tabbed -version $CVSRevisionAuthor \
  -overview "This applications converts any delimited text file to a 
   tab delimited text file.  The delimeter and test filename are inputs."

APSScrolledStatus .status -parent .userFrame -textVariable status -width 90 \
  -height 8 -packOption "-side top -fill x -expand false"

APSLabeledEntry .inputFile -parent .userFrame -label "Input file: "\
  -textVariable inputFile -width 40 -contextHelp "Input file entry box."

APSLabeledEntry .delimiter -parent .userFrame -label "Delimiter: "\
  -textVariable delimiter -width 40 -contextHelp "Delimiter entry box."

APSButton .convert -parent .userFrame -text "Convert" \
  -packOption "-side left" -command {ConvertText2Tabbed -inputFile $inputFile -delimiter $delimiter} \
  -contextHelp "Calls procedure ConvertText2Tabbed to convert the input text file
to a tab delmited file."

# Disable button procedure.

proc DisableButtons {} {
    APSDisableButton .userFrame.convert.button
}

# Enable button procedure.

proc EnableButtons {} {
    APSEnableButton .userFrame.convert.button
}

# Procedure to read in one line of the input file, replace the
# specified delimiter with a tab, then write the tab delimited line to
# the output file.

proc ConvertText2Tabbed {args} {
    global status

    DisableButtons
    update

    set inputFile ""
    set delimiter ""
    if [APSStrictParseArguments {inputFile delimiter}] {
        return -code error "usage: ConvertText2Tabbed -inputFile <inputFile> -delimiter <inputFile-delimiter>"
    }
    set lineList ""
    set tabLine ""
    if [file exists $inputFile] {
        set outputFile [join "[lindex [split $inputFile "."] 0] .tb" ""]
        if [catch {open $inputFile r} fidInput] {
            APSSetVarAndUpdate status "ConvertText2Tabbed: $fidInput"
            return
        }
        if [catch {open $outputFile w} fidOutput] {
            APSSetVarAndUpdate status "ConvertTextToCVS: $fidOutput"
            return
        }
        while {[gets $fidInput line] >= 0} {
            set lineList [split $line "$delimiter"]
            # Make sure each row element has no spaces.  Mostly for the first heading
            # row so sddsEditNew is not fooled into thinking there are more numeric
            # columns than actually exist.
            set joinedLineList ""
            foreach element $lineList {
                set element [join $element ""]
                lappend joinedLineList $element
            }
            set tabLine [join $joinedLineList \t]
            puts $fidOutput $tabLine
        }
        close $fidInput
        close $fidOutput
        APSSetVarAndUpdate status "ConvertText2Tabbed:  Conversion complete.  Tab delimited file $outputFile created."
        EnableButtons
        return
    } else {
        APSInfoWindow [APSUniqueName .] -name Message -width 30 -infoMessage \
          "File $inputFile does not exist." -contextHelp ""
        bell
        EnableButtons
        return
    }
}
