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

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

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

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

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

# IPA UNPACK
echo 
echo "IPA UNPACK START"
echo
unzip -q ${IPA_FILENAME} -d /tmp/${TMP_DIRECTORY_NAME}
echo
echo "IPA UNPACK END"

# 擷取 IPA 相關較重要的資訊
cd /tmp/${TMP_DIRECTORY_NAME}
plistutil -i Payload/*.app/Info.plist -o Info.plist.decrypt
if [ "${GAME_TYPE}" == "M12" ]; then
	cp -arf ./Payload/Build.app/mobilepack/data/connect.ini .
fi

APP_BUNDLE_ID=$(cat Info.plist.decrypt | awk '/CFBundleIdentifier/{getline; print}' | sed 's|<string>||;s|</string>||;s/\s//g')
APP_BUNDLE_SHORT_VERSION_STRING=$(cat Info.plist.decrypt | awk '/CFBundleShortVersionString/{getline; print}' | sed 's|<string>||;s|</string>||;s/\s//g')
APP_BUNDLE_VERSION=$(cat Info.plist.decrypt | awk '/CFBundleVersion/{getline; print}' | sed 's|<string>||;s|</string>||;s/\s//g')
if [ -f ~/.gamerc ]; then
	APP_ID=$(cat Info.plist.decrypt | awk '/XLSDK_APPID/{getline; print}' | sed 's|<string>||;s|</string>||;s/\s//g')

	if [ "${GAME_TYPE}" == "M12" ]; then
		if [ -f connect.ini ]; then
			APP_CONNECT=$(cat 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

# 確認 STORE / ADHOC
CHECK_APP_TYPE=$(basename ${IPA_FILENAME} | grep ad)
if test -z "${CHECK_APP_TYPE}"; then
	APP_TYPE="STORE"

	# 輸出 IPA 重要檔案內容至特定目錄
	CHECK_DIRECTORY_NAME="${HOME}/www/app/ios/build/check/${APP_BUNDLE_ID}_${APP_BUNDLE_SHORT_VERSION_STRING}_${APP_BUNDLE_VERSION}"
else
	APP_TYPE="ADHOC"

	# 輸出 IPA 重要檔案內容至特定目錄
	CHECK_DIRECTORY_NAME="${HOME}/www/app/ios/build_ad/check/${APP_BUNDLE_ID}_${APP_BUNDLE_SHORT_VERSION_STRING}_${APP_BUNDLE_VERSION}"
fi

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

mkdir -p ${CHECK_DIRECTORY_NAME}

cp -arf Info.plist.decrypt ${CHECK_DIRECTORY_NAME}
if [ -f ~/.gamerc ]; then
	if [ "${GAME_TYPE}" == "M12" ]; then
		if [ -f connect.ini ]; then
			cp -arf connect.ini ${CHECK_DIRECTORY_NAME}
		fi
	fi
fi

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

# 輸出 IPA 資訊
echo
echo "====== IPA 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_BUNDLE_ID: ${APP_BUNDLE_ID}"
echo "APP_BUNDLE_SHORT_VERSION_STRING: ${APP_BUNDLE_SHORT_VERSION_STRING}"
echo "APP_BUNDLE_VERSION: ${APP_BUNDLE_VERSION}"
if [ "${GAME_TYPE}" == "M12" ]; then
	echo "APP_CONNECT: ${APP_CONNECT}"
fi
echo
echo "====== IPA INFORMATION ======"
