#!/bin/bash
#获取git 最后一个 tag 的描述！！！！
#LatestTag=$(git describe --tags `git rev-list --tags --max-count=1`)

#install_dir="流延测厚仪安装包_"$LatestTag

version=$1
if [ -z "$version" ]; then
 echo "format error: version is empty"
 echo "Usage: ./3.get_exe.sh 7.3.0"
 exit
fi

install_dir="流延测厚仪安装包"

# 对 $install_dir_root 重命名为 ${install_dir_root}_v${version}_yyyyMMdd
# date参考 https://www.cnblogs.com/zuiyue_jing/p/12557430.html
suffix=$(date +%Y%m%d)
install_dir=${install_dir}_v${version}_${suffix}

install_dir_root=$install_dir


if [ -e $install_dir ]; then
	rm -rf $install_dir
fi
#复制 install 到 测厚仪安装包
cp -rf install $install_dir

#复制 版本描述
cp 版本描述.md $install_dir

install_dir=$install_dir"/佛山市枫莱尔自动化技术有限公司/windows"

copy_to()
{
    src_dir=$1
    dest_dir=$2
    files=$3

	#检测文件夹是否存在
	#存在，删除全部数据，再新创建；
	#不存在，创建。
	if [ -e $dest_dir ]; then
        rm -rf $dest_dir
    fi

	mkdir -p $dest_dir
	
	echo 复制 $dest_dir

	for f in ${files[*]}; do
		file_path=$src_dir"/"$f
		cp -rf $file_path $dest_dir
		if [ $? != 0 ]; then
			echo 复制失败
			exit 1
		fi
	done
}

copy_to2()
{
    src_dir=$1
	dest_dir=$2
    files=$3

    src_dir=$src_dir"/bin/Debug" 
	dest_dir=$install_dir"/"$dest_dir
    copy_to $src_dir $dest_dir $files
}

src_dir="../Project.FLY.Thick.FilmCasting/FLY.Thick.FilmCasting.UI.Client"
dest_dir="FLY.Thick.FilmCasting.UI.Client"
files=(\
	"FLY.Thick.FilmCasting.UI.Client.exe" \
	"default" "unity" "layout" "music" "update" \
	"paramDict.json" \
	"nlog.config" \
	)
copy_to2 $src_dir $dest_dir $files


src_dir="../Project.FLY.Thick.FilmCasting/FLY.Thick.FilmCasting.UI.Server" 
dest_dir="FLY.Thick.FilmCasting.UI.Server" 
files=(\
	"FLY.Thick.FilmCasting.UI.Server.exe" \
	"Gage1" "x64" "x86" \
	"nlog.config" \
	"FLY.Thick.FilmCasting.WebAPI.xml" "FLY.Thick.FilmCasting.xml" "FLY.Thick.Base.xml" \
	"MISC.xml" "FLY.OBJComponents.xml" \
	)
copy_to2 $src_dir $dest_dir $files

src_dir="../Project.FLY.Thick.FilmCasting/FLY.Thick.FilmCasting.UI.DbViewer" 
dest_dir="FLY.Thick.FilmCasting.UI.DbViewer" 
files=(\
	"FLY.Thick.FilmCasting.UI.DbViewer.exe" \
	"x64" "x86" \
	)
copy_to2 $src_dir $dest_dir $files


# 修改 $install_dir_root/install.json 中的版本号
# 参考 https://www.cnblogs.com/thePeaceOftheLord/p/11107737.html
# 正则表达式 https://www.cnblogs.com/zd520pyx1314/p/6061339.html
sed -i 's/"InstallZipVersion":".*"/"InstallZipVersion":"'${version}'"/g' ${install_dir_root}/install.json

# 复制 $install_dir_root/install.json 到 $install_dir/FLY.Thick.FilmCasting.UI.Client/update
cp ${install_dir_root}/install.json $install_dir/FLY.Thick.FilmCasting.UI.Client/update


fileName7z=${install_dir_root}.7z
if [ -e $fileName7z ]; then
	rm $fileName7z
fi

# 进入安装包内 用7z压缩
cd ${install_dir_root}
"C:\Program Files\7-Zip\7z.exe" a ../${fileName7z} ./*

cd ..

# 修改 filmCastingInstallZipVersionInfo.json
InstallZipVersionInfoFileName="filmCastingInstallZipVersionInfo.json"
sed -i 's/"InstallZipVersion":".*"/"InstallZipVersion":"'${version}'"/g' ${InstallZipVersionInfoFileName}
sed -i 's/"InstallZipUrl":".*"/"InstallZipUrl":"http:\/\/server.flyautomation.net:8889\/download\/'${fileName7z}'"/g' ${InstallZipVersionInfoFileName}
echo 完成