C------------------------------------------------------------------------
	SUBROUTINE SELBIT(BITMAP,BITMAP_LN,SNAME,S_LN,*)
C------------------------------------------------------------------------
C Subroutine to select a STARTING SUBFILE choice. If the file is not one
C of the standard choices, the file is read. Optionally, the subroutine
C spawns LOGIC to create a BITMAP file.
C
C Alternate RETURN is used for errors when in batch mode or end-of-file
C  BITMAP(:BITMAP_LN)	is name of user subfile (NSET = -1)
C  SNAME(:S_LN)	is project name
C------------------------------------------------------------------------
C
	CHARACTER*(*) sname,bitmap
	INTEGER s_ln,bitmap_ln

	include '[-.shared]jcpds.parms'
	include 'SEARCH.cmn'
	include 'SEARCH.par'
        include '[-.shared]TBL.cmn'
	CHARACTER*80 name
	INTEGER len_name
	INTEGER I,N

	INTEGER STATUS,CLI$GET_VALUE,CLI$PRESENT
	LOGICAL GET_NUMBER,flag,DEF_LOGICAL
	EXTERNAL CLI$_PRESENT,CLI$_DEFAULTED,CLI$_ABSENT,CLI$_NEGATED
	character*40 node,device,dir,type*20,ver*5
	integer*2 len_node,len_device,len_dir,len_type,len_ver

	IF (CLI$PRESENT('SUBPDF')) THEN
	  len_name = 0
	  STATUS = CLI$GET_VALUE('SUBPDF',name,len_name)
	  DO I = 1,max_sub
	    IF (name(1:len_name) .eq. SUBPDF(i)(1:len_name)) then
	      CALL GETbit(i,name,len_name,*711)
C if the change was successful, change the BITMAP name
	      NSET = i
	      bitmap = name
	      bitmap_ln = lench(bitmap)
	      RETURN
	    ENDIF
	  ENDDO
	  I = max_sub+3
	  IF (name(1:len_name) .eq. SUBPDF(i)(1:len_name)) then
	    CALL GETbit(i,name,len_name,*711)
C if the change was successful, change the BITMAP name
	    NSET = i
	    bitmap = name
	    bitmap_ln = lench(bitmap)
	    RETURN
	  ENDIF
	  type *,'SELBIT ERROR -- Illegal SUBPDF option'
	  RETURN 1
	ELSEIF (CLI$PRESENT('USERPDF')) THEN
	  STATUS = CLI$GET_VALUE('USERPDF',name,len_name)
	  CALL GETbit(-1,name,len_name,*713)
C if the change was successful, change the BITMAP name
	  NSET = -1
	  RETURN
	ELSEIF (CLI$PRESENT('LOGIC')) THEN
	  IF (.not. PROMPT) then 
	    write(lo,'(A)') ' Cannot spawn LOGIC from non-interactive JOB'
	    RETURN 1
	  ENDIF
	  goto 300
C no recognized flags were given, is input allowed?
	ELSEIF (.NOT. PROMPT) THEN
	  type *,'ERROR -- Unable to prompt for required input'
	  RETURN 1
	ENDIF
C------------------------------------------------------------------------
C get input option
400	WRITE(LO,'(A)') ' Options for sub-databases (^Z for main menu)'
	j = max_sub+3
	ln = lench(sublbl(j))
	write(lo,162)  0,sublbl(j)(:ln),' [default]'
	DO J=1,max_sub
	  ln = lench(sublbl(j))
	  IF (SUBREC(j) .gt. 0 .and. j .gt. 9) THEN
	    write(lo,163)  j,sublbl(j)(:ln),' subfile'
	  ELSEIF (SUBREC(j) .gt. 0) THEN
	    write(lo,162)  j,sublbl(j)(:ln),' subfile'
	  ENDIF
162	  format(4x,'(',i1,')  ',2A)
163	  format(3x,'(',i2,')  ',2A)
	ENDDO
	j = max_sub+2
	ln = lench(sublbl(j))
	write(lo,163)  -1,sublbl(j)(:ln),' subfile'
	WRITE(LO,'(a)') '$Enter the subfile number [Default=0]: '
	READ(LI,'(I)',ERR=400,END=3) N
	IF (N .lt. 0) then
	  goto 200
	ELSEIF (N .le. MAX_SUB) THEN
          IF (N .eq. 0) N = max_sub+3
          CALL GETbit(N,name,len_name,*711)
C if the change was successful, change the BITMAP name
	  NSET = N
	  RETURN
	ELSE
	  write (*,'(/a/)') ' Invalid input, try again'
	  GO TO 400 
	ENDIF
C------------------------------------------------------------------------
C request the file name of the USER SUBFILE
200	WRITE(lo,'(a)') ' Enter the index file name to be used'//
	1	' (default extension = .USRPDF)',
	2	' Enter a blank line to use program LOGIC to create a file.',
	3	'$Filename: '
	READ(LI,'(q,a)',END=3) len_name,name
	IF (len_name .EQ. 0) GOTO 300
	CALL GETbit(-1,name,len_name,*713)
C if the change was successful, change the BITMAP name
	NSET = -1
	bitmap = name
	bitmap_ln = len_name
	Return
C------------------------------------------------------------------------
C Spawn LOGIC to create a subfile
300	CONTINUE
C ... 1st create a file name for the bitmap to pass to LOGIC
C   (the bitmap is written on the scratch device, with extension .PDF_INP)
	call fparse(sname(1:s_ln),node,len_node,device,len_device,
	1	dir,len_dir,name,len_name,type,len_type,ver,len_ver)
	NAME = 'PS_TMP:'//NAME(1:LEN_NAME)//'_START'//'.USRPDF'
	len_name = lench(Name)
C define a logical name for LOGIC
	flag = DEF_LOGICAL('LOGIC_OUTPUT',name(1:len_name),
	1	len_name,0,lgprc)
C start LOGIC
	CALL LIB$SPAWN('@PS_EXE:SEARCH_SPAWN_LOGIC')
C we are now back from LOGIC, read in the SUBFILE
	CALL GETbit(-1,name,len_name,*712)
C if the change was successful, change the BITMAP name
	NSET = -1
	bitmap = name
	bitmap_ln = len_name
	RETURN
C************************************************************************
C we hit an error reading the bitmap file
711 	IF (PROMPT) goto 400
	STOP 'SELBIT/GETBIT ERROR -- unable to read SUBFILE record'
C Subfile error
713 	IF (PROMPT) goto 200
	STOP 'SELBIT/GETBIT ERROR -- unable to read USER SUBFILE'
C LOGIC ERROR
712 	write (*,'(A)') ' SELBIT ERROR -- no file from LOGIC'
	goto 400
3	RETURN 1
C************************************************************************
	end
