      subroutine chemsh_input_file_info(input_filename, 
     $     rtdb_name, ostartup, ocontinue)
C$Id$
      implicit none
#include "errquit.fh"
#include "inp.fh"
#include "global.fh"
#include "mafdecls.fh"
#include "msgids.fh"
#include "cfileprefix.fh"
#include "util.fh"
#include "stdio.fh"
      character*(*) input_filename ! [input]
      character*(*) rtdb_name   ! [output]
      logical ostartup           ! [output]
      logical ocontinue          ! [output]
c
      character*(nw_max_path_len) ecce_file_name
      character*(nw_max_path_len) a_temporary_file
      logical status, odirective, echo
      logical bad_permanent_dir, bad_scratch_dir
      logical already
      integer nkeys, istart, iend
      parameter (nkeys = 7)
      logical iocheckk
      integer mitob1
      character*16 keys(nkeys), field
      data keys/'start','restart','continue',
     &    'scratch_dir','permanent_dir', 'ecce_print',
     $     'echo'/
c
c     Scan the input for start/restart directives and attempt
c     to figure out the name of the desired data base, if the
c     job is a startup or a restart, what the file_prefix is.
c
c     While we're doing this also scan for scratch_dir and permanent_dir
c
c     (start || restart) [<file_prefix> = 'from input file base'] \
c                        [rtdb <rtdb_file_name>]
c
c     scratch_dir <read rest of line as character string>
c     permanent_dir <read rest of line as character string>
c
c     Only process 0 reads ... everyone else jumps to the broadcast
c
      print *, "### calling MA_sizeof..."
      call flush(6)
      mitob1=MA_sizeof(MT_INT,1,MT_BYTE)
      print *, "### MA_sizeof called, mitob1 =", mitob1
      call flush(6)
      scratch_dir = ' '
      permanent_dir = ' '
      call util_set_default_scratch_dir(scratch_dir)
      call util_set_default_permanent_dir(permanent_dir)
      iocheckk=.true.
#if defined(NOFSCHECK) || defined(CRAYXT) || defined(BGP) || defined(BGQ)
c     on catamount all fs are parallel, therefore we need only node0
c     on BGP/BGQ all fs are parallel, therefore we need only node0
      iocheckk=ga_nodeid().eq.0
#endif     
c
      rtdb_name  = ' '
      print *, "### calling ga_nodeid..."
      call flush(6)
      
      if (ga_nodeid() .gt. 0) goto 10000
c
c     default is a startup with name extracted from that of the input
c     file unless a database of that name is present in which case
c     you get a restart.  Overriden by presenting start/restart.
c
      odirective = .false.      ! True if find a start/restart/continue
      ostartup   = .true.
      ocontinue  = .false.
      echo       = .false.
      call input_default_file_prefix(input_filename,file_prefix)
c
      print *, "### rewinding luin..."
      call flush(6)
      rewind LuIn
      print *, "### luin rewound."
      call flush(6)
      call inp_init(LuIn,LuOut)
      print *, "### inp_init called."
      call flush(6)
 10   if (inp_search(.false., keys, nkeys)) then ! While
         if (.not. inp_a(field)) call errquit('input_start: inp?',0,
     &       INPUT_ERR)
         if (inp_compare(.false.,'start',field)) then
            odirective = .true.
            ostartup = .true.
            ocontinue = .false.
            call util_read_start_dir(file_prefix, rtdb_name)
         else if (inp_compare(.false.,'continue',field)) then
            call errquit('continue directive is no longer supported',
     *        555, INPUT_ERR)
            odirective = .true.
            ostartup = .false.
            ocontinue = .true.
            call util_read_start_dir(file_prefix, rtdb_name)
         else if (inp_compare(.false.,'restart',field)) then
            odirective = .true.
            ostartup = .false.
            ocontinue = .false.
            call util_read_start_dir(file_prefix, rtdb_name)
         else if (inp_compare(.false.,'scratch_dir',field)) then
            status = inp_line(scratch_dir)
            istart = 0
            status = inp_strtok(scratch_dir,' ',istart, iend)
            scratch_dir(istart:iend) = ' '
         else if (inp_compare(.false.,'permanent_dir',field)) then
            status = inp_line(permanent_dir)
            istart = 0
            status = inp_strtok(permanent_dir,' ',istart, iend)
            permanent_dir(istart:iend) = ' '
         else if (inp_compare(.false.,'echo',field)) then
            echo = .true.
         else if (inp_compare(.false.,'ecce_print', field)) then
            if (inp_a(ecce_file_name)) then
               call ecce_print_file_open(ecce_file_name)
               call ecce_print_echo_input(input_filename)
            endif
         else
            call errquit('input_start_opt: wierd error',0, INPUT_ERR)
         endif
         goto 10                ! End while
      endif
      rewind LuIn
      call inp_init(LuIn,LuOut)
c
      if (echo) call input_echo(LuIn,LuOut)
c
      if (rtdb_name .eq. ' ')
     $     call util_file_name('db',.false.,.false.,rtdb_name)
c
      if (.not. odirective) then
c
c     No start/restart directive presented.  See if we can find a database,
c     if so, assume a restart.  Otherwise it must be a startup.
c
         inquire(file=rtdb_name,exist=status)
         ostartup = .not. status
      endif
c
      rewind LuIn
      call inp_init(LuIn,LuOut)
c
c     Broadcast start options to everyone else
c
10000 call ga_brdcst(Msg_StartUp, ostartup, mitob1, 0)
      call ga_brdcst(Msg_StartUp, ocontinue, mitob1, 0)
      call util_char_ga_brdcst(Msg_startup, file_prefix, 0)
      call util_char_ga_brdcst(Msg_startup, scratch_dir, 0)
      call util_char_ga_brdcst(Msg_startup, permanent_dir, 0)
*
* now confirm (on each node) that scratch_dir and permanent_dir
* can have files
*
      call ga_sync()
*check permanent directory
      if(iocheckk) then
        call util_file_name('dir_check_p',.false.,.true.,
     &                      a_temporary_file)
        inquire(file=a_temporary_file, exist=already)
        bad_permanent_dir = .true.
        if(already) then
          iend = inp_strlen(a_temporary_file)
          write(luout,*)' Warning: test file already existed: ',
     &                  a_temporary_file(1:iend)
C         Assume all is well in the world, since file might be 
C         chmod 000
          bad_permanent_dir = .false.
        else
          open(UNIT=42,FILE=a_temporary_file,STATUS="new",ERR=91111)
          close(UNIT=42,STATUS="delete")
          call util_file_unlink(a_temporary_file)
          bad_permanent_dir = .false.
        endif
91111   continue
*check scratch directory
        call util_file_name('dir_check_s',.true.,.true.,
     &                      a_temporary_file)
        inquire(file=a_temporary_file, exist=already)
        bad_scratch_dir = .true.
        if(already) then
          iend = inp_strlen(a_temporary_file)
          write(luout,*)' Warning: test file already existed: ',
     &                  a_temporary_file(1:iend)
C         Assume all is well in the world, since file might be
C         chmod 000
          bad_scratch_dir = .false.
        else
          open(UNIT=43,FILE=a_temporary_file,STATUS="new",ERR=91122)
          close(UNIT=43,STATUS="delete")
          call util_file_unlink(a_temporary_file)
          bad_scratch_dir = .false.
        endif
91122   continue
      else
        bad_permanent_dir=.false.
        bad_scratch_dir=.false.
      endif
*
      if (bad_permanent_dir) then
        iend = inp_strlen(permanent_dir)
        write(luout,*)' could not open a file in permanent directory: ',
     &      permanent_dir(1:iend)
      endif
      if (bad_scratch_dir) then
        iend = inp_strlen(scratch_dir)
        write(luout,*)' could not open a file in scratch directory: ',
     &      scratch_dir(1:iend)
      endif
      if (bad_permanent_dir.and.bad_scratch_dir) then
        write(luout,*)
     &      ' Both permanent and scratch directory not accessible'
        call errquit('******** Fatal Error ********',911, INPUT_ERR)
      else if (bad_permanent_dir) then
        call errquit
     &      ('Fatal Error: permanent directory not accessible',911,
     &       INPUT_ERR)
      else if (bad_scratch_dir) then
        call errquit
     &      ('Fatal Error: scratch directory not accessible',911,
     &       INPUT_ERR)
      else
        return
      endif
c
      endsubroutine chemsh_input_file_info
