#!/bin/bash

NANODROID_MISSING_FILES=0
missing_database=""

check_nanodroid () {
	for file in ${files_database[@]}; do
		if [[ ! -f ${CWD}/${file} ]]; then
			NANODROID_MISSING_FILES=1
			missing_database=(${missing_databse[@]} ${file})
		fi
	done

	if [[ ${NANODROID_MISSING_FILES} -ne 0 ]]; then
		rm -f ${CWD}/.nanodroid-pull
		echo -e "\nThe following files are missing:\n"
		printf '%s\n' ${missing_database[@]}
	else	echo -e "\nNanoDroid correctly populated\n"
	fi
}

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 () {
	case ${1} in
		*/ )	zip -qr "${ZIP}" ${1} || exit 1 ;;
		"" )	zip -qr "${ZIP}" *    || exit 1 ;;
		*  )	zip -q "${ZIP}" ${1}  || exit 1 ;;
	esac
}

zip_add_config () {
	if [ -f "${CWD}/data/${1}" ]; then
		cd "${CWD}/data"
	else	cd "${CWD}/doc"
	fi

	if [ "${2}" -eq 1 ]; then
		sed -e 's/nanodroid_overlay=1/nanodroid_overlay=0/' -i "${1}"
		zip_add "${1}"
		sed -e 's/nanodroid_overlay=0/nanodroid_overlay=1/' -i "${1}"
	else
		zip_add "${1}"
	fi

	cd "${CWD}"
}

zip_add_doc () {
	zip_add README.md
	zip_add ChangeLog.md
}

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

	sed -e "s/^VERSION=.*/VERSION=${2}.${ver}/" -i "${CWD}/build-package"
	sed -e "s/^VERSION=.*/VERSION=${2}.${ver}/" -i "${CWD}/CommonInstaller"

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

	for module in uninstaller patcher; do
		sed -e "s/\"      NanoDroid.*/\"      NanoDroid ${2}.${ver}     \"/" -i \
			"${CWD}/${module}/META-INF/com/google/android/update-binary"
	done

	sed -e "s/rom_version\",.*/rom_version\",	\"${2}\")\;/" -i \
		"${CWD}/setup-wizard/META-INF/com/google/android/aroma-config"
	sed -e "s/rom_date\",.*/rom_date\",	\"${ver}\")\;/" -i \
		"${CWD}/setup-wizard/META-INF/com/google/android/aroma-config"
	sed -e "s/Wizard.*/Wizard ${2}.${ver}\")\;/" -i \
		"${CWD}/setup-wizard/META-INF/com/google/android/updater-script"
	sed -e "s/\"     NanoDroid.*/\"     NanoDroid ${2}.${ver}    \"/" -i \
		"${CWD}/setup-wizard/nanodroid-setup"
	sed -e "s/\"     addon.d.*/\"     addon.d ${2}.${ver}     \"/" -i \
		"${CWD}"/patcher/dexpatcher/75-nanodroidpatcher.sh

	exit 0
}

increase_module_version () {
	for module in Full microG F-Droid; 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/26.0.2/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
				|| required for: Full, F-Droid, microG packages

update contents:
	u-microg		| update microG and companions
				|| except: microG GmsCore, Play Store
	u-fdroid		| update F-Droid and Extension
	u-apps			| update applications
	u-swipe			| update swipe libraries
	u-gsync			| update Google Sync Adapters
				|| except: Android Oreo / SDK 26

create zip files:
	full			| create full package zip from repo
	microg			| create microG package zip from repo
	fdroid			| create F-Droid package zip from repo
	patcher			| create framework patcher zip from repo
	uninstaller		| create uninstaller zip from repo
	setupwizard		| create setup-wizard 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
"

	exit 0
}

print_package () {
		echo "
Creating Package

	Type		${1}
	Name		$(basename ${ZIP})
	Directory	$(dirname ${ZIP})
"
}

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