      SUBROUTINE GIPOUT( ISTART, FILEC )
C
C SUBROUTINE FOR DRAW PROGRAM TO OUTPUT A FILE FOR "GIP"
C
      INCLUDE 'SIZES'
      IMPLICIT REAL (A-H,O-Z)
      CHARACTER*80 FILEC, FILEIN,FILOUT,FILPLT, LLEGND
      CHARACTER*80 KEYWRD,KOMENT,TITLE,COMAND, DUMMY
      CHARACTER*6 ATSYMB
      CHARACTER*2 OPTFLG( 3)
      LOGICAL ERROR, WARNED
      COMMON/KEYS/ KEYWRD,KOMENT,TITLE
      COMMON/ATSYMB/ ATSYMB( 200)
      COMMON/ATOMS/ CO(3, NUMATM),IE( NUMATM),NATOMS, ATCHG( NUMATM)
      COMMON/GEOM/ COOLD(3, NUMATM),NA( NUMATM),NB( NUMATM),NC( NUMATM)
      COMMON/LEGEND/ FILEIN,FILOUT,FILPLT, LLEGND
      COMMON /INTCOR/ XNDOGM(3, NUMATM), INTFRE(3, NUMATM)
      LOGICAL DEBUG, DEBUGL, DEBUGN, DEBUGO, DEBUGP, DEBUGI
      COMMON /DEBCOM/ DEBUG, DEBUGL, DEBUGN, DEBUGO, DEBUGP, DEBUGI
C
C?      IF ( ISTART .LT. 0 ) ISTART = 0

      IF (FILEC(1:1) .EQ. '*') THEN
         COMAND = FILEIN
         IBRAK= INDEX( FILEIN(1:80),']')
         IF (IBRAK .GT. 2) COMAND= FILEIN(IBRAK+1:)
         IDOT = INDEX(COMAND(1:80),'.')
         IF ( IDOT .GT. 1 ) COMAND = COMAND(:IDOT-1)
      ELSEIF (FILEC(:1) .EQ. ' ') THEN
         COMAND = FILEIN(:INDEX(FILEIN(1:80),'.')-1)
      ELSE
         COMAND = FILEC
      ENDIF

      IDOT = INDEX( COMAND(1:80), '.')
      IF (IDOT .LT. 1) COMAND=COMAND(:INDEX(COMAND,' ')-1)//'.GIP'
      ITEMP = INDEX(COMAND,' ')-1

      OPEN (UNIT=8,FILE=COMAND(:ITEMP),STATUS='NEW', ERR=8000 )
      REWIND 8
      WARNED = .FALSE.

      CALL PREULR( THETA, PHI, PSI )
      WRITE (8,
     . '('' GIP/DRAW DO NOT EDIT THIS LINE!'',
     . 3F14.8)') THETA, PHI, PSI

      WRITE (8,'(A)') KEYWRD
      WRITE (8,'(A)') KOMENT
      WRITE (8,'(A)') TITLE

      DO 10 I=1,NATOMS
         DO 9 J= 1, 3
           IF( INTFRE( J, I) .GT. 9) THEN
             OPTFLG( J) = ' 1'
             IF( .NOT. WARNED) THEN
               CALL DEBUGR( 'Non-INTEGER optimization flag set to 1.')
               WARNED = .TRUE.
             ENDIF
           ELSE
             WRITE( OPTFLG( J), '(I2)') INTFRE( J, I)
           ENDIF
  9      CONTINUE
         IF( I.EQ.1) THEN
            IF (ISTART.EQ.0) THEN
               WRITE (8,1000) ATSYMB(IE(I)), CO( 1, I), OPTFLG( 1),
     .             CO( 2, I), OPTFLG( 2), CO( 3, I), OPTFLG( 3),
     .             NA(I), NB(I), NC(I)
            ELSE
               WRITE (8,1000) ATSYMB(IE(I)), CO( 1, I), OPTFLG( 1),
     .             CO( 2, I), OPTFLG( 2), CO( 3, I), OPTFLG( 3),
     .             NA(I)+1, NB(I)+2, NC(I)+3
            ENDIF
         ELSEIF( I.EQ.2) THEN
            IF (ISTART.EQ.0) THEN
               WRITE (8,1000) ATSYMB(IE(I)), CO( 1, I), OPTFLG( 1),
     .              CO( 2, I), OPTFLG( 2), CO( 3, I), OPTFLG( 3),
     .              NA(I)+ISTART, NB(I), NC(I)
             ELSE
               WRITE (8,1000) ATSYMB(IE(I)), CO( 1, I), OPTFLG( 1),
     .              CO( 2, I), OPTFLG( 2), CO( 3, I), OPTFLG( 3),
     .              NA(I)+ISTART, NB(I)+1, NC(I)+2
             ENDIF
         ELSEIF( I.EQ.3) THEN
            IF (ISTART.EQ.0) THEN
               WRITE (8,1000) ATSYMB(IE(I)), CO( 1, I), OPTFLG( 1),
     .               CO( 2, I), OPTFLG( 2), CO( 3, I), OPTFLG( 3),
     .               NA(I)+ISTART, NB(I)+ISTART, NC(I)
            ELSE
               WRITE (8,1000) ATSYMB(IE(I)), CO( 1, I), OPTFLG( 1),
     .               CO( 2, I), OPTFLG( 2), CO( 3, I), OPTFLG( 3),
     .               NA(I)+ISTART, NB(I)+ISTART, NC(I)+1
            ENDIF
         ELSE
            WRITE (8,1000) ATSYMB(IE(I)), CO( 1, I), OPTFLG( 1),
     .       CO( 2, I), OPTFLG( 2), CO( 3, I), OPTFLG( 3),
     .       NA(I)+ISTART, NB(I)+ISTART, NC(I)+ISTART
         ENDIF
 10   CONTINUE

 1000 FORMAT ( 2X, A5, 3( F15.6, 1X, A2), 3I5)

      WRITE (8,1000) '0', 0.00, '0', 0.00, '0', 0.00, '0', 0,0,0
      CLOSE (UNIT=8)
      WRITE ( DUMMY, '( '' GIP OUTPUT IN FILE '', A,
     .                  '' OFFSET BY '', I4, ''.'')')
     .      COMAND(:ITEMP), ISTART
      CALL DEBUGR( DUMMY(:ITEMP+36) )
      RETURN
 8000 CALL DEBUGR('ERROR OPENING '// COMAND(:ITEMP)//
     .      '; GIPOUT QUITS.' )
      RETURN
      END
