#! /home/xed/xsrexx

/*
 * xsdown: terminates the license server
 */

signal on novalue
option setenv mixed
address csh
parse arg  all_a

options = "-stdout 0 -host 1 -s 0 -h 1"
version = version()
if (version ~= 'windows' & ~$?DISPLAY) | version = '' then lg_stdout = 1
else                                                       lg_stdout = 0

if parg(all_a, options, tab) ~= 0 then
 { call prompt 'xsdown: invalid option.'
   exit 1
 }

lg_host   = 1
do i = 1 to tab.0
  select
    when tab.i = 'stdout'| tab.i = 's'  then lg_stdout = 1
    when tab.i = 'host' | tab.i = 'h'   then
     { host = tab.host.1
       lg_host = 0
     }
    otherwise
     { call prompt 'xsdown: unexpected option.'
       exit 2
     }
  end
end i

call read_conf     /* We read the config files */
name = fn(arg(0))

newl = '0A'x
call refresh

if lg_stdout then
 { do i = 1 to tab.0
     say tab.i
   end
   exit 0
 }

ht_scrl = 15
la_scrl = 60
la_box  = la_scrl + 10

select
  when version = 'xview' then
   { py_save  = ht_scrl + 10
     ht_box = py_save + 2
     px_save = la_box-26
     px_refresh =  abs(la_box/2)-8

   }

  when version = 'motif' then
   { py_save  = ht_scrl + 8.5
     ht_box = py_save + 3.5
     px_save = la_box-26
     px_refresh =  abs(la_box/2)-8
   }

  when version = 'windows' then
   { py_save  = ht_scrl + 6
     ht_box = py_save + 3
     px_save = la_box-27.5
     px_refresh =  abs(la_box/2)-8
   }
end

call dy_start()

call dy_label 1, 1, centre("XSDOWN - License Server Termination", la_box)
$bu_refresh = -1
$bu_save    = -1

$bu_done    = dy_button(2, py_save,  "Done")
$scl        = dy_scl(1, 3, la_scrl, ht_scrl, tab)
$handle     = dy_end("cb", 0, 0, la_box, ht_box, "n")
call dy_map $handle
exit 0

req:
   parse arg req

   sock_out = socket("AF_INET", "SOCK_STREAM", 0)
   if sock_out < 0 then
    { call prompt name': unable to create a socket, errno = 'errno
      exit 1
    }

   rc = setsockopt(sock_out, "SOL_SOCKET", "SO_LINGER", '1 2');
   if rc < 0 then
    { call prompt name': unable to setsockopt a socket, errno = 'errno
      exit 1
    }
   rc = gethostbyname(host, 'host_add.!')
   if rc ~= 1 then
    { call prompt name': unable to gethostbyname 'host', h_errno = 'h_errno
      exit 1
    }

   address_family = "AF_INET"
   address_addr   = host_add.!addr
   address_port   = port
   rc = connect(sock_out, 'address_')
   if rc < 0 then
    { call prompt name': unable to connect, no license server available.'
      exit 1
    }
   rc = send(sock_out, left(req, lenmes2xserv()))
   call usleep 100000
   do i = 1 to 30
    rc = ioctl(sock_out, 'FIONREAD', 'nb_in')
    if rc = 0 & nb_in ~= 0 then leave i
    call usleep 100000
   end i

   if rc ~= 0 | nb_in = 0 then
    { call prompt name': the license server does not answer.'
      exit 1
    }

   len = recv(sock_out, 'data', nb_in)
   if str = '' then str = data
   else             str = str || newl || data
   call closesocket sock_out
return



cb:
parse arg handle reason where

select
  when reason = 'B'$bu_done then
   { call dy_unmap handle
     return
   }

  when reason = 'B'$bu_refresh then
   { old = tab.0
     call refresh
     do i = 1 to old
       call dy_rscl handle, $scl, i, ''
     end i
     do i = 1 to tab.0
       call dy_rscl handle, $scl, i, tab.i
     end i
     return
   }

  when reason = 'B'$bu_save then
   { trace off
     'execio 1 diskw xinfo.log . .(string '
     'execio 1 diskw xinfo.log . .(string ********* License Usage on 'date('l') time('n')
     'execio 1 diskw xinfo.log . .(string '
     'execio * diskw xinfo.log . .(finis stem tab.'
     if rc = 0 then
        call prompt 'File "'cwd()'/xinfo.log" saved.'
     else
        call prompt 'Unable to save File "'cwd()'/xinfo.log".'
     return
   }
  otherwise return
end

prompt:procedure expose version lg_stdout

   parse arg mes

   if lg_stdout then
    { say mes
      sayn 'Press Enter to continue '
      pull rep
      return
    }

   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")
   select
     when version = 'xview'   then ht_prompt = 7
     when version = 'motif'   then ht_prompt = 7
     when version = 'windows' then ht_prompt = 7.5
   end
   $prompt = dy_end("proc_prompt", 0, 0, la, ht_prompt, "g")
   call dy_map $prompt
   call dy_destroy $prompt


return

proc_prompt:
return 0

refresh:
  str  = ''
  call req 'die'

  ind = 1
  do forever
    parse var str next (newl) str
    tab.ind = next
    ind = ind+1
    if str = '' then leave
  end
  tab.0 = ind-1
return


read_conf: procedure expose host port lg_host

  ff = 0
  if state(xhome()'/seditusers') then ff = xhome()'/seditusers'
  else if state(xhome()'/srexxusers') then ff = xhome()'/srexxusers'
  if ff ~= 0 then
   { host_read = ''
     port_read = ''
     trace off
     'execio * diskr 'ff' . .(finis stem users.'
     trace on
     if rc = 0 then
      do i = 1 to users.0
        ident = lower(word(users.i, 1))
        select
          when  ident = 'hostname' then
           { hh = word(users.i, 2)
             parse var hh host_read ',' .
           }

          when  ident = 'port' then
           { hh = word(users.i, 2)
             parse var hh port_read ',' .
           }

          otherwise nop
        end
      end i
   }

  if lg_host then
   { if host_read = '' then host = hostname()
     else                   host = host_read
   }
  else host = hostname()

  if port_read = '' then port = 1112
  else                   port = port_read

return
