#!/bin/bash

# find size of pointer to Fortran derived type
# used by quippy and AtomEye for Fortran<->C interoperability


cat > build/${QUIP_ARCH}/Makefile <<EOF
ifeq (\${QUIP_ARCH},)
  include Makefile.arch
else
  include Makefile.\${QUIP_ARCH}
endif
include Makefile.inc
include Makefile.rules

sizeof_fortran_t: sizeof_fortran_t.f90
	\${F90} \$< -o \$@
EOF

cd build/${QUIP_ARCH}

cat > sizeof_fortran_t.f90 <<EOF
program sizeof_fortran_t
 
  type ptr_type
     type(ptr_type), pointer :: p => NULL()
  end type ptr_type
  type(ptr_type) :: ptr
  integer, allocatable, dimension(:) :: ptr_int

  write (*,*) size(transfer(ptr, ptr_int))

end program sizeof_fortran_t
EOF

make -I ../../arch -I../.. sizeof_fortran_t > /dev/null
echo SIZEOF_FORTRAN_T=$(./sizeof_fortran_t)

rm sizeof_fortran_t.f90
rm sizeof_fortran_t
rm Makefile

