#!/usr/bin/env bash
CWD=$(cd "$(dirname "$0")"; pwd)

# *****************************************************************************
# @author khy
# @2018.11.23
#
# 打特殊包(目前仅支持单个渠道)
#
# *****************************************************************************

source ${CWD}/assemble_special_env.sh

#if [[ -n "$(git status --porcelain)" ]]; then
#    echo "Please make sure that you commit your code before release "
#    exit 0
#fi

#read -p "TINKER_PATCH_VERSION_NAME:" TINKER_PATCH_VERSION_NAME
read -p "VERSION_NAME:" VERSION_NAME
read -p "VERSION_CODE:" VERSION_CODE
read -p "PACKAGE_NAME:" PACKAGE_NAME
read -p "APP_NAME:" APP_NAME
read -p "PATCH_DOWNLOAD_NAME:" PATCH_DOWNLOAD_NAME
read -p "PATCH_DOWNLOADING_NAME:" PATCH_DOWNLOADING_NAME
read -p "CHANNEL:" CHANNEL

echo "---------请再次确认，没有输入的既不修改---------"
#echo "TINKER_PATCH_VERSION_NAME = ${TINKER_PATCH_VERSION_NAME}"
echo "VERSION_NAME = ${VERSION_NAME}"
echo "VERSION_CODE = ${VERSION_CODE}"
echo "PACKAGE_NAME = ${PACKAGE_NAME}"
echo "APP_NAME = ${APP_NAME}"
echo "PATCH_DOWNLOAD_NAME = ${PATCH_DOWNLOAD_NAME}"
echo "PATCH_DOWNLOADING_NAME = ${PATCH_DOWNLOADING_NAME}"
echo "CHANNEL = ${CHANNEL}"
echo "-------------------------------------------"

read -p "是否确认修改信息，输入yes/no:" check

echo ${check}

if [[ ${check} != "yes" ]];then
     echo "-------------exit"
     exit 0
fi

#备份需要修改的文件
cp ${CONFIG_FILE} ${BAK_CONFIG_FILE}
cp ${GRADLE_FILE} ${BAK_GRADLE_FILE}
cp ${STRINGS_FILE} ${BAK_STRINGS_FILE}

# 获取将被修改的内容(备份)
#BACKUP_VERSION_CODE=`grep -r versionCode ${CONFIG_FILE} | awk '{print $3}'`
#BACKUP_VERSION_NAME=`grep -r versionName ${CONFIG_FILE} | awk '{print $3}'`
#BACKUP_PACKAGE_NAME=`grep -r applicationId ${CONFIG_FILE} | awk '{print $3}'`
#BACKUP_TINKER_PATCH_VERSION_NAME=`grep -r PATCH_VERSION_NAME ${GRADLE_FILE} | awk -F '=' '{print $2}'`
#echo "${BACKUP_VERSION_CODE} ${BACKUP_VERSION_NAME} ${BACKUP_PACKAGE_NAME} ${BACKUP_TINKER_PATCH_VERSION_NAME}"

# 替换需要修改的字符
#if [[ -n "$TINKER_PATCH_VERSION_NAME" ]]; then
#    sed -i "s/PATCH_VERSION_NAME\=\(.*\)/PATCH_VERSION_NAME\=${TINKER_PATCH_VERSION_NAME}/g" ${GRADLE_FILE}
#fi

if [[ -n "$VERSION_CODE" ]]; then
    sed -i "s/versionCode\ = \(.*\)/versionCode\ = ${VERSION_CODE}/g" ${CONFIG_FILE}
fi

if [[ -n "$VERSION_NAME" ]]; then
    sed -i "s/versionName\ = \(.*\)/versionName\ = \"${VERSION_NAME}\"/g" ${CONFIG_FILE}
fi

if [[ -n "$PACKAGE_NAME" ]]; then
    sed -i "s/applicationId\ = \(.*\)/applicationId\ = \"${PACKAGE_NAME}\"/g" ${CONFIG_FILE}
fi

if [[ -n "$APP_NAME" ]]; then
    sed -i "s/>光环助手</>${APP_NAME}</g" ${STRINGS_FILE}
fi

if [[ -n "$PATCH_DOWNLOAD_NAME" ]]; then
    sed -i "s/>下载</>${PATCH_DOWNLOAD_NAME}</g" ${STRINGS_FILE}
fi

if [[ -n "$PATCH_DOWNLOADING_NAME" ]]; then
    sed -i "s/>下载中</>${PATCH_DOWNLOADING_NAME}</g" ${STRINGS_FILE}
fi

sed -i "s/TINKER_ENABLE\=\(.*\)/TINKER_ENABLE\=1/g" ${GRADLE_FILE}

# 执行打包命令
${CMD_GRADLE_RELEASE}

if [[ -n "$CHANNEL" ]]; then
    java -jar ${CWD}/ApkChannelPackage.jar put -c ${CHANNEL} ${SOURCE_APK_FILE} ${SOURCE_APK_PATH}
fi


# 恢复文件
mv ${BAK_CONFIG_FILE} ${CONFIG_FILE}
mv ${BAK_GRADLE_FILE} ${GRADLE_FILE}
mv ${BAK_STRINGS_FILE} ${STRINGS_FILE}







