#!/bin/bash
# 腳本參數資訊
[ "$1" == "" ] && exec echo "Usage: $0 <APK_FILENAME>"

# 如果 gamerc 存在, 則載入它
[ -f ~/.gamerc ] && source ~/.gamerc
APP_ID_VAL=$(echo ${SDK_APP_KEY})

# 確認 JAVA 套件是否有安裝
sudo yum list installed | grep ^java-
if [ "$?" -eq "1" ]; then
        exec echo "Please install JAVA rpm packages first! => sudo yum makecache fast; sudo yum -y install java"
fi

APK_FILENAME=$1
# 確認 APK 是否存在
if [ ! -f ${APK_FILENAME} ]; then
	exec echo "APK file is not exist, please check again!"
fi

# APK UNPACK 暫存位置
TMP_DIRECTORY_NAME=$(basename ${APK_FILENAME} | sed 's/.apk$//')
if [ -d /tmp/${TMP_DIRECTORY_NAME} ]; then
	rm -rf /tmp/${TMP_DIRECTORY_NAME}
fi

# APK UNPACK
echo 
echo "APK UNPACK START"
echo
apktool d ${APK_FILENAME} -o /tmp/${TMP_DIRECTORY_NAME} -q
echo
echo "APK UNPACK END"

# 擷取 APK 相關較重要的資訊
cd /tmp/${TMP_DIRECTORY_NAME}
APP_PACKAGE_NAME=$(cat AndroidManifest.xml | grep package | perl -ne 'print "$1\n" if/.* package="(\S+)" .*/')
APP_VERSOIN_NAME=$(cat apktool.yml | egrep versionName | awk -F" " '{print $2}')
APP_VERSION_CODE=$(cat apktool.yml | egrep versionCode | awk -F" " '{print $2}' | sed "s/'//g")
if [ -f ~/.gamerc ]; then
	if [ "${GAME_TYPE}" == "M12" -o "${GAME_TYPE}" == "G2" ]; then
		APP_OBB_NAME=$(cat res/values/strings.xml | egrep -i \\.obb  | perl -ne 'print "$1\n" if/<string name="\S+">(\S+)<\/string>/')
	fi
	APP_ID=$(cat res/values/strings.xml | egrep xlogin_appid | perl -ne 'print "$1\n" if/<string name="xlogin_appid">(\S+)<\/string>/')

	if [ "${GAME_TYPE}" == "M12" -o "${GAME_TYPE}" == "G2" ]; then
		if [ -f assets/pack/data/connect.ini ]; then
			APP_CONNECT=$(cat assets/pack/data/connect.ini | awk -F"=" '{print $2}')
		else
			APP_CONNECT="NOT EXIST!"
		fi
	fi

	if [ "${APP_ID_VAL}" -eq "${APP_ID}" ]; then
		APP_ID_CHECK="(O)"
	else
		APP_ID_CHECK="(X)"
	fi
fi

# 確認 APK / XAPK
CHECK_APP_TYPE=$(cat AndroidManifest.xml | grep -i channel | grep -i xapk)
if test -z "${CHECK_APP_TYPE}"; then
	APP_TYPE="APK"
else
	APP_TYPE="XAPK"
fi

# 輸出 APK 重要檔案內容至特定目錄
CHECK_DIRECTORY_NAME="${HOME}/www/app/android/check/${APP_PACKAGE_NAME}_${APP_VERSOIN_NAME}_${APP_VERSION_CODE}_${APP_TYPE}"

if [ -d ${CHECK_DIRECTORY_NAME} ]; then
	rm -rf ${CHECK_DIRECTORY_NAME}
fi

mkdir -p ${CHECK_DIRECTORY_NAME}

cp -arf apktool.yml ${CHECK_DIRECTORY_NAME}
cp -arf AndroidManifest.xml ${CHECK_DIRECTORY_NAME}
cp -arf res/values/strings.xml ${CHECK_DIRECTORY_NAME}
if [ -f ~/.gamerc ]; then
	if [ "${GAME_TYPE}" == "M12" -o "${GAME_TYPE}" == "G2" ]; then
		if [ -f assets/pack/data/connect.ini ]; then
			cp -arf assets/pack/data/connect.ini ${CHECK_DIRECTORY_NAME}
		fi
	fi
fi

# 刪除 APK UNPACK 目錄與檔案
rm -rf /tmp/${TMP_DIRECTORY_NAME}

# 輸出 APK 資訊
echo
echo "====== APK INFORMATION ======"
echo
echo "APP_TYPE: ${APP_TYPE}"
if [ -f ~/.gamerc ]; then
	echo "APP_ID: ${APP_ID} ${APP_ID_CHECK}"
else
	echo "APP_ID: ${APP_ID}"
fi
echo "APP_PACKAGE_NAME: ${APP_PACKAGE_NAME}"
echo "APP_VERSOIN_NAME: ${APP_VERSOIN_NAME}"
echo "APP_VERSION_CODE: ${APP_VERSION_CODE}"
if [ "${GAME_TYPE}" == "M12" -o "${GAME_TYPE}" == "G2" ]; then
	echo "APP_CONNECT: ${APP_CONNECT}"
	if [ "${APP_TYPE}" == "APK" ]; then
		echo "APP_OBB_NAME: ${APP_OBB_NAME}"
	fi
fi
echo
echo "====== APK INFORMATION ======"
