#!/system/bin/sh

APK_DIR=/sdcard/nanodroid_apks
nlx="https://www.nanolx.org/apk"

update_apk () {
	if $(busybox wget --help 2>/dev/null); then
		mkdir -p ${APK_DIR}

		apk=${1}
		apk_p=$(basename ${apk} .apk)
		apk_o=$(basename ${apk})

		if [[ -f ${MODPATH}/system/${apk} ]]; then
			echo -e "\nUpdating: ${apk_p}"

			rm -f ${APK_DIR}/${apk_o}
			busybox wget ${2} -O ${APK_DIR}/${apk_o} || error "failed to download ${apk_p}"
			pm install -r ${APK_DIR}/${apk_o} || error "failed to install ${apk_p}"
		fi
	else
		error "wget is not installed, install Busybox"
	fi
}

error () {
	echo "!! ${@}"
	exit 1
}

[[ $(whoami) != "root" ]] && error "not running as root"

case ${1} in
	ps | playstore )
		update_apk priv-app/Phonesky/Phonesky.apk "${nlx}/Phonesky.apk"
	;;

	#ol | openlauncher )
	#	update_apk app/OpenLauncher/OpenLauncher.apk "${nlx}/OpenLauncher.apk"
	#;;

	mpv | MPV )
		update_apk app/MPV/MPV.apk "${nlx}/MPV.apk"
	;;

	fs | fakestore )
		update_apk priv-app/FakeStore/FakeStore.apk "${nlx}/FakeStore.apk"
	;;

	mg | microG )
		update_apk priv-app/GmsCore/GmsCore.apk "${nlx}/GmsCore.apk"
	;;

	dg | DroidGuard )
		update_apk priv-app/DroidGuard/DroidGuard.apk "${nlx}/DroidGuard.apk"
	;;

	* )
		echo "nanodroid-upd

Utility to update custon NanoDroid apks

Usage: nanodroid-upd <apk>
short	| long apk name

ps	| playstore
fs	| fakestore
mpv	| MPV
mg	| microG
dg	| DroidGuard"
	;;
esac
