SHELL=/bin/bash

include ../common.mk
include release.cfg
RELEASE_DATE:=$(subst \',,$(RELEASE_DATE))#Remove Single-Quotes
export VERSION RELEASE_DATE OS_NAME OS_ARCH OS_BITS

 $(info Release: v$(VERSION) on $(RELEASE_DATE))
 $(info OS/Arch: $(OS_NAME) ($(OS_BITS)-bit))

SELF_DIR := $(dir $(abspath $(lastword $(MAKEFILE_LIST))))
HR = --------------------------------------------------------------------------------
define MAKE_HELP_MESSAGE
Help Commands  -- e.g. `make help`

  help  - Show (this) help message.

$(HR)
Build Commands -- These copy or build files in the BUILD_v$(VERSION:-XX) subdir.
  They should not affect the current repository outside of that subdir.)

  src     - build source code release
  text    - build text interface
  text-32 - build text interface for 32-bit architecture
  gui     - build Java GUI

OS-specific Build Commands:
  win     - build Windows Java wrappers and installers
  mac     - build mac bundle

$(HR)
Repository Commands
  These affect the current working directory. (Use with caution!!)
	After running one or more of these commands, you may wish to commit the 
	changes and push them to git.
	
  set     - Set release info (VERSION, RELEASE_DATE) in release.cfg.
  ver     - Updates versioned text files (Version.txt, version.h, etc).
	html    - Updates version and release date in several HTML manual files. 
	logo    - Regenerates GUI Logo Image with version number and copyright year.
	dox     - Updates Doxygen version number and runs Doxygen (for RNA_class)
	jdoc    - Run JavaDoc
	ver-all - Runs all version commands (ver html logo dox jdoc)

  upd     - Update (pull) the current Git repository.
            This is sometimes useful if errors are discovered and fixed (on 
            another machine) while building a release.
  clean   - Remove existing build directories -- i.e. all 'BUILD_v*' in
              $(SELF_DIR)
  x-clean - Clean ALL files and directories that are untracked 
            or ignored by Git. (!!)
  upload  - Copies the built binaries to the RNA2 webserver (using rsync)

$(HR)
Advanced Commands (combine Repositry and Build Commands)
  full    - Build the full release (which depends on OS)
            For $(OSNAME) this is: `make $(FULL_BUILD)`
  x-full  - Same as `make upd && make xclean full`

endef

build = bash scripts/buildRelease.bash -f

ifeq ($(OS_NAME),Windows)
	FULL_BUILD = src text text-32 gui win
else ifeq ($(OS_NAME),Linux)
	FULL_BUILD = src text text-32 gui
else # Mac
	FULL_BUILD = text gui mac
endif

help:
	@$(eval export MAKE_HELP_MESSAGE) echo "$$MAKE_HELP_MESSAGE"

full: $(FULL_BUILD)

x-full xfull:
	make update && make x-clean full

upd update:
	git pull
#$${B}Esc$${Z}/$${B}Enter$${Z}
x-clean:
	R="$(TFMT_BR)" Z="$(TFMT_RST)" B="$(TFMT_BB)"; \
	echo -e "$${R}\nCleaning would remove the following:";  git clean -xdn | sed 's/Would remove//g'; echo "$${Z}"; \
	read -p "Really $${R}DELETE$${Z} all untracked and ignore files!!!??? Press $${R}Y$${Z} to CLEAN or $${B}Esc$${Z} to SKIP. " -n1 -s INPUT \
	&& if [[ $$INPUT == [yY] ]]; then \
		echo; git clean -xdf; \
	else \
		echo -e "\nSKIPPED cleaning!"; \
	fi;

clean:
	R="$(TFMT_BR)" Z="$(TFMT_RST)" B="$(TFMT_BB)"; \
	dirs=( '$(SELF_DIR)/BUILD_v'* ); \
	if [[ $${$#dirs[@]} -eq 0 ]]; then \
		echo "No build dirs to remove."; \
	else \
	  echo -en "$${R}\nRemove the following directories:\n\t"; \
	  printf '%s\n\t' "$${dirs[@]}"; \
	  echo "$${Z}"; \
	  read -p "Really $${R}DELETE$${Z} these build dirs? Press $${R}Y$${Z} to DELETE or $${B}Esc$${Z} to SKIP. " -n1 -s INPUT \
	  && if [[ $$INPUT == [yY] ]]; then \
	    echo; git clean -xdf; \
	  else \
	    echo -e "\nSKIPPED cleaning!"; \
	  fi; \
	fi;

src:
	$(build) -b=s

set: 
	@$(build) set-cfg

text:
	$(build) -b=t  -t=+p #add python for native build.

text32 text-32:
	@echo Target Arch: 32-bit
	$(build) -b=t  -a=32

gui:
	$(build) -b=g

win:
	bash scripts/buildwin.sh all

test-headless:
	# Determine whether or not this environment is headless (in which case the mac build will fail)
	@cd scripts && javac test_headless.java && java test_headless || \
	  { echo >&2 "Environment is Headless. Cannot build Mac Bundle."; exit 1; }

mac: test-headless
	bash scripts/buildMacBundle.sh -B
	cd BUILD_v$(VERSION) && [[ -f RNAstructure-$(VERSION).dmg ]] && mv RNAstructure-$(VERSION).dmg RNAstructureForMac.dmg
	bash scripts/buildMacBundle-StructureEditor.sh -B
	cd BUILD_v$(VERSION) && [[ -f StructureEditor-$(VERSION).dmg ]] && mv StructureEditor-$(VERSION).dmg StructureEditor.dmg

upload:
	@echo "Using rsync to copy build products to RNA2 webserver."
	@echo "For this to work, you *should* have the hostname 'rna2' setup in ~/.ssh/config"
	@echo " and you *must* be a member of the 'release' group on RNA2 (or be root)."
	@echo "Files will be copied to /home/release/$(VERSION)/"
	cd BUILD_v$(VERSION); \
	shopt -s nullglob; \
	rsync *.dmg *.tgz *.zip *.exe rna2:/home/release/$(VERSION)/

ver:
	$(build) -b=v -t=T -q

ver-all:
	$(build) -b=v -t=THLDJ -q

html:
	$(build) -b=v -t=H -q	

logo:
	$(build) -b=v -t=L -q	

dox:
	$(build) -b=v -t=D -q	

jdoc:
	$(build) -b=v -t=J -q	