# PRD or DEV or UAT，与环境文件对应
BUILD_ENV=$(echo $1 | tr '[a-z]' '[A-Z]')

BUILD_TYPE="$(echo ${2:0:1} | tr '[a-z]' '[A-Z]')$(echo ${2:1} | tr '[A-Z]' '[a-z]')"

# yes or no
DD_PUSH=$(echo $3 | tr '[A-Z]' '[a-z]')

if [ ! -n "${BUILD_ENV}" ] ;then
    echo "入参 'BUILD_ENV' 有误！"
    exit 1
fi

if [ ! -n "${BUILD_TYPE}" ] ;then
    echo "入参 'BUILD_TYPE' 有误！"
    exit 1
fi

if [ ! -n "${DD_PUSH}" ] ;then
    echo "入参 'DD_PUSH' 有误！"
    exit 1
fi

# 代码分支
CODE_BRANCH="master"
# 代码仓库
CODE_SOURCE="http://用户名:密码@仓库地址"

# 脚本仓库
SCRIPT_SOURCE="http://112.124.100.131/app/Android-AutoPackage.git"

# 蒲公英 API KEY #
PGY_API_KEY=""
# 蒲公英 USER KEY #
PGY_USER_KEY=""
# 钉钉 机器人 webhook 链接的 access_token 参数值 #
DD_HOST_URL=""

# 不要改 #
WORKSPACE="/Users/${USER}/AutoPackage/Android/CODE"
# 不要改 #
SCRIPT_PATH="/Users/${USER}/AutoPackage/Android/SCRIPT"

rm -rf ${WORKSPACE}
rm -rf ${SCRIPT_PATH}
git clone -b ${CODE_BRANCH} ${CODE_SOURCE} ${WORKSPACE}
git clone ${SCRIPT_SOURCE} ${SCRIPT_PATH}

cp -r ${SCRIPT_PATH}/apk_script ${WORKSPACE}
# 修改环境文件
cp -r ${WORKSPACE}/app/src/main/res/values/ccbpi_env/${BUILD_ENV}.xml ${WORKSPACE}/app/src/main/res/values/ccbpi.xml

sh ${WORKSPACE}/apk_script/auto_package_apk.sh ${BUILD_ENV} ${BUILD_TYPE} ${PGY_API_KEY} ${PGY_USER_KEY} ${DD_HOST_URL} ${DD_PUSH}
