# Define variables
# 默认变量为空，实际值将在 checkpackagetype 后读取
PACKAGE_TYPE :=

# 检查并生成 .package_type 文件
checkpackagetype:
	@if [ ! -f .package_type ]; then \
	    echo "The file '.package_type' does not exist. Please specify the package type (public/protected):"; \
	    while true; do \
	        read answer; \
	        if [ "$$answer" = "public" ] || [ "$$answer" = "protected" ]; then \
	            echo $$answer > .package_type; \
	            break; \
	        else \
	            echo "Invalid input. Please enter 'public' or 'protected':"; \
	        fi; \
	    done; \
	fi

PACKAGE_TYPE=$(shell [ -f .package_type ] && cat .package_type || echo "")
PACKAGE_NAME=$(basename "${PWD}")
# automate package building and updating the dran repository
PACKAGE_DIR=$(PWD)
PACKAGE_NAME=$(notdir $(PACKAGE_DIR))
DRAN_DIR="../dran"
CONTRIB_DIR="$(DRAN_DIR)/$(PACKAGE_TYPE)/src/contrib"
DRAN_REMOTE="dwuser@drwater.net:/home/www/"
# Define Git commands
GIT_ADD = git add .
GIT_COMMIT = git commit -m
GIT_PUSH = git push origin main
GIT_PUSH_GITHUB = git push github main
# Define R Build command
RBUILD = R CMD build $(PACKAGE_DIR)
# upload install_drwateR.R
FILE=R/install_drwateR.R
DEST_PATH="dwuser@drwater.net:/home/www/ftp/drwateR/"

# Define variables
upd_install_drwateR:
	@if git diff --quiet HEAD -- $(FILE); then \
		echo "$(FILE) has not changed."; \
	else \
		echo "$(FILE) has changed. Uploading..."; \
		scp $(FILE) $(DEST_PATH); \
	fi

# 自动化任务
.PHONY: all document bump-version install commit push build check 

# 自动生成文档
document:
	Rscript -e "devtools::document()"

git-cm-document:
	@if git diff-index --quiet HEAD --; then \
		echo "No changes to commit."; \
	else \
		$(GIT_ADD); \
		$(GIT_COMMIT) "Updated documentation"; \
		$(GIT_PUSH); \
	fi


# 自动递增版本号（默认增加补丁版本号）
bump-version:
	Rscript -e "usethis::use_version(which = 'patch')"

# 安装包
install:
	Rscript -e "devtools::install()"

git-cm-install:
	@if git diff-index --quiet HEAD --; then \
		echo "No changes to commit."; \
	else \
		$(GIT_ADD); \
		$(GIT_COMMIT) "Compile"; \
		$(GIT_PUSH); \
	fi

# 构建包
build:
	$(RBUILD)

# Update the custom DRAN repository
update_repo: checkpackagetype
	@mkdir -p $(CONTRIB_DIR); \
	if [ -d $(CONTRIB_DIR) ]; then \
	    echo "Building the package..."; \
	    $(RBUILD); \
	    mv $(PACKAGE_NAME)_*.tar.gz $(CONTRIB_DIR); \
	    echo "Updating repository index..."; \
	    Rscript -e "tools::write_PACKAGES('$(CONTRIB_DIR)', fields = 'Remotes', type = 'source')"; \
	fi

# Sync the repository to the remote server
sync_repo: checkpackagetype
	@if [ -e $(CONTRIB_DIR) ]; then \
		rsync -avz $(DRAN_DIR) $(DRAN_REMOTE); \
	fi

# Makefile for generating the CRAN index HTML page
index.html:
	Rscript -e "rmarkdown::render('../dran/index.qmd', output_file='../dran/index.html')"

# 运行 R CMD check
check:
	R CMD check {} ${PACKAGE_NAME}_*.tar.gz

# Commit changes to Git
commit: upd_install_drwateR 
	$(GIT_ADD)
	@if git diff-index --quiet HEAD --; then \
		echo "No changes to commit."; \
	else \
		git status; \
		read -p "Enter commit message: " msg; \
		git add .; \
		git commit -m "$$msg"; \
		git push; \
	fi

# Push changes to remote repository
push:
	$(GIT_PUSH)

# Push changes to github repository
github:
	$(GIT_PUSH_GITHUB)

# 全部任务
all: commit document git-cm-document bump-version install git-cm-install repo

# 更新repo
repo: update_repo index.html sync_repo

# 清理生成的 tar.gz 文件
clean:
	rm -f ${PACKAGE_NAME}_*.tar.gz

# usethis use_git use_mit_license use_version
usegit:
	Rscript -e "usethis::use_git()"

useversion:
	Rscript -e "usethis::use_version()"

usemitlicense:
	Rscript -e "usethis::use_mit_license()"

# usethis setup
usethis: usegit useversion


updmakefile:
	@if [ "$(OS)" = "OSX" ] && [ "$(HOSTNAME)" = "max" ]; then \
		echo "基于 $$HOME/bin/drwateR.Makefile 更新本项目 Makefile..."; \
		cp "$$HOME/bin/drwateR.Makefile" "./Makefile"; \
		git add "Makefile" && git commit -m "Update Makefile"; \
		echo "本项目Makefile更新完成."; \
	else \
		echo "Makefile 无需在本系统上更新."; \
	fi

