BIN_OUTPUT_PATH=./bin
SERVER_NAME=hx-app-server

BUILD_BIN_OUTPUT_PATH=${BIN_OUTPUT_PATH}/${SERVER_NAME}

VERSION_FILE:=./VERSION
VERSION = $(shell cat ${VERSION_FILE})
GO_LIB_VERSION = $(shell grep 'git.cestong.com.cn/cecf/cecf-golib v' go.mod | cut -d' ' -f2)

updateGoLibVersion:
	sed -i.bak "s/goLibVersion\s*=\s*\".*\"/goLibVersion=\"${GO_LIB_VERSION}\"/g" cmd/server/version.go
	rm -f cmd/server/version.go.bak

updateVersion:
	sed -i.bak "s/version\s*=\s*\".*\"/version=\"${VERSION}\"/g" cmd/server/version.go
	rm -f cmd/server/version.go.bak

updateVersions:updateGoLibVersion updateVersion
	echo ${VERSION}

build-dev: updateVersions
	go mod tidy
	go build -o ${BUILD_BIN_OUTPUT_PATH} git.cestong.com.cn/cecf/hx-mobile-server-api/cmd/server

build-prod: updateVersions
	go mod tidy
	CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -a -installsuffix cgo -o bin/hx-app-server git.cestong.com.cn/cecf/hx-mobile-server-api/cmd/server

run-dev: build-dev
	${BUILD_BIN_OUTPUT_PATH} -c conf/config.yml

doc-gen:
	swag init  -d cmd/server/

doc-serve: doc-gen
	swagger serve --host 0.0.0.0 --port 6209 --no-open docs/swagger.json

CLI_GEN_DIR=./client
JS_CLI_DIR=${CLI_GEN_DIR}/js
GO_CLI_DIR=${CLI_GEN_DIR}/go-client

js-client-clean:
	rm -rf ${JS_CLI_DIR}

js-client-gen: doc-gen js-client-clean
	java -jar ~/install/swagger-codegen-cli.jar generate -i docs/swagger.yaml -l javascript -o ${JS_CLI_DIR}

go-client-clean:
	rm -rf ${GO_CLI_DIR}

go-client-gen: doc-gen go-client-clean
	java -jar ~/install/swagger-codegen-cli.jar generate -i docs/swagger.yaml -l go -o ${GO_CLI_DIR}


test-api:
	go test -v ./test/api

DOCKER_IMAGE_NAME=observe-front
DOCKER_REMOTE_IMAGE_NAME=reg.cestong.com.cn/cecf/${DOCKER_IMAGE_NAME}
#DOCKER_REMOTE_IMAGE_NAME=pujielan/${DOCKER_IMAGE_NAME}

docker-build:
	npm run build:prod
	git rev-parse --short HEAD > dist/version
	docker build . -t ${DOCKER_IMAGE_NAME}

docker-push: docker-build
	docker tag ${DOCKER_IMAGE_NAME} ${DOCKER_REMOTE_IMAGE_NAME}
	docker push ${DOCKER_REMOTE_IMAGE_NAME}
	ssh cest1 kubectl rollout restart deployment obf -n observe

build-ui:
	cd obui && npm run build && git rev-parse --short HEAD > dist/version

build-front:
	npm run build:prod
	git rev-parse --short HEAD > dist/version

docker-push-all:
	docker build . -t ${DOCKER_IMAGE_NAME} -f Dockerfile-with-obui
	docker tag ${DOCKER_IMAGE_NAME} ${DOCKER_REMOTE_IMAGE_NAME}
	docker push ${DOCKER_REMOTE_IMAGE_NAME}

pull:
	git pull
	cd obui && git pull

init-obui:
	git clone http://git.cestong.com.cn/cecf/observe-front-ui.git obui


deploy: docker-push
	ssh km1 'kubectl rollout restart deployment obf -n observe'

deploy-test: docker-push
	ssh cest-2 'cd hx-app-server && docker-compose pull && docker-compose up -d'

docker-build-php:
	docker build -t registry.cestong.com:8150/zhixueyun/hx-php-server -f ./deploy/php/Dockerfile .

docker-push-php: docker-build-php
	docker push registry.cestong.com:8150/zhixueyun/hx-php-server

docker-build-nginx:
	docker build -t registry.cestong.com:8150/zhixueyun/hx-nginx -f ./deploy/nginx/Dockerfile .

db-backup-file = ~/data/test_ctc_backup_$(shell date '+%Y-%m-%dT%H-%M').sql
db-backup-test:
	ssh cest-2 'cd data && mysqldump -h 172.17.172.137 -u root -p1qaz2wsx3edc -P 53306 --databases ctc > dump.sql'
	scp cest-2:~/data/dump.sql ${db-backup-file}

db-sync-to-local: db-backup-test
	mysql -u root -p1234 -h 127.0.0.1 < ${db-backup-file}

release-tag:
	git tag "v${VERSION}"
	git push --tags
