c*******************************************************************************
c     MAIN
c*******************************************************************************
c
c                               MC-TINKER
c
c     PROGRAM FOR MULTICONFIGURATION MOLECULAR MECHANICS CALCULATIONS
c
c     This routine is the driver for MCMM calculations of energies,
c     gradients, and Hessians at given geometries.
c
      IMPLICIT DOUBLE PRECISION (A-H,O-Z)
c
      include 'mcparm.inc'
      include 'mcperc.inc'
      include 'mccomm.inc'
      include 'eemcmm.inc'
c
      character*256 finame
      character*7  fistat
      character*20  fstat
      logical lexit
c
c     Open the input and output files (fu5 and fu6)
c
c     Use the character variables fistat (file status), finame (filename),
c     and the subprogram mcfope to open the input data file and the long
c     output files which are linked to FORTRAN units fu5 and fu6, 
c     respectively.
c
c     The error statement for units fu5 and fu6 are written to unit 6
c     (assumed to be standard out) because if there is a problem
c     with units fu5 or fu6 then the code cannot use fu6 for its output
c     and the following error message may not be written.
c
      lexit  = .false.
c
      fistat = 'unknown'
      finame = 'esp.fu86'
c
      call mcfope (fu6, fistat, finame, lexit)
      if (lexit) then
         write (6, *) 'Error opening the long output file'
         stop 'main 1'
      endif
c
      fistat = 'old'
      finame = 'esp.fu85'
c
      call mcfope (fu5, fistat, finame, lexit)
      if (lexit) then
         write (6, *) 'Error opening the input file'
         stop 'main 2'
      endif
c
c     Write out program header to unit fu6
c
      call mcpthd(fu6)
c
c     Read in the options from the general input file (fu5)
c
      call mcrfgi
c
c     Close the input data file
c
      fstat = 'keep'
      call mcfclo (fu5, fstat)
c
c     Write out the summary table of all input parameters
c
      call mcptab
c
c     Set up the calculation by reading and preparing the appropriate 
c     information
c
      call mcscal
c
c     Compute the properties (energies, gradients, and Hessians) for
c     the points in esp.fu84 input file.
c
      if (mcityp.eq.1.or.mcityp.eq.2.or.mcityp.eq.3) then                  0421OT08
      call mcsrun
      elseif (mcityp.eq.4) then                                            0421OT08
c       if (mciee.eq.0) then 
        call mcgradtest                                                      0421OT08
c       else 
c        write (fu6,4501)
c         stop 'main 3'
c       endif
      elseif (mcityp.eq.5) then
c       if (mciee.eq.0) then
        call mchesstest
c       else
c        write (fu6,4502)
c         stop 'main 4'
c       endif
      elseif (mcityp.eq.6) then                                            0227MH10
         call eechargetest                                                 0227MH10
      elseif (mcityp.eq.7) then                                            0227MH10
         mccdqv=1                                                          0227MH10
         call eedqdphitest                                                 0227MH10
      elseif (mcityp.eq.8) then                                            0227MH10
         mccdqr=1                                                          0227MH10
         call eedqdrtest                                                   0227MH10
      endif
c
      write(fu6,4500) 
 4500 format(/1x,25('*'),' End of MC-TINKER output file ',24('*'))
 4501 format(/1x,' The GRADTEST option is not currently available', 
     * 'for EE-MCMM calculations ')
 4502 format(/1x,' The HESSTEST option is not currently available', 
     * ' for EE-MCMM calculations ')

c
c     Close the output file
c
      fstat = 'keep'
      call mcfclo (fu6, fstat)
c
c      stop 'Exit MC-TINKER' 
c
      end
