#
# IMPORTED VARIABLES:
#
# OMNIMAKE_PRE_COPY_TO_PERSONAL_INSTALL_ROOT  -- targets to run before omnimake_copy_to_personal_install_root
# OMNIMAKE_POST_COPY_TO_PERSONAL_INSTALL_ROOT -- targets to run after omnimake_copy_to_personal_install_root

#
# When you build a framework, it just gets stuck in your BUILD_OUTPUTDIR.  This hook copies
# the project to your personal install root so that other projects can use the results
#

ifeq ($(OMNIMAKE_PROJECT_TYPE), Framework)
   OMNIMAKE_INSTALL_EXTENSION=.framework
endif
ifeq ($(OMNIMAKE_PROJECT_TYPE), Bundle)
   OMNIMAKE_INSTALL_EXTENSION=.$(BUNDLE_EXTENSION)
endif
ifeq ($(OMNIMAKE_PROJECT_TYPE), LoadableBundle)
   OMNIMAKE_INSTALL_EXTENSION=.$(BUNDLE_EXTENSION)
endif
ifeq ($(OMNIMAKE_PROJECT_TYPE), Palette)
   OMNIMAKE_INSTALL_EXTENSION=.palette
endif
ifeq ($(OMNIMAKE_PROJECT_TYPE), Tool)
   ifeq ($(PLATFORM_OS), winnt)
       OMNIMAKE_INSTALL_EXTENSION=.exe
   endif
endif
ifeq ($(OMNIMAKE_PROJECT_TYPE), Application)
   ifdef APP_WRAPPER_EXTENSION
       OMNIMAKE_INSTALL_EXTENSION=$(APP_WRAPPER_EXTENSION)
   else
       OMNIMAKE_INSTALL_EXTENSION=.app
   endif
endif
ifeq ($(OMNIMAKE_PROJECT_TYPE), EOApplication)
   ifdef APP_WRAPPER_EXTENSION
       OMNIMAKE_INSTALL_EXTENSION=$(APP_WRAPPER_EXTENSION)
   else
       OMNIMAKE_INSTALL_EXTENSION=.app
   endif
endif
ifeq ($(OMNIMAKE_PROJECT_TYPE), WebObjectsApplication)
   ifeq ($(PLATFORM_OS), winnt)
       OMNIMAKE_INSTALL_EXTENSION=.exe
   endif
endif


.PHONY : omnimake_create_installdir

.PHONY : omnimake_copy_project_to_personal_install_root omnimake_copy_dll_to_personal_install_root

OMNIMAKE_FAKE_DLL_INSTALL_DIR=$(OMNIMAKE_INSTALL_DIR)/../Executables

$(OMNIMAKE_INSTALL_DIR) $(OMNIMAKE_FAKE_DLL_INSTALL_DIR):
	$(SILENT) $(MKDIRS) $@

ifeq ($(PLATFORM_OS), winnt)
  ifeq ($(OMNIMAKE_PROJECT_TYPE), Framework)
    OMNIMAKE_POST_COPY_TO_PERSONAL_INSTALL_ROOT += omnimake_copy_dll_to_personal_install_root
  endif
endif

#
#  Only use remove_private_headers_link if we're building a Framework project, otherwise
#  framework.make.postamble isn't included and the target is invalid.
#

ifeq ($(OMNIMAKE_PROJECT_TYPE), Framework)
  OMNIMAKE_INSTALL_FRAMEWORK_HEADERS := omnimake_make_writable installhdrs remove_private_headers_link
  OMNIMAKE_INSTALL_FRAMEWORK_HEADERS += change-permissions
endif

omnimake_copy_to_personal_install_root: omnimake_announce_copy_to_personal_install_root $(OMNIMAKE_PRE_COPY_TO_PERSONAL_INSTALL_ROOT) _omnimake_copy_to_personal_install_root $(OMNIMAKE_POST_COPY_TO_PERSONAL_INSTALL_ROOT)

omnimake_announce_copy_to_personal_install_root:
	$(SILENT) $(ECHO) Copying built project to personal install directory

# Rather than using 'if' to see if the files exist here, we'll just use chmod
# and use '-' to not fail the build if the command fails.  The problem is that
# sometimes the file is a file, sometimes a directory and sometimes a link.
# Various versions of 'test' to not support these file types.  It doesn't
# appear that the version of test used is /bin/test on Mach (which does).
# Maybe it is using the shell-builtin.  Anyway, this seems more portable.

_omnimake_copy_to_personal_install_root: $(OMNIMAKE_INSTALL_DIR)
	-$(SILENT) $(CHMOD) -Rf u+w $(OMNIMAKE_INSTALL_DIR)/$(NAME)$(OMNIMAKE_INSTALL_EXTENSION);
	$(SILENT) $(RM) -rf $(OMNIMAKE_INSTALL_DIR)/$(NAME)$(OMNIMAKE_INSTALL_EXTENSION);
	$(SYMLINK) $(BUILD_OUTPUT_DIR)/$(NAME)$(OMNIMAKE_INSTALL_EXTENSION) $(OMNIMAKE_INSTALL_DIR)/$(NAME)$(OMNIMAKE_INSTALL_EXTENSION)

omnimake_copy_dll_to_personal_install_root: $(OMNIMAKE_FAKE_DLL_INSTALL_DIR)
	$(SILENT) $(RM) -f $(OMNIMAKE_FAKE_DLL_INSTALL_DIR)/$(NAME).dll
	$(SILENT) $(SYMLINK) $(BUILD_OUTPUT_DIR)/$(NAME)$(OMNIMAKE_INSTALL_EXTENSION)/$(NAME)$(BUILD_TYPE_SUFFIX).dll $(OMNIMAKE_FAKE_DLL_INSTALL_DIR)/$(NAME).dll

omnimake_make_writable:
	-$(CHMOD) -Rf +w $(OMNIMAKE_INSTALL_DIR)/$(NAME)$(OMNIMAKE_INSTALL_EXTENSION);
