#! /home/xed/xsrexx
#!wsrexx

/*
 * SetServ:  SEDIT and S/REXX license server install script. Do not modify it.
 *
 *   Copyright (C) 1999 by Benaroya et Compagnie
 *   All rights reserved.
 */
option setenv mixed
signal on novalue
numeric digits 15

cd xhome()
$PATH = xhome()';'$PATH

call serv
exit

rc = display('n',,
    3, "Install License Server", 'Goto to "Register"', "Exit SetServ",,
    ,,
    '       ********* SEDIT-S/REXX License Server Setup *********      ',,
    ,,
    'SetServ installs the license server for SEDIT and/or S/REXX.',,
    ,,
    'You need to install the license server only if you have a network',,
    'password. In this case, your password sheet must display the 2',,
    'additional fields:',,
    ,,
    '      licenses     : xxxx',,
    '      serial number: xxxx',
    ,,
    ,,
    'To install a standalone password, use the "Register" facility',,
    ,,
    ' "SetServ" is an S/REXX program.',
    )

select
 when rc = 1 then call serv
 when rc = 2 then
  { call '.\Register'
    exit
  }
 when rc = 3 then exit
 otherwise exit
end

/* NOTREACHED */

serv:
  do forever
    rc = display('n',,
        4, "Enter SEDIT key", "Enter S/REXX key", "Start the server", "Exit",,
        ,,
        '       ********* SEDIT-S/REXX License Server Setup *********      ',,
        ,,
        )

    select
     when rc = 1 then call inst 'SEDIT'
     when rc = 2 then call inst 'S/REXX'
     when rc = 3 then call start
     when rc = 4 then exit
    end
  end
exit


/* NOTREACHED */
inst:parse arg name

   select
     when name ='SEDIT' then
      { file = 'seditusers'
        prod = 'sedit'
      }
     when name ='S/REXX' then
      { file = 'srexxusers'
        prod = 'srexx'
      }
   end

   hn = hostname()
   id = cpuid()
   pa = ''
   se = ''
   po = 1112
   ex = ''
   op = ''
   li = ''
   ho = xhome()
   comment.0 = 0         /* Number of unrecognized lines */

   if state(file) then
    { /*
       *  hostname C
       *  hostid   803b1479
       *  password 12693-23656-09813-34511
       *  serial   1001
       *  port     1112
       *  expire   Never
       *  options  APL S/REXX DEBUG
       *  home     /home/xed
       */
      trace off
      'execio * diskr 'file' . .(finis stem tab.'
      trace on
      if rc ~= 0 then
       { call display('n', 1, 'Exit',,
               ,,
               'SetServ: Unable to read file 'file,)

         exit 1
       }
      do i = 1 to tab.0
          ss = strip(tab.i)
          if ss[1] = '*' | length(ss) = 0 then
           { comment.0 = comment.0 + 1
             a = comment.0
             comment.a = tab.i
             iterate i
           }

          item = word(tab.i, 1)
          lower item
          select
            when item = 'hostname' then hn = word(tab.i, 2)

            when item = 'hostid'  |,
                 item = 'etherid' |,
                 item = 'sysid'   |,
                 item = 'sysinfo' |,
                 item = 'seditid' |,
                 item = 'uname'   |,
                 item = 'cpu_id'      then id = word(tab.i, 2)

            when item = 'password' then pa = word(tab.i, 2)
            when item = 'serial'   then se = word(tab.i, 2)
            when item = 'port'     then po = word(tab.i, 2)
            when item = 'expire'   then ex = word(tab.i, 2)
            when item = 'version' | item = 'options' then op = subword(tab.i, 2)
            when item = 'licenses' | item = 'users' then li = word(tab.i, 2)
            when item = 'home'     then ho = word(tab.i, 2)
            otherwise
             comment.0 = comment.0 + 1
             a = comment.0
             comment.a = tab.i
         end
      end i
    }

   /*
    *
    * ---------------------------------------------------------------
    * |                                                             |
    * |  ******* SEDIT License Server Settings  *******             |
    * |                                                             |
    * |   xxxxxx:           _________________                       |
    * |                                                             |
    * |                                                             |
    * |  |Cancel|                                     |Register|    |
    * ---------------------------------------------------------------
    *
    */
   arch = arch()
   select
     when arch = 'alpha'   then name_id = 'Etherid'
     when arch = 'cray'    then name_id = 'uname'
     when arch = 'dec'     then name_id = 'uname'
     when arch = 'hp'      then name_id = 'uname'
     when arch = 'ibm'     then name_id = 'Uname'
     when arch = 'linux'   then name_id = 'Seditid'
     when arch = 'sco'     then name_id = 'Seditid'
     when arch = 'sgi'     then name_id = 'Sysid'
     when arch = 'sinix'   then name_id = 'Hostid'
     when arch = 'sparc'   then name_id = 'Hostid'
     when arch = 'sun4'    then name_id = 'Hostid'
     when arch = 'i86pc'   then name_id = 'Hostid'
     when arch = 'uxw'     then name_id = 'Seditid'
     when arch = 'wnt'     then name_id = 'CPU ID'
     otherwise                  name_id = 'CPU ID'
   end

   la = 10
   ii = la+3
   call dy_start()
   call dy_label 10, 2, '******* 'name' License Server Settings  *******'
   y = 5
   dy = 1.5
   call dy_label 2, y, left('Hostname:', la)
   $hn = dy_input(ii, y, 50, hn  )
   y = y+dy

   call dy_label 2, y, left(name_id':', la)
   $id = dy_input(ii, y, 23, id)
   y = y+dy

   call dy_label 2, y, left('Password:', la)
   $pa = dy_input(ii, y, 23, pa)
   y = y+dy

   call dy_label 2, y, left('Serial:', la)
   $se = dy_input(ii, y, 23, se)
   y = y+dy

   call dy_label 2, y, left('Port:', la)
   $po = dy_input(ii, y, 23, po)
   y = y+dy

   call dy_label 2, y, left('Expire:', la)
   $ex = dy_input(ii, y, 23, ex)
   y = y+dy

   call dy_label 2, y, left('Options:', la)
   $op = dy_input(ii, y, 23, op)
   y = y+dy

   call dy_label 2, y, left('Licenses:', la)
   $li = dy_input(ii, y, 23, li)
   y = y+dy

   call dy_label 2, y, left('Home:', la)
   $ho = dy_input(ii, y, 50, ho)
   y = y+3

   $cancel   = dy_button(2, y,  "Cancel")
   $register = dy_button(48, y,  "Register "name)

   $handle_register = dy_end("proc_reg", 0, 0, 65, y+2, "n")
   call dy_map $handle_register
return

proc_reg:
  parse arg handle reason

  select
    when reason = 'B'$cancel | reason = '^c' then
      {  call dy_unmap(handle)
         return 0
      }
    when reason = 'B'$register then
      { id = dy_vinput(handle, $id)
        npa = dy_vinput(handle, $pa)
        npa = change(npa, ' ', '')
        npa = change(npa, '_', '-')
        npa = change(npa, '--', '-')
        call ckpa prod, id, npa, stem, se
        if result ~= 0 then
         { call prompt result, handle, $pa, pa
           return 0
         }
        pa = npa
        hn = strip(dy_vinput(handle, $hn))
        id = strip(dy_vinput(handle, $id))
        se = strip(dy_vinput(handle, $se))
        po = strip(dy_vinput(handle, $po))
        ex = strip(dy_vinput(handle, $ex))
        op = strip(dy_vinput(handle, $op))
        li = strip(dy_vinput(handle, $li))
        ho = strip(dy_vinput(handle, $ho))
        if state(file) then
         { do nv = 1 to 1000
              if ~state(file'.'nv) then leave nv
           end nv
           call mv file, file'.'nv
         }
        call lineout file, 'hostname 'hn
        nid = lower(change(name_id, ' ', '_'))  /* cpu id --> cpu_id */
        call lineout file, left(nid, 9)id
        call lineout file, 'password 'pa
        call lineout file, 'licenses 'li
        call lineout file, 'serial   'se
        call lineout file, 'port     'po
        call lineout file, 'expire   'ex
        call lineout file, 'options  'op
        call lineout file, 'home     'ho
        do k = 1 to comment.0
           call lineout file, comment.k
        end k
        call lineout file
        call prompt3 'File 'file' successfully updated.'
        call dy_unmap(handle)
        return 0
      }
    otherwise return 0
  end
return

/*
 * start: install the 'xserv' service, and start it
 */

start:

  if ~state('seditusers') & ~state('srexxusers') then
   { call prompt3 'You must enter licensing information first.'
     return
   }

return


prompt:procedure

   parse arg mes, handle, item, cpuid

   call dy_sinput handle, item, cpuid

   call dy_start()
   la = max(40, length(mes)+10)
   pos = (la-length(mes))/2
   call dy_label pos, 2, mes
   pos = la/2 - 4
   call dy_button(pos, 5,  "Retry")
   $prompt = dy_end("proc_prompt", 0, 0, la, 8, "g")
   call dy_map $prompt
   call dy_destroy $prompt


return

prompt2:procedure

   parse arg mes

   call dy_start()
   la = max(40, length(mes)+10)
   pos = (la-length(mes))/2
   call dy_label pos, 2, mes
   pos = la/2 - 4
   call dy_button(pos, 5,  "Abort")
   $prompt = dy_end("proc_prompt", 0, 0, la, 7, "g")
   call dy_map $prompt
   exit 1
return

prompt3:procedure

   parse arg mes

   call dy_start()
   la = max(40, length(mes)+10)
   pos = (la-length(mes))/2
   call dy_label pos, 2, mes
   pos = la/2 - 4
   call dy_button(pos, 5,  "Continue")
   $prompt = dy_end("proc_prompt", 0, 0, la+1, 7, "g")
   call dy_map $prompt
   call dy_destroy $prompt
return

proc_prompt:
return 0
