SHELL=/usr/bin/env bash
date_dir_name = $(shell date +"%Y%m%d-%H%M%S")
# 6.0 or 8.0
dotnet_ver = 6.0
# git分支
# git_branch = 
# git版本号-"分支名称"-"commit id"
git_commit = 1.1.2-`git rev-parse --abbrev-ref HEAD | awk '{print $1}'`-`git rev-parse --short HEAD | awk '{print $1}'`

#测试环境变量
test:
	echo git_commit:$(git_commit)
	echo dotnet_ver:$(dotnet_ver)

# 编译Debug版本
build-debug:
	dotnet build -c Debug /p:InformationalVersion="$(git_commit)"


# 编译Release版本
build-release:
	dotnet build -c Release /p:InformationalVersion="$(git_commit)"

# 发布running-scoring debug版本
publish-running-scoring-debug:
	dotnet clean
	rm -rf ./RunningScoring/bin/Release/net$(dotnet_ver)/publish
	dotnet publish -c Debug /p:InformationalVersion="$(git_commit)" -o "./RunningScoring/bin/Release/net$(dotnet_ver)/publish" "./RunningScoring/RunningScoring.csproj"

# 发布running-scoring release版本
publish-running-scoring-release:
	dotnet clean
	rm -rf ./RunningScoring/bin/Release/net$(dotnet_ver)/publish
	dotnet publish -c Release /p:InformationalVersion="$(git_commit)" -o "./RunningScoring/bin/Release/net$(dotnet_ver)/publish" "./RunningScoring/RunningScoring.csproj"

# 安装running-scoring服务
only-install-running-scoring_service:
	# 分发文件
	mkdir -p /disk/ai_sports/data/run_face_pic
	mkdir -p /disk/ai_sports/data/run_output
	mkdir -p /disk/syslog
	mkdir -p /disk/ai_sports/RunningScoring
	mkdir -p /disk/ai_sports/bak//RunningScoring-$(date_dir_name)
	# 备份
	rsync -av --exclude='NLog' --exclude='output' /disk/ai_sports/RunningScoring  /disk/ai_sports/bak/RunningScoring-$(date_dir_name)
	# 复制文件
	rsync -av --exclude='appsettings.json' --exclude='*/' ./RunningScoring/bin/Release/net$(dotnet_ver)/publish/*  /disk/ai_sports/RunningScoring/
	rsync -av ./RunningScoring/bin/Release/net$(dotnet_ver)/publish/runtimes  /disk/ai_sports/RunningScoring/
	rsync -av ./RunningScoring/bin/Release/net$(dotnet_ver)/publish/wwwroot  /disk/ai_sports/RunningScoring/
	# 复制配置文件
	rsync -av --ignore-existing ./Install/ubuntu/RunningScoring/appsettings.json  /disk/ai_sports/RunningScoring/
	rsync -av ./Install/ubuntu/RunningScoring/NLog.config  /disk/ai_sports/RunningScoring/
	# 安装服务
	install -C -m 0644 ./Install/ubuntu/running-scoring.service /etc/systemd/system/running-scoring.service
	systemctl daemon-reload
	@echo
	@echo "running-scoring.service installed. Don't forget to run 'sudo systemctl start running-scoring.service' to start it and 'sudo systemctl enable running-scoring.service' for it to be enabled on startup."

# 安装running-scoring-service调试版服务:
install-running-scoring_service_debug:publish-running-scoring-debug only-install-running-scoring_service

# 安装running-scoring-service:
install-running-scoring_service:publish-running-scoring-release only-install-running-scoring_service
    

# 发布sunny-sports debug版本
publish-sunny-sports-debug:
	dotnet clean
	rm -rf ./SunnySports/bin/Release/net$(dotnet_ver)/publish
	dotnet publish -c Debug /p:InformationalVersion="$(git_commit)" -o "./SunnySports/bin/Release/net$(dotnet_ver)/publish" "./SunnySports/SunnySports.csproj"

# 发布sunny-sports release版本
publish-sunny-sports-release:
	dotnet clean
	rm -rf ./SunnySports/bin/Release/net$(dotnet_ver)/publish
	dotnet publish -c Release /p:InformationalVersion="$(git_commit)" -o "./SunnySports/bin/Release/net$(dotnet_ver)/publish" "./SunnySports/SunnySports.csproj"

# 安装sunny-sports服务
only-install-sunny-sports_service:
	# 分发文件
	mkdir -p /disk/ai_sports/data/sunny_face_pic
	mkdir -p /disk/ai_sports/data/sunny_sports_output
	mkdir -p /disk/syslog
	mkdir -p /disk/ai_sports/SunnySports
	mkdir -p /disk/ai_sports/bak//SunnySports-$(date_dir_name)
	# 备份
	rsync -av --exclude='NLog' --exclude='output' /disk/ai_sports/SunnySports  /disk/ai_sports/bak/SunnySports-$(date_dir_name)
	# 复制文件
	rsync -av --exclude='appsettings.json' --exclude='*/' ./SunnySports/bin/Release/net$(dotnet_ver)/publish/*  /disk/ai_sports/SunnySports/
	rsync -av ./SunnySports/bin/Release/net$(dotnet_ver)/publish/runtimes  /disk/ai_sports/SunnySports/
	rsync -av ./SunnySports/bin/Release/net$(dotnet_ver)/publish/wwwroot  /disk/ai_sports/SunnySports/
	# 复制配置文件
	rsync -av --ignore-existing ./Install/ubuntu/SunnySports/appsettings.json  /disk/ai_sports/SunnySports/
	rsync -av ./Install/ubuntu/SunnySports/NLog.config  /disk/ai_sports/SunnySports/
	# 安装服务
	install -C -m 0644 ./Install/ubuntu/sunny-sports.service /etc/systemd/system/sunny-sports.service
	systemctl daemon-reload
	@echo
	@echo "sunny-sports.service installed. Don't forget to run 'sudo systemctl start sunny-sports.service' to start it and 'sudo systemctl enable sunny-sports.service' for it to be enabled on startup."


# 安装sunny-sports-service调试版服务:
install-sunny-sports_service_debug:publish-sunny-sports-debug only-install-sunny-sports_service

# 安装sunny-sports-service:
install-sunny-sports_service:
	echo "拉取代码" 
	git branch
	git pull
	echo "发布sunny-sports release版本"
	$(MAKE) publish-sunny-sports-release 
	echo "安装sunny-sports服务"
	$(MAKE) only-install-sunny-sports_service
	echo "重启sunny-sports服务"
	systemctl restart sunny-sports.service
	echo "执行结束,发布成功"

	
# 安装sunny-sports-service_debug:
install-sunny-sports_service_debug2:
	echo "拉取代码" 
	git branch
	git pull
	echo "发布sunny-sports debug版本"
	$(MAKE) publish-sunny-sports-debug
	echo "安装sunny-sports服务"
	$(MAKE) only-install-sunny-sports_service
	echo "重启sunny-sports服务"
	systemctl restart sunny-sports.service
	echo "执行结束,发布成功"

###############################
# 长跑发包
# 流程：
#第1步：make publish-running-scoring-release
#第2步：make prepare-running-installer
#第3步：make rsync-running-installer-30002 (实际执行的时候更换具体设备的端口号) 

# 准备长跑安装包
prepare-running-installer:
	# 删除原因的发布文件
	rm -rf ./Install/ubuntu/RunningScoring/publish
	rm -rf ./Install/ubuntu/SunnySports/publish
	# 分发文件
	# RunningScoring
	rsync -av ./RunningScoring/bin/Release/net6.0/publish ./Install/ubuntu/RunningScoring/

rsync-running-installer-30002:
	# 同步到远程
	rsync -avzP -e 'ssh -p 30002' ./Install/ubuntu root@82.156.193.182:/disk/ai_sports/Install/

###############################
# 阳光体育发包
# 流程：
#第1步：make publish-sunny-sports-release
#第2步：make prepare-sunny-sports-installer
#第3步：make rsync-running-installer-30003 (实际执行的时候更换具体设备的端口号) 

# 同步ai_sports安装包
prepare-sunny-sports-installer:
	rm -rf ./Install/ubuntu/RunningScoring/publish
	rm -rf ./Install/ubuntu/SunnySports/publish
	# 分发文件
	# SunnySports
	rsync -av ./SunnySports/bin/Release/net6.0/publish ./Install/ubuntu/SunnySports/

rsync-sunny-sports-installer-30003:
	# 同步到远程
	rsync -avzP -e 'ssh -p 30003' ./Install/ubuntu root@82.156.193.182:/disk/ai_sports/Install/
