##############################################
# 分应用补丁解压脚本
# 使用方法
# 1. 将此脚本和分应用补丁放置于补丁仓库目录
# 2. 执行此脚本 ./sepPatchParser.sh
# 3. 执行完毕后可在租户管理中心继续执行升级操作
#
# @Date   2021年5月26日 15:40:36
# @Author Tony Tang
##############################################
pd=$PWD

mkdir -p $pd/temp
rm -rf $pd/temp/*
ALLZIPNAME=`ls -tr *.zip|tail -1`
unzip -o ${ALLZIPNAME} -d $pd/temp
SepPatch_ZIPSNAME=(`ls $pd/temp/*.zip|awk -F '/' '{print $NF}'|awk -F ".zip" '{print $1}'`)


for((i=0;i<${#SepPatch_ZIPSNAME[@]};i++))
do
unzip -o $pd/temp/${SepPatch_ZIPSNAME[$i]}.zip -d  $pd/temp/${SepPatch_ZIPSNAME[$i]}
FORMAT_VERSION=`cat $pd/temp/${SepPatch_ZIPSNAME[$i]}/kdpkgs.xml|grep '<format'|awk -F '"' '{print $2}'`
PRODUCT_NAME=`cat $pd/temp/${SepPatch_ZIPSNAME[$i]}/kdpkgs.xml|grep 'product '|awk -F 'name=' '{print $2}'|awk -F '"' '{print $2}'`
PRODUCT_VER=`cat $pd/temp/${SepPatch_ZIPSNAME[$i]}/kdpkgs.xml|grep 'product '|awk -F 'ver=' '{print $2}'|awk -F '"' '{print $2}'`
FIELD_NAME=`cat $pd/temp/${SepPatch_ZIPSNAME[$i]}/kdpkgs.xml|grep 'field '|awk -F 'name=' '{print $2}'|awk -F '"' '{print $2}'`
FIELD_nameCN=`cat $pd/temp/${SepPatch_ZIPSNAME[$i]}/kdpkgs.xml|grep 'field '|awk -F 'nameCN' '{print $2}'|awk -F '"' '{print $2}'`

#### sepReleaseList
if [ "${FORMAT_VERSION}" == "3.0" ];then
    if [ ! -f $pd/sepReleaseList.xml ];then
        cat > $pd/sepReleaseList.xml <<EOF
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sepReleaseList>
    <product name="${PRODUCT_NAME}">
        <field name="${FIELD_NAME}" nameCN="${FIELD_nameCN}">
            <patch dependOn="" modifyTime="`date '+%Y-%m-%d%H%M%S'`" ver="${PRODUCT_VER}"/>
        </field>
    </product>
</sepReleaseList>
EOF
    fi
    if [ -s $pd/sepReleaseList.xml ];then
        cat $pd/sepReleaseList.xml|grep -w ${PRODUCT_NAME}
  	    if [ $? -eq 0 ];then
		    PRODUCT_NAME_ROWNUM=`cat -n $pd/sepReleaseList.xml|grep -w ${PRODUCT_NAME}|awk '{print $1}'`
			PATCH_ROWNUM=$((${PRODUCT_NAME_ROWNUM} + 2))
  	        sed -i "${PATCH_ROWNUM} {s/<patch\ dependOn.*/<patch dependOn=\"\" modifyTime=\"`date '+%Y-%m-%d%H%M%S'`\" ver=\"${PRODUCT_VER}\"\/>/g}" $pd/sepReleaseList.xml
  	    else
  	        sed -i "/<sepReleaseList>/a \ \ \ \ </product>" $pd/sepReleaseList.xml
  	        sed -i "/<sepReleaseList>/a \ \ \ \ \ \ \ \ </field>" $pd/sepReleaseList.xml
  	        sed -i "/<sepReleaseList>/a \ \ \ \ \ \ \ \ \ \ \ \ <patch dependOn=\"\" modifyTime=\"`date '+%Y-%m-%d%H%M%S'`\" ver=\"${PRODUCT_VER}\"\/>" $pd/sepReleaseList.xml
            sed -i "/<sepReleaseList>/a \ \ \ \ \ \ \ \ <field name=\"${FIELD_NAME}\" nameCN=\"${FIELD_nameCN}\">" $pd/sepReleaseList.xml
            sed -i "/<sepReleaseList>/a \ \ \ \ <product name=\"${PRODUCT_NAME}\">" $pd/sepReleaseList.xml
  	    fi
    fi
[ -d $pd/kingdee/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER} ] && rm -rf $pd/kingdee/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}/* || mkdir -p $pd/kingdee/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}
unzip -o $pd/temp/${SepPatch_ZIPSNAME[$i]}.zip -d $pd/kingdee/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}/
fi
	
#### emergency
if [ "${FORMAT_VERSION}" == "3.1" ];then
    if [ ! -f $pd/emerReleaseList.xml ];then
        cat > $pd/emerReleaseList.xml <<EOF
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sepReleaseList>
    <product name="${PRODUCT_NAME}">
        <field name="${FIELD_NAME}" nameCN="${FIELD_nameCN}">
            <patch dependOn="" modifyTime="`date '+%Y-%m-%d%H%M%S'`" ver="${PRODUCT_VER}"/>
        </field>
    </product>
</sepReleaseList>
EOF
    fi
    if [ -s $pd/emerReleaseList.xml ];then
        cat $pd/emerReleaseList.xml|grep -w ${PRODUCT_NAME}
  	    if [ $? -eq 0 ];then
		    PRODUCT_NAME_ROWNUM=`cat -n $pd/emerReleaseList.xml|grep -w ${PRODUCT_NAME}|awk '{print $1}'`
			PATCH_ROWNUM=$((${PRODUCT_NAME_ROWNUM} + 2))
  	        sed -i "${PATCH_ROWNUM} {s/<patch\ dependOn.*/<patch dependOn=\"\" modifyTime=\"`date '+%Y-%m-%d%H%M%S'`\" ver=\"${PRODUCT_VER}\"\/>/g}" $pd/emerReleaseList.xml
  	    else
  	        sed -i "/<sepReleaseList>/a \ \ \ \ </product>" $pd/emerReleaseList.xml
  	        sed -i "/<sepReleaseList>/a \ \ \ \ \ \ \ \ </field>" $pd/emerReleaseList.xml
  	        sed -i "/<sepReleaseList>/a \ \ \ \ \ \ \ \ <patch dependOn=\"\" modifyTime=\"`date '+%Y-%m-%d%H%M%S'`\" ver=\"${PRODUCT_VER}\"\/>" $pd/emerReleaseList.xml
            sed -i "/<sepReleaseList>/a \ \ \ \ \ \ \ \ <field name=\"${FIELD_NAME}\" nameCN=\"${FIELD_nameCN}\">" $pd/emerReleaseList.xml
            sed -i "/<sepReleaseList>/a \ \ \ \ <product name=\"${PRODUCT_NAME}\">" $pd/emerReleaseList.xml
  	    fi
    fi
[ -d $pd/emergency/kingdee/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER} ] && rm -rf $pd/emergency/kingdee/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}/* || mkdir -p $pd/emergency/kingdee/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}
unzip -o $pd/temp/${SepPatch_ZIPSNAME[$i]}.zip -d $pd/emergency/kingdee/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}/
fi

#### sepLang 
if [ "${FORMAT_VERSION}" == "3.2" ];then 
    if [ ! -f $pd/langReleaseList.xml ];then
    cat > $pd/langReleaseList.xml <<EOF
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sepReleaseList>
    <product name="${PRODUCT_NAME}">
        <field name="${FIELD_NAME}" nameCN="${FIELD_nameCN}">
            <patch dependOn="" modifyTime="`date '+%Y-%m-%d%H%M%S'`" ver="${PRODUCT_VER}"/>
        </field>
    </product>
</sepReleaseList>
EOF
    fi
	if [ -s $pd/langReleaseList.xml ];then
	    cat -n $pd/langReleaseList.xml|grep -w ${PRODUCT_NAME}
  	    if [ $? -eq 0 ];then
		    PRODUCT_NAME_ROWNUM=`cat -n $pd/langReleaseList.xml|grep -w ${PRODUCT_NAME}|awk '{print $1}'`
			PATCH_ROWNUM=$((${PRODUCT_NAME_ROWNUM} + 2))
  	        sed -i "${PATCH_ROWNUM} {s/<patch\ dependOn.*/<patch dependOn=\"\" modifyTime=\"`date '+%Y-%m-%d%H%M%S'`\" ver=\"${PRODUCT_VER}\"\/>/g}" $pd/langReleaseList.xml
  	    else
  	        sed -i "/<sepReleaseList>/a \ \ \ \ </product>" $pd/langReleaseList.xml
  	        sed -i "/<sepReleaseList>/a \ \ \ \ \ \ \ \ </field>" $pd/langReleaseList.xml
  	        sed -i "/<sepReleaseList>/a \ \ \ \ \ \ \ \ <patch dependOn=\"\" modifyTime=\"`date '+%Y-%m-%d%H%M%S'`\" ver=\"${PRODUCT_VER}\"\/>" $pd/langReleaseList.xml
            sed -i "/<sepReleaseList>/a \ \ \ \ \ \ \ \ <field name=\"${FIELD_NAME}\" nameCN=\"${FIELD_nameCN}\">" $pd/langReleaseList.xml
            sed -i "/<sepReleaseList>/a \ \ \ \ <product name=\"${PRODUCT_NAME}\">" $pd/langReleaseList.xml
  	    fi
    fi
[ -d $pd/sepLang/kingdee/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER} ] && rm -rf $pd/sepLang/kingdee/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}/* || mkdir -p $pd/sepLang/kingdee/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}
unzip -o $pd/temp/${SepPatch_ZIPSNAME[$i]}.zip -d $pd/sepLang/kingdee/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}/
fi
done

