#!/bin/bash

report_file="${CWD}/NanoDroid-Report-${VERSION}"

create_report_real () {

	case ${VERSION} in
		*99999999* ) build_type="Beta Release" ;;
		*          ) build_type="Stable Release" ;;
	esac

	[ "${build_date}" -ne "${version_date}" ] && build_type="Snapshot Release"

	rm -f "${report_file}"*

	echo -e "
creating NanoDroid report:

	| NanoDroid Version:	${VERSION}
	| NanoDroid Type:	${build_type}
	| NanoDroid Report:	$(basename ${report_file})
"

	echo "NanoDroid Package Report
	| NanoDroid Version:	${VERSION}
	| NanoDroid Type:	${build_type}
	| Report Date:		$(date +%Y%m%d-%H.%M.%S)
" > ${report_file}

	for file in $(find ${CWD} -maxdepth 1 -type f | sort) \
		    $(find ${CWD} -mindepth 2 -type f | sort); do
		case ${file} in
			*NanoDroid*.zip* ) get_dst_info ${file} ;;
			*.apk.gz ) get_apk_info ${file} ;;
			*.so*    ) get_lib_info ${file} ;;
			*xbin*   ) get_bin_info ${file} ;;
			*tools*  ) get_bin_info ${file} ;;
			*etc*    ) get_etc_info ${file} ;;
			*.dex*   ) get_dex_info ${file} ;;
			*        ) get_msc_info ${file} ;;
		esac
	done

	sum_sign_package "${report_file}"

}

create_report () {
	create_report_real &
	progress $!
}

get_apk_info () {

	APK=${CWD}/_report.apk

	gzip -dc ${1} > ${APK}

	apk_file=$(basename ${1})
	apk_path=$(dirname ${1} | sed -e 's/.*NanoDroid/NanoDroid/')
	apk_name=$(aapt dump badging ${APK} 2>/dev/null | gawk -F \' '/^package: name/{print $2}')
	apk_version=$(aapt dump badging ${APK} 2>/dev/null | gawk -F \' '/^package: name/{print $6}')
	apk_code=$(aapt dump badging ${APK} 2>/dev/null | gawk -F \' '/^package: name/{print $4}')
	apk_perm=$(aapt dump badging ${APK} 2>/dev/null | gawk -F \' '/^uses-permission:/{print $2}' | sort)
	apk_sha=$(${sumsha256} ${APK} | gawk '{print $1}')
	gzip_sha=$(${sumsha256} ${1} | gawk '{print $1}')

	case ${1} in
		*gsync/K* ) APK_TYPE="Google Sync Adapters [KitKat]" ;;
		*gsync/L* ) APK_TYPE="Google Sync Adapters [Lolipop]" ;;
		*gsync/M* ) APK_TYPE="Google Sync Adapters [Marshmallow]" ;;
		*gsync/N* ) APK_TYPE="Google Sync Adapters [Nougat]" ;;
		*gsync/O* ) APK_TYPE="Google Sync Adapters [Oreo]" ;;
		*gsync/P* ) APK_TYPE="Google Sync Adapters [Pie]" ;;
		*gsync/common*) APK_TYPE="Google Sync Adapters [Generic]" ;;
		*priv-app*) APK_TYPE="Privileged Application" ;;
		*app*     ) APK_TYPE="Generic Application" ;;
	esac

	echo "Android Package: ${apk_name}
	| Filename:	${apk_file}
	| Origin:	${apk_path}
	| Type:		${APK_TYPE}
	| Version:	${apk_version}
	| VersionCode:	${apk_code}
	| SHA256 APK:	${apk_sha}
	| SHA256 GZip:	${gzip_sha}" >> ${report_file}

	if [ -z "${apk_perm}" ]; then
		echo -e "	| Permissions:	none requested\n" >> ${report_file}
	else	echo -e "	| Permissions:\n$(printf "\t|- %s\n" ${apk_perm})\n" >> ${report_file}
	fi

	rm -f ${APK}

}

get_lib_info () {

	local lib_file=$(basename ${1})
	local lib_path=$(dirname ${1} | sed -e 's/.*NanoDroid/NanoDroid/')

	case ${lib_file} in
		*libjni_latinimegoogle* ) LIB_NAME="Latin IME Google" ;;
		*libjni_keyboarddecoder*) LIB_NAME="Keyboard Decoder" ;;
	esac

	case ${1} in
		*19* ) local lib_code="19 [KitKat]" ;;
		*21* ) local lib_code="21 [Lollipop]" ;;
		*22* ) local lib_code="22 [Lollipop]" ;;
		*23* ) local lib_code="23 [Marshmallow]" ;;
		*24* ) local lib_code="23 [Nougat] {24}" ;;
		*25* ) local lib_code="23 [Nougat] {25}" ;;
		*26* ) local lib_code="26 [Oreo] {26}" ;;
		*27* ) local lib_code="27 [Oreo]" ;;
		*28* ) local lib_code="28 [Pie]" ;;
		*    ) local lib_code="Generic" ;;
	esac

	local lib_arch=$(file -b ${1} | gawk -F, '{print $2 " [ " $1 " ] [" $3 " ]"}')
	local lib_specs=$(file -b ${1} | gawk -F, '{print $4 " [" $5 " ]"}')

	lib_sha=$(${sumsha256} ${1} | gawk '{print $1}')

	echo "Android Library: ${LIB_NAME}
	| Filename:	${lib_file}
	| Origin:	${lib_path}
	| SDK Code:	${lib_code}
	| Arch:		${lib_arch}
	| Specifics:	${lib_specs}
	| SHA256:	${lib_sha}
" >> ${report_file}

}

get_bin_info () {

	local bin_file=$(basename ${1})
	local bin_name=$(basename ${1} | sed -e 's/\.[^.]*$//')
	local bin_path=$(dirname ${1} | sed -e 's/.*NanoDroid/NanoDroid/')

	case ${bin_path} in
		*xbin* ) local bin_type="Android User Utility" ;;
		*tools*) local bin_type="Android Installer Utility" ;;
	esac

	local bin_arch=$(file -b ${1} | gawk -F, '{print $2 " [ " $1 " ] [" $3 " ]"}')
	local bin_specs=$(file -b ${1} | gawk -F, '{print $4 " [" $7 " ] [" $6 " ]"}')

	local bin_sha=$(${sumsha256} ${1} | gawk '{print $1}')

	echo "${bin_type}: ${bin_name}
	| Filename:	${bin_file}
	| Origin:	${bin_path}
	| Arch:		${bin_arch}
	| Specifics:	${bin_specs}
	| SHA256:	${bin_sha}
" >> ${report_file}

}

get_etc_info () {

	local etc_file=$(basename ${1})
	local etc_path=$(dirname ${1} | sed -e 's/.*NanoDroid/NanoDroid/')

	local etc_pkgs=""

	case ${1} in
		*gsync*default-permissions* ) local etc_type="Google Sync Adapters Permissions"
		                              local etc_pkgs="$(gawk -F\" '/package=/{print $2}' ${1})" ;;
		*etc/permissions*           ) local etc_type="Application Permissions"
		                              local etc_pkgs="$(gawk -F\" '/package=/{print $2}' ${1})" ;;
		*etc/default-permissions*   ) local etc_type="Application Permissions"
		                              local etc_pkgs="$(gawk -F\" '/package=/{print $2}' ${1})" ;;
		*etc/terminfo*              ) local etc_type="GNU Nano Terminfo" ;;
		*etc/init.d*                ) local etc_type="NanoDroid init Script" ;;
		*etc/sysconfig*             ) local etc_type="Linux sysconfig Settings" ;;
		*etc/bash*                  ) local etc_type="GNU Bash Configuration" ;;
		*etc/org.fdroid.fdroid*     ) local etc_type="F-Droid Configuration" ;;
	esac

	local etc_sha=$(${sumsha256} ${1} | gawk '{print $1}')

	echo "${etc_type}: ${etc_file}
	| Filename:	${etc_file}
	| Origin:	${etc_path}
	| SHA256:	${etc_sha}" >> ${report_file}

	if [ ! -z "${etc_pkgs}" ]; then
		echo -e "	| Packages:\n$(printf "\t|- %s\n" ${etc_pkgs})\n" >> ${report_file}
	else	echo "" >> ${report_file}
	fi

}

get_dex_info () {

	local dex_file=$(basename ${1})
	local dex_path=$(dirname ${1} | sed -e 's/.*NanoDroid/NanoDroid/')

	case ${dex_file} in
		*services* ) local dex_type="Android DEX Patch File"
		             local dex_name=$(basename ${1} .jar.dex) ;;
		*          ) local dex_type="Android Dalvik Executable"
		             local dex_name=$(basename ${1} .dex) ;;
	esac

	case ${dex_file} in
		*services* ) local dex_sdk=$(gawk -F\_ '{print $2}' <(echo ${dex_file})) ;;
		*smali*    ) local dex_sdk=$(sed 's/.*_\(.*\)\..*/\1/' <(echo ${dex_file})) ;;
		*          ) local dex_sdk="universal" ;;
	esac

	dex_ver=$(file -b ${1} | gawk '{print $5}')
	dex_sha=$(${sumsha256} ${1} | gawk '{print $1}')

	echo "${dex_type}: ${dex_name}
	| Filename:	${dex_file}
	| Origin:	${dex_path}
	| DEX Version:	${dex_ver}
	| DEX SDK:	${dex_sdk}
	| SHA256:	${dex_sha}
" >> ${report_file}

}

get_msc_info () {

	case ${1} in
		*CommonAddon*     ) local msc_type="NanoDroid universal addon.d" ;;
		*CommonInstaller* ) local msc_type="NanoDroid universal Installer" ;;

		*service.sh*      ) local msc_type="NanoDroid universal Magisk Module service.sh" ;;

		*Full*update*     ) local msc_type="NanoDroid Full Package TWRP invoker" ;;
		*Full*.prop*      ) local msc_type="NanoDroid Full Package Module Module properties" ;;

		*microG*update*   ) local msc_type="NanoDroid microG Package TWRP invoker" ;;
		*microG*.prop*    ) local msc_type="NanoDroid microG Package Module Module properties" ;;

		*FDroid*update*   ) local msc_type="NanoDroid F-Droid Package TWRP invoker" ;;
		*FDroid*.prop*    ) local msc_type="NanoDroid F-Droid Package Module Module properties" ;;

		*Bromite*update*  ) local msc_type="NanoDroid BromiteWebView Package TWRP invoker" ;;
		*Bromite*.prop*   ) local msc_type="NanoDroid BromiteWebView Package Module Module properties" ;;

		*OsmAnd*update*   ) local msc_type="NanoDroid OsmAnd Package TWRP invoker" ;;
		*OsmAnd*.prop*    ) local msc_type="NanoDroid OsmAnd Package Module Module properties" ;;

		*CommonPatcher*   ) local msc_type="NanoDroid Framework Patcher" ;;
		*70-nanodroidpatcher.sh* ) local msc_type="NanoDroid Framework Patcher addon.d" ;;
		*org.spoofing.apk*) local msc_type="NanoDroid Framework Patcher spoofing info provider" ;;
		*patcher*update*  ) local msc_type="NanoDroid Framework Patcher TWRP invoker" ;;
		*patcher*.prop*      ) local msc_type="NanoDroid Framework Patcher Module Module properties" ;;

		*SetupWizard*update*) local msc_type="NanoDroid SetupWizard AROMA invoker" ;;
		*SetupWizard*aroma*) local msc_type="NanoDroid SetupWizard AROMA configuration" ;;
		*SetupWizard/nano*) local msc_type="NanoDroid SetupWizard Worker" ;;

		*SysTest*         ) local msc_type="NanoDroid System Tester" ;;
		*systest*update*  ) local msc_type="NanoDroid System Tester TWRP invoker" ;;

		*uninstaller*binary*) local msc_type="NanoDroid Uninstaller" ;;
		*uninstaller*update*) local msc_type="NanoDroid Uninstaller TWRP invoker" ;;

		*Google*update*   ) local msc_type="NanoDroid Google Package TWRP invoker" ;;
		*Google*.prop*    ) local msc_type="NanoDroid Google Module properties" ;;

		*ChangeLog.md*    ) local msc_type="NanoDroid ChangeLog" ;;
		*README.md*       ) local msc_type="NanoDroid README" ;;

		*.nanodroid-apps* ) local msc_type="NanoDroid default app installation configuration" ;;
		*.nanodroid-setup*) local msc_type="NanoDroid default installation configuration" ;;
		*.nanodroid-overlay*) local msc_type="NanoDroid default overlay configuration" ;;

		*.maps.jar*       ) local msc_type="microG Maps v1 support"
				    local msc_name=$(basename ${1}) ;;

		*LICENSE*         ) local msc_type="exclude" ;;
		*doc*             ) local msc_type="exclude" ;;
		*build-package*   ) local msc_type="exclude" ;;
		*.json*           ) local msc_type="exclude" ;;
		*.git*            ) local msc_type="exclude" ;;
		*NanoDroid-Report*) local msc_type="exclude" ;;
		*NanoDroid/dist*  ) local msc_type="exclude" ;;

		*                 ) local msc_type="unknown" ;;
	esac

	[[ "${1}" == *.prop* ]] && local msc_name=$(basename ${1})
	[[ "${msc_type}" == *Zelda* ]] && local msc_name=$(basename ${1} | sed -e 's/\..*//;s/_/ /g')

	case ${msc_type} in
		exclude ) return 0 ;;
		unknown ) echo "No report template for file:	${1}" ;;
		*       ) local msc_file=$(basename ${1})
		          local msc_path=$(dirname ${1} | sed -e 's/.*NanoDroid/NanoDroid/')
		          local msc_sha=$(${sumsha256} ${1} | gawk '{print $1}')

		          [ -z "${msc_name}" ] && local msc_name=$(basename ${1} | sed -e 's/\.[^.]*$//')

		          echo "${msc_type}: ${msc_name}
	| Filename:	${msc_file}
	| Origin:	${msc_path}
	| SHA256:	${msc_sha}
" >> ${report_file}
		;;
	esac

}

get_dst_info () {

	if [[ ! ${1} == *NanoDroid/dist* ]]; then

		local dst_file=$(basename ${1})
		local dst_name=$(basename ${1} | sed -e 's/-[0-9].*//')
		local dst_path=$(dirname ${1} | sed -e 's/.*NanoDroid/NanoDroid/')

		case ${dst_file} in
			*NanoDroid-[0-9]*.zip*) local dst_pkg="NanoDroid Full" ;;
			*microG*.zip*         ) local dst_pkg="NanoDroid microG" ;;
			*fdroid*.zip*         ) local dst_pkg="NanoDroid F-Droid" ;;
			*BromiteWebView*.zip* ) local dst_pkg="NanoDroid BromiteWebView" ;;
			*OsmAnd*.zip*         ) local dst_pkg="NanoDroid OsmAnd" ;;
			*patcher*.zip*        ) local dst_pkg="NanoDroid Framework Patcher" ;;
			*setupwizard*.zip*    ) local dst_pkg="NanoDroid SetupWizard" ;;
			*systest*.zip*        ) local dst_pkg="NanoDroid System Tester" ;;
			*uninstaller*.zip*    ) local dst_pkg="NanoDroid Uninstaller" ;;
			*Google*.zip*         ) local dst_pkg="NanoDroid Google" ;;
		esac

		case ${dst_file} in
			*.zip    ) local dst_type="Flashable Package" ;;
			*.sha256 ) local dst_type="Flashable Package (SHA256 Checksum)" ;;
			*.sig    ) local dst_type="Flashable Package (GPG Signature)" ;;
		esac

		dst_sha=$(${sumsha256} ${1} | gawk '{print $1}')

		echo "${dst_type}: ${dst_name}
	| Filename:	${dst_file}
	| Origin:	${dst_path}
	| Package:	${dst_pkg}
	| SHA256:	${dst_sha}
" >> ${report_file}

	fi

}
