* CREATEI
* CREATE.INDEX and MAKE.INDEX verbs
* Copyright (c) 2005 Ladybridge Systems, All Rights Reserved
*
* 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, 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, write to the Free Software Foundation,
* Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
* 
* Ladybridge Systems can be contacted via the www.openqm.com web site.
* 
* START-HISTORY:
* 11 Oct 05  2.2-14 Added ak.path argument to $mkindx and corresponding
*                   command line option.
* 20 May 05  2.2-0 All except command parsing moved to mkindx.
* 28 Mar 05  2.1-11 Use PARSER$MFILE.
* 16 Mar 05  2.1-10 0327 Changed how A/S type indices store their AK info data.
* 24 Dec 04  2.1-0 Added support for A, C and S-type records.
* 13 Oct 04  2.0-5 Use message handler.
* 16 Sep 04  2.0-1 OpenQM launch. Earlier history details suppressed.
* END-HISTORY
*
* START-DESCRIPTION:
*
* CREATE.INDEX filename field... [NO.NULLS] [PATHNAME akpath]
*
* MAKE.INDEX filename field... [NO.NULLS] [PATHNAME akpath]
*
* The two commands are identical except that MAKE.INDEX automatically
* goes on to build the index.
*
* END-DESCRIPTION
*
* START-CODE

$internal
program createi
$catalog $CREATEI

$include keys.h
$include int$keys.h
$include parser.h
$include err.h


   parser = "!PARSER"

   @system.return.code = -ER$ARGS     ;* Preset for argument errors

   akpath = ''

   call @parser(PARSER$RESET, 0, @sentence, 0)
   call @parser(PARSER$GET.TOKEN, token.type, token, keyword) ;* Verb

   * Get and process file name

   dict.flag = ''
   call @parser(PARSER$MFILE, token.type, filename, keyword)
   if keyword = KW$DICT then
      dict.flag = 'DICT'
      call @parser(PARSER$MFILE, token.type, filename, keyword)
   end
   if token.type = PARSER$END then
      stop sysmsg(2102)   ;* File name required
   end

   * Check file exists and is a dynamic file

   open dict.flag, filename to data.f else
      open dict.flag, upcase(filename) to data.f else
         stop sysmsg(2019)   ;* File not found
      end
      filename = upcase(filename)
   end

   if fileinfo(data.f, FL$TYPE) # FL$TYPE.DH then stop sysmsg(2010)

   if dict.flag = '' then
      open 'DICT', filename to dict.f else stop sysmsg(2012)
   end else
      open 'DICT.DICT' to dict.f else stop sysmsg(2012)
   end
   
   * Collect field names and associated data

   no.case = @false
   no.nulls = @false
   new.ak.names = ''
   loop
      call @parser(PARSER$GET.TOKEN, token.type, token, keyword)
   until token.type = PARSER$END
      begin case
         case keyword = KW$NO.NULLS
            no.nulls = @true

         case keyword = KW$PATHNAME
            call @parser(PARSER$GET.TOKEN, token.type, akpath, keyword)
            if token.type # PARSER$TOKEN then
               stop sysmsg(2633, token)
            end

         case 1
            if token = '' then stop sysmsg(2607)  ;* Illegal null field name

            read dict.rec from dict.f, token else
               read dict.rec from dict.f, upcase(token) else
                  stop sysmsg(2608, token)
               end
               token = upcase(token)
            end

            locate token in new.ak.names<1> setting pos then
               stop sysmsg(2609, token)
            end

            new.ak.names<-1> = token
      end case
   repeat

   if new.ak.names = '' then stop sysmsg(2614)  ;* No fields specified

   data.path = fileinfo(data.f, FL$PATH)
   dict.path = fileinfo(dict.f, FL$PATH)

   close data.f
   close dict.f

   flags = MKI$CREATE
   if no.case then flags += MKI$NO.CASE
   if no.nulls then flags += MKI$NO.NULLS
   if @option = 1 then flags += MKI$BUILD
   call $mkindx(data.path, dict.path, akpath, new.ak.names, flags, err)

   @system.return.code = err
   
   return

   * Avoid compiler warnings
   dict.rec = dict.rec
   pos = pos
end

* END-CODE
