#!/bin/bash

NANODROID_MISSING_FILES=0
NANODROID_BROKEN_FILES=0
missing_database=""

check_nanodroid () {
	for file in ${files_database[@]}; do
		if [[ ! -f ${CWD}/${file} ]]; then
			NANODROID_MISSING_FILES=1
			missing_database=(${missing_database[@]} ${file})
		else
			case ${file} in
				*.gz )
					mimetype=$(file -b --mime-type ${file})
					mimetype_int=$(gzip -dc ${file} | file -b --mime-type - )
				;;

				*.so* )
					mimetype=$(file -b --mime-type ${file})
					mimetype_int=""
				;;
			esac

			case ${mimetype} in
				application/x-sharedlib )
					# library
				;;

				application/gzip )
					# APK
				;;

				* )
					NANODROID_BROKEN_FILES=1
					broken_database=(${broken_database[@]} ${file})
					rm -f ${file}
				;;
			esac

			case ${mimetype_int} in
				"" )
					# library
				;;

				application/java-archive | application/zip )
					# APK
				;;

				* )
					NANODROID_BROKEN_FILES=1
					broken_database=(${broken_database[@]} ${file})
					rm -f ${file}
				;;
			esac
		fi
	done

	if [[ ${NANODROID_MISSING_FILES} -ne 0 ]]; then
		echo -e "\nThe following files are missing:\n"
		printf '%s\n' ${missing_database[@]} | uniq
	fi

	if [[ ${NANODROID_BROKEN_FILES} -ne 0 ]]; then
		echo -e "\nThe following files are broken (and will be removed):\n"
		printf '%s\n' ${broken_database[@]} | uniq
	fi

	if [[ ${NANODROID_MISSING_FILES} -eq 0 && ${NANODROID_BROKEN_FILES} -eq 0 ]]; then
		echo -e "\nNanoDroid correctly populated\n"
		return 0
	else	return 1
	fi
}

update_patches () {
	grab_patches sigspoof-core/services.jar.dex core_services.jar.dex
	grab_patches sigspoof-hook-4.1-6.0/services.jar.dex hook_4.1-6.0_services.jar.dex
	grab_patches sigspoof-hook-7.0-9.0/services.jar.dex hook_7.0-9.0_services.jar.dex
}

progress () {
	sleep 0.5
	while [ -d "/proc/${1}" ];do
		echo -ne '/\x08' ; sleep 0.05
		echo -ne '-\x08' ; sleep 0.05
		echo -ne '\\\x08' ; sleep 0.05
		echo -ne '|\x08' ; sleep 0.05
	done

	echo "Done"
}

zip_add () {
	FILE_PATH="${CWD}/${1}"
	FILE_MASK="${2}"
	ZIP_PATH="${CWD}/${ZIP}"

	cd "${FILE_PATH}"

	if [ -z "${FILE_MASK}" ]; then
		zip -qr "${ZIP_PATH}" . || exit 1
	elif [ -d "${FILE_MASK}" ]; then
		zip -qr "${ZIP_PATH}" "${FILE_MASK}"  || exit 1
	else
		zip -q "${ZIP_PATH}" ${FILE_MASK} || exit 1
	fi

	cd "${CWD}"
}

zip_add_setup () {
	cd "${CWD}/doc"

	ZIP_PATH="${CWD}/${ZIP}"

	case ${1} in
		0 )
			sed -e 's/nanodroid_overlay=1/nanodroid_overlay=0/' -i .nanodroid-setup
			zip -q "${ZIP_PATH}" .nanodroid-setup || exit 1
			sed -e 's/nanodroid_overlay=0/nanodroid_overlay=1/' -i .nanodroid-setup
		;;

		1 )
			zip -q "${ZIP_PATH}" .nanodroid-setup || exit 1
		;;
	esac

	cd "${CWD}"
}

increase_version_number () {
	if [ -z ${3} ]; then
		ver=99999999
	else	ver=${3}
	fi

	for file in  "build-package" "CommonInstaller" \
		"systest/SysTest" "patcher/CommonPatcher" \
		"SetupWizard/nanodroid-setup" "CommonAddon" \
		"uninstaller/META-INF/com/google/android/update-binary"; do
		sed -e "s/^VERSION=.*/VERSION=${2}.${ver}/" -i "${CWD}/${file}"
	done

	for module in Full microG FDroid BromiteWebView OsmAnd; do
		sed -e "s/version=.*/version=v${2}.${ver}/" -i \
			"${CWD}/${module}/module.prop"
	done

	sed -e "s/rom_version\",.*/rom_version\",	\"${2}\")\;/" -i \
		"${CWD}/SetupWizard/META-INF/com/google/android/aroma-config"
	sed -e "s/rom_date\",.*/rom_date\",	\"${ver}\")\;/" -i \
		"${CWD}/SetupWizard/META-INF/com/google/android/aroma-config"

	exit 0
}

increase_module_version () {
	for module in Full microG FDroid BromiteWebView OsmAnd; do
		eval $(grep versionCode "${PWD}"/"${module}"/module.prop)
		versionCode=$((versionCode+1))
		sed -e "s/versionCode.*/versionCode=${versionCode}/" \
			-i "${CWD}/${module}/module.prop"
	done

	exit 0
}

dalvikize_jar () {
	if [[ -f ${1} ]]; then
		${ANDROID_HOME}/build-tools/27.0.3/dx --dex \
			--output="$(basename "${1}" .jar).dex" \
			"${1}"
	fi

	exit 0
}

show_help () {
	echo -e "
** NanoDroid ${VERSION} helper script **

usage:	build-package [opt] [arg]

setup NanoDroid [! do this first !]:
	pull			| download all required apks for NanoDroid

update contents:
	u-microg		| update microG and companions
	u-fdroid		| update F-Droid and Extension
	u-osmand		| update OsmAnd and Plugins
	u-apps			| update applications
	u-swipe			| update swipe libraries
	u-gsync			| update Google Sync Adapters

create zip files:
	full			| create Full package zip from repo
	microg			| create microG package zip from repo
	fdroid			| create FDroid package zip from repo
	patcher			| create framework patcher zip from repo
	uninstaller		| create uninstaller zip from repo
	setupwizard		| create SetupWizard zip from repo
	bromitewebview		| create Bromite WebView zip from repo
	osmand		| create OsmAnd package zip from repo
	all			| create all zips from repo

misc. stuff:
	check			| check if all files were properly downloaded
	clean			| remove any untracked files from the repo
	dalvik	[jar]		| prepare a jar file for dalvikvm usage
	ver	[ver] [date]	| bump version
	bump			| bump versionCode in Magisk Modules
	u-patch			| download Haystack patches
"

	exit 0
}

print_package () {
		echo "
Creating Package

	Type		${1}
	Name		${ZIP}
	Directory	${CWD}
"

	rm -f "${CWD}/${ZIP}"*
}

error () {
	echo -e "${@}"
	exit 1
}

sum_sign_package () {
	cd "${CWD}"
	cd "$(dirname "${ZIP}")"
	sha256sum "$(basename "${ZIP}")" > "${ZIP}.sha256"
	cd "${CWD}"
	gpg --sign --detach-sign "${ZIP}.sha256"
}
