##############################################
# 分应用补丁解压脚本
# 使用方法
# 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 'ver=' '{print $2}' |awk -F '"' '{print $2}'`
FORMAT_TYPE=`cat $pd/temp/${SepPatch_ZIPSNAME[$i]}/kdpkgs.xml|grep '<format ' | awk -F 'type' '{print $2}' |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}'`


#### v
if [ "${FORMAT_TYPE}" == "v" ] || [ -z "${FORMAT_TYPE}" -a "${FORMAT_VERSION}" == "3.0" ]; then
    if [ ! -f $pd/vReleaseList.xml ];then
        cat > $pd/vReleaseList.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/vReleaseList.xml ];then
        cat $pd/vReleaseList.xml|grep -w ${PRODUCT_NAME}
  	    if [ $? -eq 0 ];then
		    PRODUCT_NAME_ROWNUM=`cat -n $pd/vReleaseList.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/vReleaseList.xml
  	    else
  	        sed -i "/<sepReleaseList>/a \ \ \ \ </product>" $pd/vReleaseList.xml
  	        sed -i "/<sepReleaseList>/a \ \ \ \ \ \ \ \ </field>" $pd/vReleaseList.xml
  	        sed -i "/<sepReleaseList>/a \ \ \ \ \ \ \ \ \ \ \ \ <patch dependOn=\"\" modifyTime=\"`date '+%Y-%m-%d%H%M%S'`\" ver=\"${PRODUCT_VER}\"\/>" $pd/vReleaseList.xml
            sed -i "/<sepReleaseList>/a \ \ \ \ \ \ \ \ <field name=\"${FIELD_NAME}\" nameCN=\"${FIELD_nameCN}\">" $pd/vReleaseList.xml
            sed -i "/<sepReleaseList>/a \ \ \ \ <product name=\"${PRODUCT_NAME}\">" $pd/vReleaseList.xml
  	    fi
    fi
[ -d $pd/kingdee/v/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER} ] && rm -rf $pd/kingdee/v/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}/* || mkdir -p $pd/kingdee/v/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}
unzip -o $pd/temp/${SepPatch_ZIPSNAME[$i]}.zip -d $pd/kingdee/v/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}/
fi
	
#### r
if [ "${FORMAT_TYPE}" == "r" ] ;then
    if [ ! -f $pd/rReleaseList.xml ];then
        cat > $pd/rReleaseList.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/rReleaseList.xml ];then
        cat $pd/rReleaseList.xml|grep -w ${PRODUCT_NAME}
  	    if [ $? -eq 0 ];then
		    PRODUCT_NAME_ROWNUM=`cat -n $pd/rReleaseList.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/rReleaseList.xml
  	    else
  	        sed -i "/<sepReleaseList>/a \ \ \ \ </product>" $pd/rReleaseList.xml
  	        sed -i "/<sepReleaseList>/a \ \ \ \ \ \ \ \ </field>" $pd/rReleaseList.xml
  	        sed -i "/<sepReleaseList>/a \ \ \ \ \ \ \ \ \ \ \ \ <patch dependOn=\"\" modifyTime=\"`date '+%Y-%m-%d%H%M%S'`\" ver=\"${PRODUCT_VER}\"\/>" $pd/rReleaseList.xml
            sed -i "/<sepReleaseList>/a \ \ \ \ \ \ \ \ <field name=\"${FIELD_NAME}\" nameCN=\"${FIELD_nameCN}\">" $pd/rReleaseList.xml
            sed -i "/<sepReleaseList>/a \ \ \ \ <product name=\"${PRODUCT_NAME}\">" $pd/rReleaseList.xml
  	    fi
    fi
[ -d $pd/kingdee/r/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER} ] && rm -rf $pd/kingdee/r/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}/* || mkdir -p $pd/kingdee/r/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}
unzip -o $pd/temp/${SepPatch_ZIPSNAME[$i]}.zip -d $pd/kingdee/r/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}/
fi


#### p
if [ "${FORMAT_TYPE}" == "p" ];then
    if [ ! -f $pd/pReleaseList.xml ];then
        cat > $pd/pReleaseList.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/pReleaseList.xml ];then
        cat $pd/pReleaseList.xml|grep -w ${PRODUCT_NAME}
  	    if [ $? -eq 0 ];then
		    PRODUCT_NAME_ROWNUM=`cat -n $pd/pReleaseList.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/pReleaseList.xml
  	    else
  	        sed -i "/<sepReleaseList>/a \ \ \ \ </product>" $pd/pReleaseList.xml
  	        sed -i "/<sepReleaseList>/a \ \ \ \ \ \ \ \ </field>" $pd/pReleaseList.xml
  	        sed -i "/<sepReleaseList>/a \ \ \ \ \ \ \ \ \ \ \ \ <patch dependOn=\"\" modifyTime=\"`date '+%Y-%m-%d%H%M%S'`\" ver=\"${PRODUCT_VER}\"\/>" $pd/pReleaseList.xml
            sed -i "/<sepReleaseList>/a \ \ \ \ \ \ \ \ <field name=\"${FIELD_NAME}\" nameCN=\"${FIELD_nameCN}\">" $pd/pReleaseList.xml
            sed -i "/<sepReleaseList>/a \ \ \ \ <product name=\"${PRODUCT_NAME}\">" $pd/pReleaseList.xml
  	    fi
    fi
[ -d $pd/kingdee/p/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER} ] && rm -rf $pd/kingdee/p/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}/* || mkdir -p $pd/kingdee/p/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}
unzip -o $pd/temp/${SepPatch_ZIPSNAME[$i]}.zip -d $pd/kingdee/p/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}/
fi


#### eme
if [ "${FORMAT_TYPE}" == "eme" ] || [ -z "${FORMAT_TYPE}" -a "${FORMAT_VERSION}" == "3.1" ]; then
    if [ ! -f $pd/emeReleaseList.xml ];then
        cat > $pd/emeReleaseList.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/emeReleaseList.xml ];then
        cat $pd/emeReleaseList.xml|grep -w ${PRODUCT_NAME}
  	    if [ $? -eq 0 ];then
		    PRODUCT_NAME_ROWNUM=`cat -n $pd/emeReleaseList.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/emeReleaseList.xml
  	    else
  	        sed -i "/<sepReleaseList>/a \ \ \ \ </product>" $pd/emeReleaseList.xml
  	        sed -i "/<sepReleaseList>/a \ \ \ \ \ \ \ \ </field>" $pd/emeReleaseList.xml
  	        sed -i "/<sepReleaseList>/a \ \ \ \ \ \ \ \ <patch dependOn=\"\" modifyTime=\"`date '+%Y-%m-%d%H%M%S'`\" ver=\"${PRODUCT_VER}\"\/>" $pd/emeReleaseList.xml
            sed -i "/<sepReleaseList>/a \ \ \ \ \ \ \ \ <field name=\"${FIELD_NAME}\" nameCN=\"${FIELD_nameCN}\">" $pd/emeReleaseList.xml
            sed -i "/<sepReleaseList>/a \ \ \ \ <product name=\"${PRODUCT_NAME}\">" $pd/emeReleaseList.xml
  	    fi
    fi
[ -d $pd/kingdee/eme/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER} ] && rm -rf $pd/kingdee/eme/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}/* || mkdir -p $pd/kingdee/eme/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}
unzip -o $pd/temp/${SepPatch_ZIPSNAME[$i]}.zip -d $pd/kingdee/eme/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}/
fi

#### lang 
if [ "${FORMAT_TYPE}" == "lang" ] || [ -z "${FORMAT_TYPE}" -a "${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/kingdee/lang/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER} ] && rm -rf $pd/kingdee/lang/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}/* || mkdir -p $pd/kingdee/lang/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}
unzip -o $pd/temp/${SepPatch_ZIPSNAME[$i]}.zip -d $pd/kingdee/lang/${PRODUCT_NAME}/${FIELD_NAME}/${PRODUCT_VER}/
fi

done

