######################################################################
####function:install appstore
####author:lance_cao
####create date:2019-11
####modify date:2019-11
######################################################################

#!/bin/bash
set -e
echo "[`date +'%F %H:%M:%S'`] [INFO] useage : bash installappstore.sh sharepath sys_user"
if [ $# -ne 2 ]; then
        echo "$(basename $0): Missing script argument"
        echo "$(basename $0) [sharepath] [sys_user]"
        exit 1
fi

## ENV ##
pd=$PWD
vsharepath=$1
sys_user=$2
isSharepath=1
if [ -z "$vsharepath" ];then
    isSharepath=0
    vsharepath=$pd
fi
# 创建appstatic.tar.gz中不存在的文件夹
mkdir -p $vsharepath/appstatic
# 二开路径
mkdir -p $vsharepath/appstatic/appstore/cosmic/cus
# 补丁仓库
mkdir -p $vsharepath/appstatic/patchwarehouse/mc-self-upgrade

## tar appstatic.tar.gz ##
if [ -f "$pd/appstatic.tar.gz" ];then
    if [ $isSharepath -eq "1" ];then
       cp appstatic.tar.gz $vsharepath
    fi
    cd $vsharepath
    tar -xf appstatic.tar.gz
#    unzip -o appstatic.tar.gz
fi

#create link
cd $vsharepath/appstatic
pd=$PWD
# 删掉原来的连接再连接一次
ln -sf $pd /var/appstatic
chown -R $sys_user. $vsharepath/appstatic
echo "appstore install success "

# 修改jar包配置，防止建库接口报错
function modify_jar() {
    echo -e "开始解压 bos-corelib.zip"
    # 解压压缩包
    if [ -d /var/appstatic/appstore/mc/bos/bos-corelib ]
    then
      rm -rf /var/appstatic/appstore/mc/bos/bos-corelib
    fi
    unzip -o -d /var/appstatic/appstore/mc/bos/ /var/appstatic/appstore/mc/bos/bos-corelib.zip > /dev/null 2>&1
    if [ $? -ne 0 ]
    then
        echo -e "unzip bos-corelib.zip failed!"
        exit 3
    fi
    # 切换路径
    cd /var/appstatic/appstore/mc/bos/bos-corelib
    pwd
    # 提取jar包文件
    echo -e "查看 bos-mq-1.0.jar"
    jar -xf bos-mq-1.0.jar `jar -tf bos-mq-1.0.jar|grep mqqueueconfig.xml`
    exists=`grep -q "exeDataBase_service" kd/bos/mq/mqqueueconfig.xml; echo $?`
    # 0表示存在，直接返回不再添加
    if [ $exists -eq 0 ]; then
        echo -e "mq package exist, no need to add"
        cd .. && rm -rf /var/appstatic/appstore/mc/bos/bos-corelib
        exit 0
    fi
    echo -e "mq package not exist, add to mqqueueconfig.xml"
    #修改文件内容
    CONTENT='        <region name=\"mc\">\
                <queue name=\"exeDataBase_service\">\
                  <consumer class=\"kd.bos.mc.datacenter.async.ExecuteDataBaseConsumer\"></consumer>\
                </queue>\
            </region>'
    sed -i '/<\/root>/i\'"$CONTENT" kd/bos/mq/mqqueueconfig.xml > /dev/null 2>&1
    if [ $? -ne 0 ]
    then
      echo -e "add mq package failed"
      exit 7
    fi
    echo "add mq package success"
    # 更新jar包
    jar uf bos-mq-1.0.jar kd/bos/mq/mqqueueconfig.xml > /dev/null 2>&1
    if [ $? -ne 0 ]
    then
      echo -e "update jar bos-mq-1.0.jar failed"
      exit 8
    fi
    echo -e "update jar bos-mq-1.0.jar success"
    # 删除文件
    rm -rf kd
    # 重新压缩文件
    zip -r bos-corelib.zip *
    # 移动压缩文件
    mv -f bos-corelib.zip ../bos-corelib.zip
    # 删除文件
    cd .. && rm -rf bos-corelib
}

#echo "start to change mc jar"
#modify_jar
#echo "finish to change mc jar"
