# 
# * Fri Dec 28 2012 04:15:55 PM Steven E. Pav <steven@cerebellumcapital.com>
#
# Makefile 'remixed' from RTikZDevice and optmatch packages. HT to Sharpie and
# markmfredrickson.
#
# you may have too
# sudo apt-get install -y texinfo
#
#
# Created: 2012.12.28
#

SHELL 						 = /bin/bash

DOCKER 						?= $(shell which docker)
BIN_TIME          ?= $(shell which time)

R_DEV_FILES 			?= $(wildcard ./R/*.r)
R_FILES 					?= $(R_DEV_FILES)
R_FILES 					+= $(wildcard ./inst/tests/*.r)
R_FILES 					+= $(wildcard ./man-roxygen/*.R)
R_FILES 					+= $(wildcard ./tests/*.R)

R_QPDF 						?= $(shell which qpdf)
R_GSCMD						?= $(shell which gs)
GS_QUALITY 				?= 'ebook'

M4_FILES					?= $(wildcard m4/*.m4)

VMAJOR 						 = 1
VMINOR 						 = 1
VPATCH  					 = 0
VDEV 							 = .3000
VERSION 					 = $(VMAJOR).$(VMINOR).$(VPATCH)$(VDEV)
TODAY 						:= $(shell date +%Y-%m-%d)

PKG_NAME 					:= SharpeR
PKG_LCNAME 				:= $(shell echo $(PKG_NAME) | tr 'A-Z' 'a-z')
PKG_VERSION				:= $(VERSION)
PKG_SRC 					:= $(shell basename $(PWD))

PKG_TGZ 					 = $(PKG_NAME)_$(PKG_VERSION).tar.gz

LOCAL 						:= .local
RCHECK 						 = $(PKG_NAME).Rcheck
RCHECK_SENTINEL 	 = $(RCHECK)/$(PKG_NAME)/DESCRIPTION
PKG_CRANCHECK 		 = $(basename $(basename $(PKG_TGZ))).crancheck
DRAT_SENTINEL   	 = .drat_$(PKG_TGZ)

# Specify the directory holding R binaries. To use an alternate R build (say a
# pre-prelease version) use `make RBIN=/path/to/other/R/` or `export RBIN=...`
# If no alternate bin folder is specified, the default is to use the folder
# containing the first instance of R on the PATH.
RBIN 							?= $(shell dirname "`which R`")
R         				 = $(RBIN)/R
RSCRIPT   				 = $(RBIN)/Rscript
#R_FLAGS 					?= --vanilla --verbose -q
#R_FLAGS 					?= --vanilla -q
R_FLAGS 					?= -q --no-save --no-restore --no-init-file

# packages I need to test this one
TEST_DEPS  				 = testthat roxygen2 knitr TTR quantmod MASS \
										 sandwich xtable matrixcalc LambertW sadists xts timeSeries
INSTALLED_DEPS 		 = $(patsubst %,$(LOCAL)/%/DESCRIPTION,$(TEST_DEPS)) 
PKG_TESTR 				 = tests/run-all.R

# see http://stackoverflow.com/a/7531247/164611
null  						:=
space 						:= $(null) #
comma 						:= ,
# turn space list to comma list:
COMMA_IT 					 = $(subst $(space),$(comma),$(strip $(1)))

TEST_DEPS_LIST  	 = $(call COMMA_IT,$(TEST_DEPS))

RD_DUMMY 					 = man/$(PKG_NAME).Rd

# do not distribute these!
NODIST_R_DIR			 = nodist
NODIST_FILES			 = ./Makefile $(M4_FILES) .gitignore .gitattributes 
NODIST_FILES			+= rebuildTags.sh .tags .R_tags 
NODIST_FILES			+= Makefile
#NODIST_FILES			+= README.md 
NODIST_DIRS				 = .git man-roxygen m4 $(NODIST_R_DIR)
NODIST_DIRS				+= $(VIGNETTE_D)/figure 

# extradata
EXTDATA_D 				 = inst/extdata
PREMAKE_R 				 = $(wildcard ./$(NODIST_R_DIR)/make_*.R)
PREMAKE_RDA 			 = $(patsubst ./$(NODIST_R_DIR)/make_%.R,$(EXTDATA_D)/%.rda,$(PREMAKE_R))
EXTDATA_FILES	 		 = $(PREMAKE_RDA)

# vignette stuff
VIGNETTE_D 				 = vignettes
VIGNETTE_CACHE 		 = $(VIGNETTE_D)/cache
VIGNETTE_EXTRAS		 = $(VIGNETTE_D)/SharpeRatio.Rnw $(VIGNETTE_D)/SharpeR.sty
# slight of hand here ... 
# VIGNETTE_EXTRAS		+= $(VIGNETTE_D)/AsymptoticMarkowitz.Rnw
NODIST_FILES       += $(VIGNETTE_D)/AsymptoticMarkowitz.Rnw
VIGNETTE_SRCS  		 = $(VIGNETTE_D)/$(PKG_NAME).Rnw $(VIGNETTE_D)/$(PKG_NAME).bib
VIGNETTE_PDF   		 = $(VIGNETTE_D)/$(PKG_NAME).pdf
VIGNETTE_HTML  		 = $(VIGNETTE_D)/index.html
VIGNETTE_CACHE_SENTINEL = $(VIGNETTE_CACHE)/__$(PKG_NAME).etc


SUPPORT_FILES 		 = ./DESCRIPTION ./NAMESPACE ./ChangeLog $(RD_DUMMY) ./inst/CITATION

# for building the package.tgz
#BUILD_FLAGS 			?= --compact-vignettes
BUILD_FLAGS 			?= --compact-vignettes="gs+qpdf" --resave-data=best
BUILD_ENV 				 = R_QPDF=$(R_QPDF) R_GSCMD=$(R_GSCMD) \
									 GS_QUALITY=$(GS_QUALITY)

NODIST_FILES 			+= $(VIGNETTE_PDF) $(VIGNETTE_HTML)
SUPPORT_FILES 		+= $(VIGNETTE_SRCS) $(VIGNETTE_EXTRAS)
EXTRA_PKG_DEPS 		 = 
#EXTRA_PKG_DEPS 	+= $(VIGNETTE_CACHE_SENTINEL)

EXTRA_PKG_DEPS 		+= $(EXTDATA_FILES)

#INSTALL_FLAGS 		?= --preclean --no-multiarch --library=$(LOCAL) 
INSTALL_FLAGS 		?= --preclean --library=$(LOCAL) 

TEST_PRAGMA     	?= release

GIT_BRANCH 				?= master
#GIT_BRANCH 				?= dev1402

# for R CMD build
ifeq ($(TEST_PRAGMA),thorough)
	# noop
else 
	SLOW_TESTS 			 = $(wildcard inst/tests/test-slow*.r)
	NODIST_FILES 		+= $(SLOW_TESTS)
endif

define \n


endef

fooz :
	echo $(PREMAKE_RDA)

STAGING 				?= .staging
STAGED_PKG 			 = $(STAGING)/$(PKG_NAME)

# latex bother. bleah.
#TEXINPADD    = .:$(HOME)/sys/etc/tex:$(HOME)/sys/etc/tex/SEPtex:$(HOME)/work/math/TEX
TEXINPADD    = .:./$(VIGNETTE_D)
PRETEX       = TEXINPUTS=$(TEXINPADD):$$TEXINPUTS
PREBIB       = BSTINPUTS=$(TEXINPADD):$$BSTINPUTS \
               BIBINPUTS=$(TEXINPADD):$$BIBINPUTS 
BIBTEX      := $(shell which bibtex)
LATEX       := $(shell which latex)

BASE_DEF_PACKAGES   = "utils,graphics,grDevices,methods,stats,$(PKG_NAME)"

#FAST_

#########################################################################
# MACROS
#########################################################################

# install locally
INSTALLPKG = $(RLOCAL) -e "install.packages('$(1)', repos = 'http://cran.cnr.Berkeley.edu')" 
	
# make a directory
MKDIR = mkdir -p $(1)

# warn new deps
#WARN_DEPS = $(warning newer deps are $(?))
WARN_DEPS = $(warning will build $@ ; newer deps are $(?))

#########################################################################
# TARGETS
#########################################################################

# these are phony targets
.PHONY: help tags all 
.PHONY: gitpull gitpush dratit staged 
.PHONY: news rdfiles docs build install testthat tests 
.PHONY: staging_d local_d 
.PHONY: clean realclean 
.PHONY: vignette_cache 
.PHONY: the_vignette 
.PHONY: static_vignette 
.PHONY: the_paper 
.PHONY: R

help:  ## generate this help message
	@echo "\nTasks for $(PKG_NAME)\n"
	@echo "Usage: \`make <task>\` where <task> is one of:"
	@echo ""
	@echo "Development Tasks"
	@echo "-----------------"
	@echo "  tags       Build the ctags, for dev purposes"
	@echo "  deps       Install dependencies for package development"
	@echo "  rdfiles    Invoke roxygen to generate Rd files in man/"
	@echo "  docs       Generate Rd files and more"
	@echo "  testthat   Run unit tests."
	@echo '  tests       "   "     "   '
	@echo "  staged     Create a staging version of this package."
	@echo "  build      Make docs and then R CMD build the package.tgz"
	@echo "  install    Make build and then install the result."
	@echo "  R          Make install, then invoke R in the local context w/ the package."
	@echo "  vignette_cache    fuck. this is broken. 2FIX"
	@echo "  the_vignette   Build the vignette in the local context."
	@echo "  clean      Do some cleanup."
	@echo "  realclean  Do lots of cleanup."
	@echo "  codecov    Code coverage, uploaded to codecov.io."
	@echo "  shinecov   Code coverage in local shiny window."
	@echo "  suggestions whee."
	@echo ""
	@echo "  subadvice  CRAN submission advice."
	@echo "  submit     Submit to CRAN."
	@echo ""
	@echo "Packaging Tasks"
	@echo "---------------"
	@echo "  check      Make build, then R CMD check the package as CRAN."
	@echo "  gitpush    Yes, I am lazy"
	@echo "  dratit     Make build, then upload package to my drat repo."
	@echo "  tag        suggestions for tagging a release."
	@echo ""
	@echo "Using R in: $(RBIN)"
	@echo "Set the RBIN environment variable to change this."
	@echo ""
	@grep -E '^[a-zA-Z_-]+\s*:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
	@echo ""
	@grep -P '^(([^\s]+\s+)*([^\s]+))\s*:.*?##\s*.*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

# dev stuff
~/.ctags :
	@-echo -E '--langdef=R' >> $@
	@-echo -E '--langmap=R:.s.S.R.r.q' >> $@
	@-echo -E '--regex-R=/^[ \t]+"?([.A-Za-z][.A-Za-z0-9_]*)"?[\t]*<-[\t]*function/\1/' >> $@
	@-echo -E '--regex-R=/^"?([.A-Za-z][.A-Za-z0-9_]*)"?[ \t]*<-/\1/' >> $@

.R_tags: $(R_FILES)
	./rebuildTags.sh

tags: .R_tags  ## make ctags, used for development.

.Renviron : 
	echo "R_LIBS=$(LOCAL)" >> $@

# if you use emacs (shudder)
TAGS: 
	$(R) --slave CMD rtags

% : m4/%.m4 Makefile
	m4 -I ./m4 -DVERSION=$(VERSION) -DDATE=$(TODAY) -DPKG_NAME=$(PKG_NAME) $< > $@

%.md : %.Rmd 
	$(call WARN_DEPS)
	$(call MKDIR,$(EXTDATA_D))
	R_LIBS=$(LOCAL) R_PROFILE=load.R \
				 R_DEFAULT_PACKAGES="$(BASE_DEF_PACKAGES),knitr,quantmod" \
				 $(R) $(R_FLAGS) --slave -e \
				 "setwd(dirname('$@'));knitr::knit(basename('$<'));"

README.md : $(NODIST_R_DIR)/README.md   ## knit the readme.
	mv $< $@
	rsync -av --delete $(NODIST_R_DIR)/github_extra/figure/ ./github_extra/figure/

# macro for local R
R_LOCALLY  						= R_LIBS=$(LOCAL) $(R) $(R_FLAGS)
R_TIME_LOCALLY  			= R_LIBS=$(LOCAL) $(BIN_TIME) --verbose $(R) $(R_FLAGS)

# make directories
local_d :
	$(call MKDIR,$(LOCAL))

staging_d :
	$(call MKDIR,$(STAGING))

# debugging
echo :
	@-echo $(R_FILES)

# install an R package in the 'LOCAL' directory.
$(LOCAL)/%/DESCRIPTION : 
	$(call MKDIR,$(LOCAL))
	$(R_LOCALLY) -e "install.packages('$*', repos = 'http://cran.cnr.Berkeley.edu')" 

deps: $(INSTALLED_DEPS)

# roxygen it.
man/$(PKG_NAME).Rd NAMESPACE: $(R_FILES)
	$(call WARN_DEPS)
	$(R_LOCALLY) --slave -e "require(roxygen2); roxygenize('.', clean=TRUE)"
	touch $@

rdfiles : DESCRIPTION man/$(PKG_NAME).Rd  ## roxygenize the R files to Rd files.

docs: README.md DESCRIPTION man/$(PKG_NAME).Rd ## build all documentation.

#RSYNC_FLAGS     = -av
#RSYNC_FLAGS     = -vrlpgoD --delete
RSYNC_FLAGS     = -av --delete 

# a parallel version of this package, but without the support structure
$(STAGED_PKG)/DESCRIPTION : $(R_FILES) $(SUPPORT_FILES) $(EXTRA_PKG_DEPS)
	$(call WARN_DEPS)
	@-echo clean up first
	@-rm -rf $(STAGED_PKG)
	$(call MKDIR,$(STAGED_PKG))
	@-echo sync over
	rsync $(RSYNC_FLAGS) \
  --include=man/ --include=man/* \
  --include=NAMESPACE --include=DESCRIPTION \
  --include=$(EXTDATA_D)/ \
  --exclude-from=.gitignore \
 $(patsubst %, % \${\n},$(patsubst %,--exclude=%,$(NODIST_FILES)))  --exclude=$(LOCAL) \
 $(patsubst %, % \${\n},$(patsubst %,--exclude=%,$(NODIST_DIRS)))  --exclude=$(basename $(STAGING)) \
  --exclude=$(RCHECK) \
  . $(@D)
	touch $@

staged : $(STAGED_PKG)/DESCRIPTION 

# make the 'package', which is a tar.gz
$(PKG_TGZ) : $(STAGED_PKG)/DESCRIPTION $(INSTALLED_DEPS) $(EXTRA_PKG_DEPS) 
	$(call WARN_DEPS)
	# check values
	@$(BUILD_ENV) $(R_LOCALLY) --slave -e 'print(Sys.getenv("R_QPDF"));print(Sys.getenv("R_GSCMD"));print(Sys.getenv("GS_QUALITY"));'
	$(BUILD_ENV) $(R_LOCALLY) CMD build $(BUILD_FLAGS) $(<D)

#package : $(PKG_TGZ)

.docker_img : docker/Dockerfile
	$(DOCKER) build --rm -t $(USER)/$(PKG_LCNAME)-crancheck docker

%.crancheck : %.tar.gz .docker_img
	$(DOCKER) run -it --rm --volume $(PWD):/srv:ro $(USER)/$(PKG_LCNAME)-crancheck $< > $@

build : $(PKG_TGZ) ## build the package

build_list : $(PKG_TGZ) ## list the entries in the package
	tar -tzvf $<

# an 'install'
$(LOCAL)/$(PKG_NAME)/INDEX : $(PKG_TGZ) 
	$(call WARN_DEPS)
	$(call MKDIR,$(LOCAL))
	$(R_LOCALLY) CMD INSTALL $(INSTALL_FLAGS) $<
	touch $@

install: $(LOCAL)/$(PKG_NAME)/INDEX ## install the package locally.

# rely on the 'install' target above.
$(LOCAL)/doc/$(PKG_NAME).pdf : $(LOCAL)/$(PKG_NAME)/INDEX

# check and install
$(RCHECK_SENTINEL) : $(PKG_TGZ)
	$(call WARN_DEPS)
	$(R_TIME_LOCALLY) CMD check --as-cran --timings $^ 

#$(R_LOCALLY) CMD check --as-cran --outdir=$(RCHECK) $^ 
	
oldcheck: $(RCHECK_SENTINEL)

check: $(PKG_CRANCHECK) ## check the package as CRAN.

checksee : $(RCHECK_SENTINEL)
	okular $(RCHECK)/$(PKG_NAME)-manual.pdf

$(DRAT_SENTINEL) : $(PKG_TGZ)
	$(call WARN_DEPS)
	$(R) --slave -e "drat:::insertPackage('$<',repodir='~/github/drat',commit=TRUE)"

dratit : tag $(DRAT_SENTINEL) ## push to my drat store.

#$(RCHECK)/$(PKG_NAME)/doc/$(PKG_NAME).pdf : $(VIGNETTE_SRCS) $(RCHECK_SENTINEL)

#slow_vignette : $(RCHECK)/$(PKG_NAME)/doc/$(PKG_NAME).pdf

################################
# UNIT TESTING
################################

#$(R_LOCALLY) --slave -e "if (require(testthat) && require($(PKG_NAME))) testthat::test_dir('./inst/tests')" | tee $@

# 2FIX:
unit_test.log : $(LOCAL)/$(PKG_NAME)/INDEX $(LOCAL)/testthat/DESCRIPTION $(PKG_TESTR)
	$(call WARN_DEPS)
	R_LIBS=$(LOCAL) R_PROFILE=load.R \
				 R_DEFAULT_PACKAGES=$(BASE_DEF_PACKAGES) $(R) $(R_FLAGS) \
				 --slave < $(PKG_TESTR) | tee $@

testthat : unit_test.log

tests    : unit_test.log

# drop into R shell in the 'local context'
R : deps $(LOCAL)/$(PKG_NAME)/INDEX ## drop into R shell in the local context with the installed package
	R_LIBS=$(LOCAL) R_PROFILE=load.R \
				 R_DEFAULT_PACKAGES=$(BASE_DEF_PACKAGES) $(R) -q --no-save

cheapR : 
	R_LIBS=$(LOCAL) R_PROFILE=load.R \
				 R_DEFAULT_PACKAGES=$(BASE_DEF_PACKAGES) $(R) -q --no-save

$(PKG_NAME).pdf: $(VIGNETTE_SRCS) deps $(LOCAL)/$(PKG_NAME)/INDEX 
	$(PRETEX) R_LIBS=$(LOCAL) R_PROFILE=load.R \
				 R_DEFAULT_PACKAGES="$(BASE_DEF_PACKAGES),knitr,TTR" \
				 $(R) $(R_FLAGS) --slave -e "knitr::knit2pdf('$<');"
	if grep Citation $(PKG_NAME).log > /dev/null; then $(PREBIB) $(BIBTEX) $(PKG_NAME); \
		$(PRETEX) "$(R)" CMD pdflatex $(PKG_NAME).tex; fi
	if grep Rerun $(PKG_NAME).log > /dev/null; then $(PRETEX) "$(R)" CMD pdflatex $(PKG_NAME).tex; fi

$(PKG_NAME)_fast.pdf : $(VIGNETTE_SRCS) 
	$(PRETEX) R_LIBS=$(LOCAL) R_PROFILE=load.R \
				 R_DEFAULT_PACKAGES="$(BASE_DEF_PACKAGES),knitr,TTR" \
				 $(R) $(R_FLAGS) --slave -e "knitr::knit2pdf('$<');"
	if grep Citation $(PKG_NAME).log > /dev/null; then $(PREBIB) $(BIBTEX) $(PKG_NAME); \
		$(PRETEX) "$(R)" CMD pdflatex $(PKG_NAME).tex; fi
	if grep Rerun $(PKG_NAME).log > /dev/null; then $(PRETEX) "$(R)" CMD pdflatex $(PKG_NAME).tex; fi
	mv $(PKG_NAME).pdf $<

fast_vignette: $(PKG_NAME)_fast.pdf

the_vignette: $(PKG_NAME).pdf

$(VIGNETTE_CACHE_SENTINEL) : $(VIGNETTE_SRCS) $(LOCAL)/$(PKG_NAME)/INDEX
	$(call WARN_DEPS)
	$(call MKDIR,$(VIGNETTE_CACHE))
	$(PRETEX) R_LIBS=$(LOCAL) R_PROFILE=load.R \
				 R_DEFAULT_PACKAGES="$(BASE_DEF_PACKAGES),knitr,TTR" \
				 FORCE_RECOMPUTE='TRUE' \
				 $(R) $(R_FLAGS) --slave -e "setwd('$(VIGNETTE_D)');knitr::knit(basename('$<'));"
	touch $@

vignette_cache : $(VIGNETTE_CACHE_SENTINEL)

%.tex : %.Rnw 
	$(call WARN_DEPS)
	$(PRETEX) R_LIBS=$(LOCAL) R_PROFILE=load.R \
				 R_DEFAULT_PACKAGES="$(BASE_DEF_PACKAGES),knitr,TTR" \
				 FORCE_RECOMPUTE='TRUE' \
				 $(R) $(R_FLAGS) --slave -e "setwd('$(VIGNETTE_D)');knitr::knit(basename('$<'));"

%.dvi : %.tex 
		$(PRETEX) $(LATEX) $<
		if grep Citation $*.log > /dev/null; then $(PREBIB) $(BIBTEX) $*; $(PRETEX) $(LATEX) $*; fi
		if grep Rerun $*.log > /dev/null; then $(PRETEX) $(LATEX) $*; fi
		@-cp $*.dvi $(VIGNETTE_D)
		@-cp $*.aux $(VIGNETTE_D)
		@-cp $*.log $(VIGNETTE_D)

%.bbl : %.bib
		$(PREBIB) $(BIBTEX) $*
		@-cp $*.bbl $(VIGNETTE_D)

the_paper : vignettes/AsymptoticMarkowitz.dvi vignettes/AsymptoticMarkowitz.tex vignettes/AsymptoticMarkowitz.bbl

# make data needed by the vignette. what bother.
$(EXTDATA_D)/%.rda : $(NODIST_R_DIR)/make_%.R
	$(call WARN_DEPS)
	$(call MKDIR,$(EXTDATA_D))
	R_LIBS=$(LOCAL) R_PROFILE=load.R \
				 R_DEFAULT_PACKAGES="$(BASE_DEF_PACKAGES),knitr,quantmod" \
				 $(R) $(R_FLAGS) --slave -e \
				 "setwd('$(NODIST_R_DIR)');source(basename('$<'));"
	# horribly hacky!
	mv $(NODIST_R_DIR)/*.rda $(EXTDATA_D)

$(NODIST_R_DIR)/sp500_data.csv : 
	curl -o $@ 'https://raw.githubusercontent.com/datasets/s-and-p-500/master/data/data.csv'

$(VIGNETTE_D)/rauto.bib : $(NODIST_R_DIR)/gen_bib.R
	$(call WARN_DEPS)
	$(call MKDIR,$(EXTDATA_D))
	R_LIBS=$(LOCAL) R_PROFILE=load.R \
				 R_DEFAULT_PACKAGES="$(BASE_DEF_PACKAGES),knitr,quantmod" \
				 $(R) $(R_FLAGS) --slave -e \
				 "setwd('$(NODIST_R_DIR)');source(basename('$<'));"
	# horribly hacky!
	mv $(NODIST_R_DIR)/*.bib $@

cachedata: $(EXTDATA_FILES)

newbuild :
	$(MAKE) clean
	$(MAKE) cachedata
	$(MAKE) docs
	$(MAKE) tags
	$(MAKE) build

shinecov : ## Code coverage in local shiny window.
	Rscript -e 'library(covr);shine(package_coverage())'

# http://krisjordan.com/essays/encrypting-with-rsa-key-pairs
.codecov_token :
	echo "export CODECOV_TOKEN='$$CODECOV_TOKEN'" | openssl rsautl -encrypt -pubin -inkey ~/.ssh/id_rsa.pub.pem > $@

token : .codecov_token
	< $< openssl rsautl -decrypt -inkey ~/.ssh/id_rsa | tee $@

codecov : token ## Code coverage, uploaded to codecov.io.
	. token && Rscript -e 'library(covr);codecov()'

# Python. well, iPython.

%.tex : %.ipynb
		ipython nbconvert --to latex --output $* $<

################################
# CLEAN UP 
################################

texclean :
	-rm -rf $(PKG_NAME).log
	-rm -rf $(PKG_NAME).aux
	-rm -rf $(PKG_NAME).out
	-rm -rf $(PKG_NAME).bbl
	-rm -rf $(PKG_NAME).blg

clean : texclean ## clean a bunch of stuff up.
	-rm DESCRIPTION
	-rm -rf man/*.Rd
	-rm -rf $(STAGED_PKG)
	-rm -rf $(RCHECK)
	-rm -rf $(PKG_NAME).tex
	-rm -rf $(PKG_NAME).pdf

realclean : clean
	-rm -rf $(LOCAL)
	-rm -rf $(STAGING)
	-rm -rf ./cache
	-rm -rf convoluted_build.sh 

################################
# git FOO 
################################

gitpush :
	git push origin $(GIT_BRANCH)

gitpull :
	git pull origin $(GIT_BRANCH)

tag : ## create a git tag corresponding to the package version.
	@-echo "git tag -a r$(VERSION) -m 'release $(VERSION)'"
	@-echo "git push --tags"

################################
# CRAN SUBMISSION
################################

# FTP junk
~/.netrc :
	echo -e "machine cran.r-project.org login anonymous password anonymous macdef init\ncd incoming\n\n" > $@

.cran_upload : $(PKG_TGZ)
	@-read -p 'really upload? [y/n] ' -n 1 yorn ; \
	[[ "$$yorn" == "y" ]] && echo -e "user anonymous anonymous\nbinary\ncd incoming\nput $(PKG_TGZ)\nls\nbye\n" | ftp -n -v cran.r-project.org

.send_email : 
	@-read -p 'really send email? [y/n] ' -n 1 yorn ; \
	[[ "$$yorn" == "y" ]] && echo "automatic message" | mail -s "CRAN submission $(PKG_NAME) $(VERSION)" CRAN@R-project.org

submit : .cran_upload .send_email ## submit to CRAN via ftp upload and email?

subadvice : ## CRAN submission advice, echoed.
	@-echo -e "upload $(PKG_TGZ) to cran.r-project.org/incoming via anonymous ftp"
	@-echo -e "then email CRAN@R-project.org w/ subject 'CRAN submission $(PKG_NAME) $(VERSION)'"

#vignette:
#cd $(VIGNETTE_D);\
#$(R) CMD Sweave $(PKG_NAME).Rnw;\
#texi2dvi --pdf $(PKG_NAME).tex;\
#$(R) --vanilla --slave -e "tools:::compactPDF(getwd(), gs_quality='printer')"

mactex : 
	sudo port install -v \
		texlive texlive-basic texlive-bibtex-extra texlive-bin texlive-bin-extra \
		texlive-common texlive-fonts-extra texlive-fonts-recommended \
		texlive-fontutils texlive-formats-extra texlive-generic-extra \
		texlive-generic-recommended texlive-latex texlive-latex-extra \
		texlive-latex-recommended texlive-math-extra 

# cheesy checkin:
#
# sleep `jot -r 1 5 55` && git commit -a -m 'working on vignette'
# git push origin dev1311

suggestions : 
	@-echo 'sleep `jot -r 1 2 57` && git commit -a -m "working on vignette"'
	@-echo "git push origin $(GIT_BRANCH)"

# for submission to arxiv;
# what bother;
#
# I basically did
# make vignettes/AsymptoticMarkowitz.tex
# cd vignettes/
# make -f ~/sys/etc/MOAMakefile AsymptoticMarkowitz.dvi 
# and then used the .tex and .bbl files
arxiv : vignettes/AsymptoticMarkowitz.tex
	@-echo 'cd vignettes/'
	@-echo 'make -f ~/sys/etc/MOAMakefile AsymptoticMarkowitz.dvi'

#for vim modeline: (do not edit)
# vim:ts=2:sw=2:tw=129:fdm=marker:fmr=FOLDUP,UNFOLD:cms=#%s:tags=tags;:syntax=make:filetype=make:ai:si:cin:nu:fo=croqt:cino=p0t0c5(0:
