# iex - An import (and eventually export) utility for ascii files and
# the 'bbuuzzb' database server/engine.
# Rick Smereka, Copyright (C) 2006.

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.

# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with this program; if not, get a copy via the Internet at
# http://gnu.org/copyleft/gpl.html or write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston,
# MA 02111-1307 USA

# You can contact the author via email at rsmereka@future-lab.com

# Program syntax:

#    iex ascii_input table_output profile_name

# Where 'ascii_input' is the path and file name of the ASCII
# input file, 'table_ouput' is the name of the output
# 'bbuuzzb' table and 'profile_name' is the name of the
# import profile loaded into field one of the profile
# table 'iex.config'. Refer to the module 'iexlib.tcl' for a
# complete description of the 'iex.config' field layout.

# Initial tcl version. Jun/2006, Rick Smereka

# required path to library modules
set TCLDIR /home/rick/gpl/tcl

source $TCLDIR/lib/stdhead.tcl
source $TCLDIR/lib/comcode.tcl
source $TCLDIR/lib/appinit.tcl
const MULTIUSER 0
source $TCLDIR/dbeng/dbmess.tcl
source $TCLDIR/dbeng/dbiocode.tcl
source $TCLDIR/dbeng/dbeng.tcl
source $TCLDIR/dbeng/dbengcfg.tcl
source $TCLDIR/dbeng/dbengcat.tcl
source $TCLDIR/dbeng/dbfxp.tcl
source $TCLDIR/dbeng/dbengsrt.tcl
source $TCLDIR/dbeng/dbcomm.tcl
source $TCLDIR/dbeng/iexlib.tcl

const APNAME "iex"
const VERSION "1.05.01-2006.06.18"

# global data
set input_name ""
set output_name ""
set profile_name ""

proc c_initialize {} {
   # Start the 'dbeng' local engine, read the config file and
   # display the result settings. Returns 1 upon success, 0
   # otherwise.
   global DBENG_OK DBENG_CONFIG_UNABLE_TO_OPEN
   set mname "c_initialize"

   set ret [db_initialize]

   # interpret code from init which may be a
   # fatal config error
   switch $ret \
      $DBENG_OK {puts "$mname:read config file successfully"} \
      $DBENG_CONFIG_UNABLE_TO_OPEN \
                {puts "$mname:config file not found,loaded defaults"} \
      default {puts "$mname:fatal config error([db_io_code_string $ret])"; \
               puts "$mname:terminating";return 0}

   db_config_get_tmp_path mes
   puts "$mname:tmp path is '$mes'"
   db_config_get_log mes
   puts "$mname:log is '$mes'"
   set ret [db_config_get_catalog mes]

   if {$ret == $DBENG_OK} {
      puts "$mname:catalog is '$mes'"
   } else {
      puts "$mname:catalog name not loaded"
   }

   db_config_get_catalog_flag flag

   if {[flag_2_logic_string $flag mes]} {
      puts "$mname:catalog is $mes"
   }

   return 1
}

proc term_app {} {
   # Prepare to terminate the application. Shutdown all API's
   db_end

   if {[logman_is_active]} {
      logman_end
   }

   appinit_remove_name
}

proc usage {} {
   # Display program usage.
   global APNAME

   puts "usage: $APNAME input_name output_name profile_name"
}

# Program main line.
# unbuffered stdout
fconfigure stdout -buffering none

# check number of parameters passed
if {$argc == 0} {
   usage
   exit
}

if {$argc < 3} {
   puts "$APNAME:insufficient number of parameters"
   usage
   exit
}

# get command parms
set input_name [lindex $argv 0]
set output_name [lindex $argv 1]
set profile_name [lindex $argv 2]

# display logo
puts "$APNAME for $tcl_platform(platform) Version $VERSION"
puts "By Rick Smereka, Copyright (c) 2006"

# register application name with 'appinit'
if {![appinit_register_name $APNAME]} {
   puts "$APNAME:fatal error registering app name with appinit"
   exit
}

# initialize local engine if we are running locally or
# startup the 'socloc' interface and connect to a
# Bbuuzzb database server if we are running in
# client/server mode
if {![c_initialize]} {
   term_app
   exit
}

puts "$APNAME:run start at [get_ftime]"
# set ret [db_config_set_log_flag 1]
# puts "db_config_set_log_flag=[db_io_code_string $ret]"
# logman_console 1
set ret [iexlib_import $profile_name $input_name $output_name]

if {$ret != $DBENG_OK} {
   puts "$APNAME:bad rc([db_io_code_string $ret]) from iexlib_import,\
         import failed"
}

term_app
puts "$APNAME:program complete at [get_ftime]"
exit
