SUBROUTINE Write_vasp(poscar)
!----------------------------------------------------------------------
!  Copyright 2003-2014 Ecole Normale Supérieure de Lyon, 
!  Centre National de la Recherche Scientifique,
!  Université Claude Bernard Lyon 1. All rights reserved.
!
!  This work is registered with the Agency for the Protection of Programs 
!  as IDDN.FR.001.100009.000.S.P.2014.000.30625
!
!  Authors: P. Fleurat-Lessard, P. Dayal
!  Contact: optnpath@gmail.com
!
! This file is part of "Opt'n Path".
!
!  "Opt'n Path" is free software: you can redistribute it and/or modify
!  it under the terms of the GNU Affero General Public License as
!  published by the Free Software Foundation, either version 3 of the License,
!  or (at your option) any later version.
!
!  "Opt'n Path" is distributed in the hope that it will be useful,
!  but WITHOUT ANY WARRANTY; without even the implied warranty of
!
!  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
!  GNU Affero General Public License for more details.
!
!  You should have received a copy of the GNU Affero General Public License
!  along with "Opt'n Path". If not, see <http://www.gnu.org/licenses/>.
!
! Contact The Office of Technology Licensing, valorisation@ens-lyon.fr,
! for commercial licensing opportunities.
!----------------------------------------------------------------------

  Use Path_module, only : Nat, NGeomF,XyzGeomF,intCoordF,Coord,FrozAtoms,IndZmat,LatR,Ncoord, Vmd
  Use Io_module

  IMPLICIT NONE

  CHARACTER(SCHARS), INTENT(IN) :: poscar

  INTEGER(KINT) :: IGeom, Idx
  REAL(KREAL), ALLOCATABLE :: GeomTmpC(:), GeomTmp(:)
  CHARACTER(SCHARS) :: Line,Line2

  ! For VMD printing
  CHARACTER(SCHARS), PARAMETER :: FirstWord="mol "
  CHARACTER(LCHARS), PARAMETER :: LastWords=" type POSCAR first 0 last -1 step 1 filebonds 1 autobonds 1 waitfor all"

  CHARACTER(LCHARS) :: TITLE
  LOGICAL :: Debug

  INTERFACE
     function valid(string) result (isValid)
       CHARACTER(*), intent(in) :: string
       logical                  :: isValid
     END function VALID
  END INTERFACE

  debug=valid('write_vasp')
  if (debug) WRITE(*,*) '================ Entering Write_VASP ===================='

  ALLOCATE(GeomTmpC(3*Nat), GeomTmp(NCoord))

  Line2="00"
  DO IGeom=1,NGeomF
     WRITE(Line,'(I5)') IGeom-1
     Idx=2-Len_TRIM(ADJUSTL(Line))
     OPEN(IOTMP,File=TRIM(POSCAR) // "_" // Line2(1:Idx) // AdjustL(TRIM(Line)))
     ! we convert the coordinates into Cartesian coordinates
     SELECT CASE (Coord)
     CASE ('CART','HYBRID')
        GeomTmpC=Reshape(reshape(XyzGeomF(IGeom,:,:),(/Nat,3/),ORDER=(/2,1/)),(/3*Nat/))
     CASE ('ZMAT')
        GeomTmp=IntCoordF(IGeom,:)
        ! we have to generate the cartesian coordinates from the internal coordinates
        CAll Int2Cart(nat,indzmat,geomtmp,GeomTmpC)
     CASE ('MIXED')
        GeomTmp=IntCoordF(IGeom,:)
        CAll Mixed2Cart(nat,indzmat,geomtmp,GeomTmpC)
     END SELECT

     if (debug) WRITE(*,*) "Converting geom ",IGeom," into file ",Trim(POSCAR) // "_" // Line2(1:Idx) // AdjustL(TRIM(Line))

     WRITE(Title,'(A,I2,"/",I2)') Trim(Vasp_Title) // " - Geom ",IGeom-1,NGeomF-1

     Call PrintGeomVasp(Title,geomTmpC,Latr,FrozAtoms,IoTmp)

     CLOSE(IOTMP)
  END DO

  if (Vmd) THEN
     WRITE(IOOUT,*) "## Insert the following list into the Test_NEB_example.vmd file for an easy VMD use"

     Line2="00"
     WRITE(IOOUT,'(A)') TRIM(FirstWord) // " new " // TRIM(POSCAR) // "_00" // TRIM(LastWords)
     DO IGeom=2,NGeomF
        WRITE(Line,'(I5)') IGeom-1
        Idx=2-Len_TRIM(ADJUSTL(Line))
        WRITE(IOOUT,'(A)') TRIM(FirstWord) // " addfile " // TRIM(POSCAR) //  &
             "_" // Line2(1:Idx) // TRIM(ADJUSTL(Line)) // TRIM(LastWords)
     END DO
     WRITE(IOOUT,*) "## End of List"
  END IF

  if (debug) WRITE(*,*) '================  Write_VASP OVER ===================='
  DeALLOCATE(GeomTmpC, GeomTmp)

END SUBROUTINE Write_vasp
