      subroutine gau_ext_opt(optinp,geom,ierr,prognme)
C
**********************************************************************
*    Gau_ext_opt is an  which uses Gaussian's external keyword
*    and Gaussian's optmizer
*
*      MULTILEVEL  <-->  G03 <--> Gau_external <--> MULTILEVEL
*   
*
*     Created:  March. 2004 by Yan Zhao
*     Revised:
*     Purpose:  To perform  optimization using Gaussian's optimizer 
*     
*     Called by:  mlohook
*     Calls:  optghk, opthhk 
*
*     Variables defined here:  none.
**********************************************************************
c
      use files
      use input, only : mlopt, mlinp,cartgeom
      use optimize, only : ener, grad, hess
      
      implicit  none
c
      integer            ierr, i, j, k, n3tm
      character*10       prognme                                        0808RMP12
      character*20       basis, method,v3s 
      type (cartgeom) :: geom    
      type (mlopt)    :: optinp
 
c
      n3tm = 3*mlinp%geom%natoms

c       call opthhk
       basis = ' '     
       method = 'external' 
       v3s = 'v3s'
       open (unit=extoinf, status='unknown', file=extin, err=9991)
       call g98inp(extoinf, method,basis, geom, 
     *            optinp%optl, 1, v3s)
c
       if (prognme(:3).eq.'g94') then                                   0808RMP12
        call system('./g94shuttle extopt.inp extopt.out')
       elseif (prognme(:3).eq.'g98') then
        call system('./g98shuttle extopt.inp extopt.out')
       elseif (prognme(:3).eq.'g03') then
        call system('./g03shuttle extopt.inp extopt.out')
       elseif (prognme(:3).eq.'g09') then
        call system('./g09shuttle extopt.inp extopt.out')
       endif
c
       call g98outo(geom, ener, grad, ierr) 
            if (ierr.eq.1) go to 7000 
c
       mlinp%geom = geom
c
c  Display the results of the optimization
c
      write(outfile,1100)
      write(outfile,1250)  ener
      write(outfile,1300)
      write(outfile,1350)  grad(:n3tm)
      write(outfile,1400)
      write(outfile,1450)
      do 20 i=1, geom%natoms
         write(outfile,1500) geom%atm(i), geom%aucoord(3*i-2),
     *       geom%aucoord(3*i-1), geom%aucoord(3*i)
  20  continue
      write(outfile,1550)
      do 40 i=1, geom%natoms
         write(outfile,1500) geom%atm(i), geom%angcoord(3*i-2),
     *       geom%angcoord(3*i-1), geom%angcoord(3*i)
  40  continue

1100  format(/2(/80('-')),//2x,'External Optimization with Gaussian',
     *         a,//80('-')/)
1250  format(/2x,'Optimized Energy:',t30,f19.12)
1300  format(/2x,'Optimized Cartesian Gradient:')
1350  format(t20,d15.8,t40,d15.8,t60,d15.8)
1400  format(/2x,'Optimized Geometry:')
1450  format(//5x,'Atomic Coordinates (in a.u.):',//8x,'Atom', t28, 'X',
     *         t48, 'Y', t68, 'Z'/)
1500  format(9x,a2,t20,d15.8,t40,d15.8,t60,d15.8)
1550  format(//5x,'Atomic Coordinates (in ang.):',//8x,'Atom', t28, 'X',
     *         t48, 'Y', t68, 'Z'/)  
      return

9991  write (outfile,*) 'Error creating Gaussian98 Opt input file'
      stop 'progohk 1'
7000  write (outfile,'(/a)') 'ERROR: External Optimization failed.'
      stop 'gau_ext_opt'
      end     
       
