#===========================================================================
#
# OPUS Makefile
#
# author: Scott Binegar 
#
# Modification History:
#
#   Date       OPR         Who       Reason
# --------   -------   ------------  -----------------------------------------
# 09/20/01   n/a       S. Binegar    first version
# 02/26/02   44741     S. Binegar    add jar files to install
# 01/16/03   47179     Chris blindly made changes for Jill
# 06/22/05   53328     sontag        install the opus/install txt files too
# 10/06/06   56418     MSwam         add dbms/tools/wavecal_months dir
# 12/23/09   64182     MSwam         changes for Linux
# 01/05/09   64274     MSwam         take out all OPUS-IRAF
# 12/23/10   38534     MSwam         more comments
# 05/19/11   68236     MSwam         remove SDDL 
#
#===========================================================================
# Eliminate implicit rules
.SUFFIXES:

# Define TOPDIR if not already there and export it to all sub-makefiles.
ifndef TOPDIR
  export TOPDIR:=$(shell pwd)
endif

# SHAREROOT envvar must be defined in order to build OPUS
ifneq ($(SHAREROOT),)
  # pull in some operating system-specific definitions
  include $(SHAREROOT)/platform.defs
else
  warn_text = ERROR: Environment variable SHAREROOT is not defined!
  WARN = $(warning $(warn_text))
.PHONY: warn
warn:
        # bail out
	@echo $(warn_text)
	@false
endif


# Build source
all: src
	@echo "Finished build"

# Build the source
src : FORCE
	@echo "Building Source..."
	@cd src && $(MAKE)

# Make clean
clean:
	@cd src && $(MAKE) clean

FORCE:

#======================= Install definitions ======================
#
# set installation variables if the Installation directory is defined
ifneq ($(INSTALL_DIR),)

bin_files = $(filter-out %/CVS , $(wildcard bin/$(OS_SUB_DIR)/*))
dest_bin_files = $(addprefix $(INSTALL_DIR)/, $(bin_files))

lib_files = $(filter-out %/CVS , $(wildcard lib/$(OS_SUB_DIR)/*))
dest_lib_files = $(addprefix $(INSTALL_DIR)/, $(lib_files))

dat_files = $(filter-out %/CVS , $(wildcard dat/*))
dest_dat_files = $(addprefix $(INSTALL_DIR)/, $(dat_files))

# add %/wavecal_months to filter-out because it is included in the
#   dbms/tools/wavecal_months/* selection and including it individually
#   collided with the dirs: rule below. (it is not a file). - Mary
dbms_files = $(filter-out %/CVS %/wavecal_months , $(wildcard dbms/load/*) \
				  $(wildcard dbms/sql_files/*) \
				  $(wildcard dbms/tools/wavecal_months/*) \
				  $(wildcard dbms/tools/*) )
dest_dbms_files = $(addprefix $(INSTALL_DIR)/, $(dbms_files))

definitions_files = $(filter-out %/CVS %/unix , $(wildcard definitions/*)) \
	            $(filter-out %/CVS , $(wildcard definitions/unix/*))
dest_definitions_files = $(addprefix $(INSTALL_DIR)/, $(definitions_files))

hlp_files = $(filter-out %/CVS %/gif , $(wildcard hlp/*)) \
            $(filter-out %/CVS, $(wildcard hlp/gif/*)) 
dest_hlp_files = $(addprefix $(INSTALL_DIR)/, $(hlp_files))

Install_files = $(filter-out %/CVS , $(wildcard install/*.txt))
dest_Install_files = $(addprefix $(INSTALL_DIR)/, $(Install_files))

java_files = $(wildcard bin/java/*.jar)
dest_java_files = $(addprefix $(INSTALL_DIR)/, $(java_files))

#  added _dirs rules to work with the random structure of the owl_template
#   directories.  Once that worked added the dbms_dirs rules to simplify
#   the dirs: rule below.

dbms_dirs := $(filter-out %/CVS, $(wildcard dbms/*) dbms/tools/wavecal_months)
dest_dbms_dirs := $(addprefix $(INSTALL_DIR)/, $(dbms_dirs))

owl_template_dirs := $(filter-out %/CVS, $(wildcard owl_templates/*))
dest_owl_template_dirs := $(addprefix $(INSTALL_DIR)/, $(owl_template_dirs))

#  used the owl_template_dirs rule above to find the files.
#  CVS filter currently not needed because the search is for the specif files
#   *.job and *.dag. If changed to * then filter-out needs to be added.

find_owl_template_files = $(wildcard $(owl_template_dir)/*.job $(owl_template_dir)/*.dag)
owl_template_files := $(foreach owl_template_dir, $(owl_template_dirs), $(find_owl_template_files))
dest_owl_template_files := $(addprefix $(INSTALL_DIR)/, $(owl_template_files))

.PHONY : new
new : dirs install
	@echo "NEW Install complete"

update_dirs =  bin lib dat dbms definitions hlp Install java owl_template

.PHONY : install
install : $(update_dirs)
	@chmod -R go-w $(INSTALL_DIR)
	@chmod ugo+rx $(INSTALL_DIR)/bin/$(OS_SUB_DIR)/*
	@chmod ugo+r $(INSTALL_DIR)/lib/$(OS_SUB_DIR)/*
	@chmod ugo+rx $(INSTALL_DIR)/bin/java/*
	@echo "Install/Update complete"

#
# Create all of the destination directories
#

partial_dir_list := bin/$(OS_SUB_DIR) bin/java dat definitions/unix hlp/gif install lib/$(OS_SUB_DIR)
dest_partial_dirs_list := $(addprefix $(INSTALL_DIR)/, $(partial_dir_list))
dest_dirs_list := $(dest_partial_dirs_list) $(dest_dbms_dirs) $(dest_owl_template_dirs) 

.PHONY : dirs_intro dirs

dirs_intro :
	@echo "INSTALL_DIR = $(INSTALL_DIR)"
	@sleep 5

$(dest_dirs_list) :
	mkdir -p $@

testbed_ver :
	@echo "create the dat/testbed.ver file..."
	src/scripts/build_testbed_ver.py

dirs : dirs_intro $(dest_dirs_list) testbed_ver

#
# Install all of the build files
#

bin : $(dest_bin_files)
dat : $(dest_dat_files)
dbms : $(dest_dbms_files)
definitions : $(dest_definitions_files)
hlp : $(dest_hlp_files)
Install : $(dest_Install_files)
java : $(dest_java_files)
lib : $(dest_lib_files)
owl_template : $(dest_owl_template_files)

dest_files := $(foreach dir, $(update_dirs), $(dest_$(dir)_files))

$(dest_files) : $(INSTALL_DIR)/% : %
	@echo 'Installing: $(<)' 
	@cp -f '$(<)' '$(@)'

print:
	@echo $(dest_files)

else # INSTALL_DIR not defined

ERR = $(error ERROR)
.PHONY: install
install:
	@echo "INSTALL_DIR not defined"
	$(ERR)
endif 
