#!/bin/bash
ProjectL=wfc
ProjectU=WFC
RelDate=`date +%Y%m%d`
#GitRootDir=${HOME}/wfc.git
GitRootDir=$(dirname $(dirname $(dirname $(realpath $0))))
BERootDir=${GitRootDir}/be.wfc
FERootDir=${GitRootDir}/fe.wfc
FEUserRootDir=${GitRootDir}/fe.wfc.user
BuildRootDir=${GitRootDir}/build.wfc

WFCWorkDir=/opt/wfc
BuildDockerDir=${BuildRootDir}/build/docker
ReleseDir=${BuildRootDir}/release
SystemdDir=${BuildRootDir}/build/systemd/system

DebBuildDir=${BuildRootDir}/debbuild
DebReleseDir=${ReleseDir}/debs
DebSystemdDir=${DebBuildDir}/etc/systemd/system

TmpDir=${BuildRootDir}/tmp
TmpEnvFile=${TmpDir}/.env.tmp

usage() {
	echo "Usage: sh build.sh [extras|jar|dist|copy|tar|srctar|deb|all] [-g]"
	exit 1
}

SKIP_PRE_GIT_PULL=false

# Reorder arguments so that options come first
ARGS=()
for arg in "$@"; do
    case $arg in
        -g)
            SKIP_PRE_GIT_PULL=true
            ;;
        *)
            ARGS+=("$arg")
            ;;
    esac
done

if SKIP_PRE_GIT_PULL=true; then
	ARGS+=("-g")
fi

# Restore positional parameters
set -- "${ARGS[@]}"

# Process options
while getopts "g" opt; do
    case ${opt} in
        g )
            SKIP_PRE_GIT_PULL=true
            ;;
        \? )
            usage
            ;;
    esac
done
shift $((OPTIND -1))

pre-build-tar(){
	if [ ! -d ${ReleseDir}/tars ]; then
		mkdir -p ${ReleseDir}/tars
	fi
	source ${BuildRootDir}/bin/control.sh
	cd ${BuildRootDir}
	sed -i "s/YYYYMMDD/${RelDate}/" ${BuildDockerDir}/env/wfc-release
	# git update-index --assume-unchanged ${BuildDockerDir}/env/wfc-release	
}

pre-git-pull(){
	cd ${BERootDir}
	git pull
	cd ${FERootDir}
	git checkout src/typings/
	git pull
	cd ${FEUserRootDir}
	git checkout src/typings/	
	git pull
	cd ${BuildRootDir}
	git pull
}

pre-build-deb(){
	if [ ! -d ${ReleseDir}/debs ]; then
		mkdir -p ${ReleseDir}/debs
	fi
	if [ ! -d ${DebBuildDir}/opt/wfc ]; then
		mkdir -p ${DebBuildDir}/opt/wfc
	fi
	if [ ! -d ${DebSystemdDir} ]; then
		mkdir -p ${DebSystemdDir}
	fi
	cd ${BuildRootDir}
	sed -i "s/YYYYMMDD/${RelDate}/" ${BuildDockerDir}/env/wfc-release
	# git update-index --assume-unchanged ${BuildDockerDir}/env/wfc-release
}

build-extras(){
	cd ${BuildRootDir}
	echo -n "Begin build extras file ... "
	cd ${BuildRootDir}/bin
	javac -source 8 -target 8 -bootclasspath /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/rt.jar -extdirs ""  -Xlint:-options printJarVer.java	
	echo "done"
}

build-jar(){
	cd ${BERootDir}
	# git pull
	mvn clean package -Dmaven.test.skip=true -P prod
}

pre-build-src-tar(){
	if [ ! -d ${ReleseDir}/src-tars ]; then
		mkdir -p ${ReleseDir}/src-tars
	fi
	source ${BuildRootDir}/bin/control.sh
}

clean-jar(){
	cd ${BERootDir}
	mvn clean
	cd ${LicenseRootDir}
	mvn clean
}

build-dist(){
	cd ${FERootDir}
	# git checkout src/typings/
	# git pull
	pnpm i
	pnpm build

	cd ${FEUserRootDir}
	# git checkout src/typings/	
	# git pull
	if ! pnpm list terser --depth 0 &> /dev/null; then
		pnpm add terser --save-dev
	fi
	pnpm i
	pnpm build
}

copy-file(){
	${BuildRootDir}/bin/copy.sh ${GitRootDir}
	cp -f ${BuildRootDir}/bin/printJarVer.class ${BuildRootDir}/build/bin/
}

copy-file-debbuild(){
	rm -rf ${DebBuildDir}/opt/wfc/*
	cp -rf ${BuildRootDir}/build/bin/ ${DebBuildDir}/opt/wfc
	cp -rf ${BuildRootDir}/build/docker/ ${DebBuildDir}/opt/wfc
	cp -rf ${BuildRootDir}/build/systemd/ ${DebBuildDir}/opt/wfc
	if [ -f ${DebBuildDir}/opt/wfc/docker/.env ]; then
		rm ${DebBuildDir}/opt/wfc/docker/.env
	fi
	cp -rf ${SystemdDir}/* ${DebSystemdDir}/
    chmod 755 ${DebBuildDir}/DEBIAN/preinst	
    chmod 755 ${DebBuildDir}/DEBIAN/postinst
    chmod 755 ${DebBuildDir}/DEBIAN/postrm	
}

# create tar package
create-tar(){
	TarFileName=${ProjectL}-${RelVersion}-${RelDate}.tar.gz	
	echo -n "Begin build tar package(${TarFileName}) ... "
	cd ${BuildRootDir}/build
	tar cvfz ${ReleseDir}/tars/${TarFileName} \
        --exclude='./docker/mysql/logs' \
        --exclude='./docker/mysql/data' \
        --exclude='./docker/mysql/tmp' \
        --exclude='./docker/nacos/logs' \
        --exclude='./docker/nginx/logs' \
        --exclude='./docker/.env' \
        . 1>/dev/null
	echo "done"
}

# create source tar package
create-src-tar(){
	SrcTarFileName=${ProjectL}-${RelVersion}-${RelDate}.src.tar.gz	
	echo -n "Begin build source tar package(${SrcTarFileName}) ... "
	WFCRelativeDir=./wfc.git
	BeRelativeDir=${WFCRelativeDir}/be.wfc
	FeRelativeDir=${WFCRelativeDir}/fe.wfc
	FeUserRelativeDir=${WFCRelativeDir}/fe.wfc.user
	BuildRelativeDir=${WFCRelativeDir}/build.wfc
	cd ${HOME}
	tar cvfz ${ReleseDir}/src-tars/${SrcTarFileName} \
		--exclude=${WFCRelativeDir}/.vscode \
		--exclude=${BeRelativeDir}/.vscode \
		--exclude=${BeRelativeDir}/.git \
		--exclude=${BeRelativeDir}/bin \
		--exclude=${BeRelativeDir}/docker \
		--exclude=${BeRelativeDir}/.github \
		--exclude=${BeRelativeDir}/tmp \
		--exclude=${FeRelativeDir}/.vscode \
		--exclude=${FeRelativeDir}/.git \
		--exclude=${FeRelativeDir}/dist \
		--exclude=${FeRelativeDir}/node_modules \
		--exclude=${FeUserRelativeDir}/.vscode \
		--exclude=${FeUserRelativeDir}/.git \
		--exclude=${FeUserRelativeDir}/dist \
		--exclude=${FeUserRelativeDir}/node_modules \
		--exclude=${BuildRelativeDir}/.vscode \
		--exclude=${BuildRelativeDir}/.git \
		--exclude=${BuildRelativeDir}/tmp \
		--exclude=${BuildRelativeDir}/release \
		--exclude=${BuildRelativeDir}/build/docker/nginx/html \
		--exclude=${BuildRelativeDir}/build/docker/wfc/auth/jar/*.jar \
		--exclude=${BuildRelativeDir}/build/docker/wfc/gateway/jar/*.jar \
		--exclude=${BuildRelativeDir}/build/docker/wfc/modules/*/jar/*.jar \
		--exclude=${BuildRelativeDir}/build/docker/wfc/visual/jar/*.jar \
		--exclude=${BuildRelativeDir}/debbuild/etc \
		--exclude=${BuildRelativeDir}/debbuild/opt \
		--exclude=${BuildRelativeDir}/bin/cpto205.sh \
		--exclude=${BuildRelativeDir}/docs/99-reference \
		--exclude=${BuildRelativeDir}/docs/100-images \
      ${WFCRelativeDir} 1>/dev/null
	echo "done"
}

# create deb package
create-deb(){
	Version=$(grep '^Version:' ${DebBuildDir}/DEBIAN/control | awk '{print $2}')
	DebPkgFile=${ProjectL}-${Version}-${RelDate}-ub22.deb
	echo -n "Begin build deb package(${DebPkgFile}) ... "
	dpkg -b ${DebBuildDir} ${DebReleseDir}/${DebPkgFile} 1>/dev/null
	echo "done"
}

post-build(){
	# if [ -f ${TmpEnvFile} ] && [ ! -f ${BuildDockerDir}/.env ]; then
	# 	mv -f ${TmpEnvFile} ${BuildDockerDir}/.env
	# fi
	cd ${BuildRootDir}
	sed -i "s/${RelDate}/YYYYMMDD/" ${BuildDockerDir}/env/wfc-release
	# git checkout ${BuildDockerDir}/env/wfc-release
	# git update-index --no-assume-unchanged ${BuildDockerDir}/env/wfc-release
}

case "$1" in
	"extras")
		build-extras
		;;
	"jar")
		build-jar
		;;
	"dist")
		build-dist
		;;
	"copy")
		copy-file
		;;	
	"tar")
        if [ "$SKIP_PRE_GIT_PULL" = false ]; then
            pre-git-pull
        fi	
		pre-build-tar
		build-extras
		build-jar
		build-dist
		copy-file
		create-tar
		post-build
		;;
	"deb")
        if [ "$SKIP_PRE_GIT_PULL" = false ]; then
            pre-git-pull
        fi		
		pre-build-deb
		build-extras
		build-jar
		build-dist
		copy-file
		copy-file-debbuild
		create-deb
		post-build
		;;
	"srctar")
        if [ "$SKIP_PRE_GIT_PULL" = false ]; then
            pre-git-pull
        fi		
		pre-build-src-tar
		clean-jar
		create-src-tar
		# post-build
		;;
	"all")
		# Recursively call the script with the 'deb' argument and pass necessary parameters
		$0 deb "${ARGS[@]:2}"
		# Recursively call the script with the 'tar' argument and pass necessary parameters
		$0 tar "${ARGS[@]:2}"
		;;	
	*)
		usage
		;;
esac
