#	Makefile for an AzimuthRef package

BUILD_FLAGS=
DATA_RAW_DIR = data-raw
data_raw := $(sort $(wildcard ${DATA_RAW_DIR}/*.r ${DATA_RAW_DIR}/*.R))
rtools := $(sort $(wildcard tools/*.R))

PKG_NAME := $(shell grep -i ^package DESCRIPTION | cut -d ":" -d " " -f 2)
PKG_VERSION := $(shell grep -i ^version DESCRIPTION | cut -d ":" -d " " -f 2)

.PHONY: all build clean deps docs resave tools

all: clean build

$(PKG_NAME)_$(PKG_VERSION).tar.gz: data docs
	R CMD build $(BUILD_FLAGS) .

build: $(PKG_NAME)_$(PKG_VERSION).tar.gz

clean:
	#	Remove data files, associated man pages, and the built tarball
	-rm $(addprefix man/, $(addsuffix .Rd, $(basename $(notdir $(wildcard data/*.rda)))))
	-rm -rf data/
	-rm $(PKG_NAME)_$(PKG_VERSION).tar.gz

data:
	#	Build datasets
	$(foreach dr, ${data_raw}, Rscript -e 'source("$(realpath ${dr})", echo = TRUE)';)

deps:
	#	Install package dependencies
	Rscript -e 'if (!require("remotes")) {install.packages("remotes", repos = "http://cran.rstudio.com")}; remotes::install_deps(dependencies = TRUE, upgrade = FALSE)'

desc: data
	#	Update the description with the following metadata:
	#	- set default dataset, if not set
	#	- add additional datasets
	#	- update number of cells in default dataset
	#	- update required version of SeuratObject
	Rscript tools/gen_desc.R

docs: data
	#	Generate documentation for the datasets (and package)
	Rscript -e 'if (!require("roxygen2")) {install.packages("roxygen2", repos = "http://cran.rstudio.com")}; roxygen2::roxygenize()'

resave: data
	# Try to compress datasets beyond the default
	Rscript -e 'if (require("tools")) tools::resaveRdaFiles(paths = "data")'

tools: data
	echo ${rtools}

var:
	Rscript tools/01_resave_data.R
