	program tecs_client

	real*4 temp(4)
	character device*32, init*80, line*80, cmd*16, par*80, response*80
	integer i,j,k,iret,l
	character file*128, cmdpar*128
	logical oneCommand

! functions
	integer tecs_get_par, tecs_quit_server, tecs_send, tecs_set_par

	call sys_getenv('TECS_INIT', init)

	call sys_get_cmdpar(line, l)
	if (l .ne. 0) then
	  if (line .eq. 'off' .or. line .eq. 'OFF') init=' '
	  oneCommand=.true.
	else
	  oneCommand=.false.
	endif

	if (init .eq. ' ') then
	  call tecs_open(0, ' ', iret)
	else
	  call tecs_open(1, init, iret)
	endif

	if (iret .lt. 0) goto 91
	if (oneCommand) goto 11

	print *
	print *,'Tecs Client'
	print *,'-----------'
	print *
	print *,'<empty line>         show temperature and device'
	print *,'set <temp>           set temperature'
	print *,'send <command>       direct command to LSC340'
	print *,'device <device>      set cryo device'
	print *,'<parameter>          show parameter'
	print *,'<parameter> <value>  set parameter'
	print *,'plot                 temperature and power chart'
	print *,'log <n>              show last n lines of logfile'
	print *,'kill                 close TecsServer and exit'
	print *,'exit,quit            exit, but do not close TecsServer'
	print *,'help                 show list of parameters and cryo devices'
	print *

	l=0
1	if (oneCommand) goto 99
	call sys_rd_line(line, l, 'tecs> ')
	if (l .lt. 0) goto 99
11	l=l+1	
	line(l:l)=' '
	cmd=' '
	k=0
	do j=1,l
	  if (line(j:j) .gt. ' ') then
	    k=k+1
	    cmd(k:k)=line(j:j)
	    if (cmd(k:k) .ge. 'A' .and. cmd(k:k) .le. 'Z') then ! set to lowercase
	      cmd(k:k)=char(ichar(cmd(k:k))+32)
	    endif
	  elseif (k .gt. 0) then ! end of command
	    goto 2
	  endif
	enddo

	if (k .eq. 0) then ! empty line
	  call tecs_get_t(6, temp, iret)
	  if (iret .ne. 0) goto 1
	  iret=tecs_get_par('device', device)
	  if (iret .lt. 0) goto 19
	  print '(x,3(a,f8.3),2a)','tempX=', temp(3),', tempP=',temp(2)
	1        ,', set=',temp(1), ', device=',device
	  goto 1
	endif

	print *,'command too long'
	goto 1
	
2	par=' '
	do i=j,l
	  if (line(i:i) .gt. ' ') then
	    par=line(i:l)
	    goto 3
	  endif
	enddo

	! simple query

	if (cmd .eq. 'kill' .or. cmd .eq. 'off') then
	  iret=tecs_quit_server()
	elseif (cmd .eq. 'exit' .or. cmd .eq. 'quit') then
	  goto 99
	elseif (cmd .eq. 'on') then
	  l=0
	  goto 11
	elseif (cmd .eq. 'plot') then
	  call cho_choose('G')
	  iret=tecs_get_par('dlogfile', file)
	  if (iret .lt. 0) goto 19
	  call tecs_plot(file)
	elseif (cmd .eq. 'help') then
	  print *
	  print *,'Writeable parameters:'
	  print *
	  print *,'set           temperature set-point'
	  print *,'device        temperature device'
	  print *,'controlMode   control on: 0: heat exchanger, '
	1        ,'1: sample, 2: second loop'
	  print *,'maxPower      heater max. power'
	  print *,'prop          PID gain'
	  print *,'int           PID integration time: 1000/int sec'
	  print *,'deriv         PID derivation term'
	  print *,'maxShift      maximum (set-tempH) for controlMode=2'
	  print *,'int2          integration time (sec) for controlMode=2'
	  print *
	  print *,'Read only parameters:'
	  print *
	  print *,'tX            heat exchanger temperature'
	  print *,'tS            sample temperature'
	  print *,'tempH         set-point on regulation'
	  print *,'tLimit        temperature limit'
	  print *,'htr           heater current percentage'
	  print *,'resist        heater resistance'
	  print *,'logfile       name of the logfile'
	  print *,'remoteMode    1: local, 2: remote '
	1        ,'(switch on with device command)'
	  print *
	  print *,'t1            regulation temperature (hi-T sensor)'
	  print *,'t2            regulation temperature (low-T sensor)'
	  print *,'t3            sample temperature (hi-T sensor)'
	  print *,'t4            sample temperature (low-T sensor)'
	  print *
	  print *,'Temperature devices:'
	  print *
	  print *,'ill1, ill2, ill3 (cryofurnace), ill4 (focus-cryo), '
	1        ,'ill5 (maxi)' 
	  print *,'cti1, cti2, cti3, cti4, cti5 (maxi), cti6 (focus), apd'
	  print *,'ccr4k (4K closed cycle), hef4c (TriCS 4circle cryo)'
	  print *,'sup4t (supra.magnet 4T)'
	  print *,'rdrn (LTF dilution, 20kOhm), rdrn2 (2kOhm)'
	  print *
	elseif (cmd .eq. 'log') then
	  iret=tecs_get_par('logfile', file)
	  if (iret .lt. 0) goto 19
	  call show_log(50, file)
	else
	  iret=tecs_get_par(cmd, response)
	  if (iret .lt. 0) goto 19
	  print '(7x,3a)',cmd(1:k),'=',response
	endif
	goto 1

3	if (cmd .eq. 'send') then
	  iret=tecs_send(par, response)
	  if (iret .lt. 0) goto 19
	  print '(7x,2a)','response: ',response
	elseif (cmd .eq. 'log') then
	  i=50
	  read(par, *, err=31) i
31	  iret=tecs_get_par('logfile', file)
	  if (iret .lt. 0) goto 19
	  call show_log(i, file)
	else
	  iret=tecs_set_par(cmd, par)
	  if (iret .lt. 0) goto 19
	  print '(7x,3a)',cmd(1:k),':=',par
	endif
	goto 1

19	if (iret .eq. -2) then
	  call tecs_write_msg(6)
	else
	  call tecs_write_error(6)
	endif
	goto 1

91	if (iret .lt. 0) then
	  call tecs_write_error(6)
	endif
99	end


	subroutine show_log(lines, file)

	integer lines

	integer i,l
	character str*132, file*(*)

	print *
	print *
	open(1, name=file, status='old', readonly, shared, err=39)
	i=0
31	read(1,'(a)',end=32)
	i=i+1
	goto 31
32	rewind(1)
	do i=1,i-lines
	  read(1,*,end=39)
	enddo
33	read(1,'(q,a)',end=39) l,str
	print *,str(1:min(len(str),max(1,l)))
	goto 33
39	continue
	close(1)
	end
