# Set native Jar name based on OS and Architecture
set(NATIVE_NAME "natives-@MAVEN_LOCAL_NATIVE_NAME@")

message("Package VTK for Java - ${NATIVE_NAME}")

# Create the natives directory

# Only for windows but no harm to do it always
file (COPY "${CMAKE_INSTALL_PREFIX}/bin/"
      DESTINATION "${CMAKE_INSTALL_PREFIX}/${NATIVE_NAME}"
      FILES_MATCHING
      PATTERN *.dll
      PATTERN *.jnilib
      PATTERN *.cmake
      PATTERN *.txt
      PATTERN *.in
)

# For Apple and Linux
file (COPY "${CMAKE_INSTALL_PREFIX}/@VTK_INSTALL_LIBRARY_DIR@/"
      DESTINATION "${CMAKE_INSTALL_PREFIX}/${NATIVE_NAME}"
      FILES_MATCHING
      PATTERN *.so
      PATTERN *.dylib
      PATTERN *.jnilib
      PATTERN *.cmake
      PATTERN *.in
      PATTERN *.txt
)

# Rename vtk.jar with version number
execute_process(
  COMMAND "${CMAKE_COMMAND}" -E rename
     "${CMAKE_INSTALL_PREFIX}/@VTK_INSTALL_LIBRARY_DIR@/vtk.jar"
     "${CMAKE_INSTALL_PREFIX}/vtk-@VTK_MAJOR_VERSION@.@VTK_MINOR_VERSION@.jar"
)

# Create the jar file
execute_process(
    COMMAND "@JAVA_ARCHIVE@" -cf "@CMAKE_INSTALL_PREFIX@/vtk-@VTK_MAJOR_VERSION@.@VTK_MINOR_VERSION@-${NATIVE_NAME}.jar" -C "@CMAKE_INSTALL_PREFIX@/${NATIVE_NAME}" .
)

set(VTK_JAVA_CLEANUP_INSTALL "@VTK_JAVA_CLEANUP_INSTALL@")
set(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT "@CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT@")

if (NOT VTK_JAVA_CLEANUP_INSTALL AND NOT CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
  # Remove un-wanted files
  execute_process(
    COMMAND "${CMAKE_COMMAND}" -E remove_directory "${CMAKE_INSTALL_PREFIX}/bin"
    )
  execute_process(
    COMMAND "${CMAKE_COMMAND}" -E remove_directory "${CMAKE_INSTALL_PREFIX}/lib"
    )
  execute_process(
    COMMAND "${CMAKE_COMMAND}" -E remove_directory "${CMAKE_INSTALL_PREFIX}/share"
    )
  execute_process(
    COMMAND "${CMAKE_COMMAND}" -E remove_directory "${CMAKE_INSTALL_PREFIX}/include"
    )
  execute_process(
    COMMAND "${CMAKE_COMMAND}" -E remove_directory "${CMAKE_INSTALL_PREFIX}/www"
    )
endif()

execute_process(
  COMMAND "${CMAKE_COMMAND}" -E copy pom.xml "${CMAKE_INSTALL_PREFIX}/pom.xml"
)
execute_process(
  COMMAND "${CMAKE_COMMAND}" -E copy MAVEN-README.txt "${CMAKE_INSTALL_PREFIX}/README.txt"
)

message("Java repackaging done")