* DELSRVR
* DELSRVR - DELETE.SERVER command.
* Copyright (c) 2004 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:
* 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:
*
*    DELETE.SERVER name
*
*    Prompts appear for absent items
*
* END-DESCRIPTION
*
* START-CODE

$internal
program delsrvr
$catalogue $DELSRVR

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

   server.name.chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-'

   parser = "!PARSER"
   prompt ''

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

   if not(kernel(K$ADMINISTRATOR,-1)) then
      stop sysmsg(2001) ;* Command requires administrator privileges
   end

   call @parser(parser$reset, 0, @sentence, 0)
   call @parser(parser$get.token, token.type, token, keyword) ;* Verb

   * Get server name

   call @parser(parser$get.token, token.type, server.name, keyword)
   loop
      if token.type = PARSER$END then
         display sysmsg(3300) :  ;* Server name:
         input server.name
         server.name = trim(server.name)
         if server.name = '' then goto exit.delete.server
      end
      server.name = upcase(server.name)
   until convert(server.name.chars, '', server.name) = ''
      display sysmsg(3301) ;* Invalid server name
      if token.type # PARSER$END then stop
   repeat


   * All looks ok - Update the config file

   openseq kernel(K$INIPATH,0) to ini.f else
      @system.return.code = -status()
      stop sysmsg(3308) ;* Unable to open configuration file
   end

   ini.data = ''
   section = ''
   found = @false
   loop
      readseq line from ini.f else exit

      if trim(line) = '' then continue   ;* Skip blanks, reinserting correctly

      if line[1,1] = '[' then
         if ini.data # '' then ini.data<-1> = ''

         section = upcase(field(line[2,999], ']', 1))
      end else if section = 'QMNET' then
         if line[1,len(server.name)+1] = server.name:'=' then
            found = @true
            continue
         end
      end
      ini.data<-1> = line
   repeat

   if not(found) then stop sysmsg(3315) ;* Server name not known

   seek ini.f else stop sysmsg(3309) ;* Error rewinding configuration file

   weofseq ini.f on error stop sysmsg(3310) ;* Error truncating configuration file

   loop
      writeseq remove(ini.data, more) to ini.f else
         stop sysmsg(3311) ;* Error writing configuration file
      end
   while more
   repeat

   display sysmsg(3312) ;* Server configuration updated

exit.delete.server:
   @system.return.code = 0
end

* END-CODE
