#!/sbin/sh

umask 022
OUTFD=${2}
ZIP=${3}

mkdir /dev/tmp
unzip -oq "${ZIP}" CommonPatcher -d "/dev/tmp"
if [ ! -f /dev/tmp/CommonPatcher ]; then
	echo " !! failed to load CommonPatcher"
	exit 1
else
	source "/dev/tmp/CommonPatcher"
fi

detect_bootmode

setup_environment
BASEDIR="${TMPDIR}/dexpatcher"

detect_outfd

show_banner

mount_partitions

if [ -f /data/adb/.nanodroid-patcher ]; then
	ui_print " !! /data/adb/.nanodroid-patcher exists"
	ui_print " !! if you want to force patching, then"
	error    " remove this file and try again"
elif [ -f /data/adb/NanoDroid_Patched ]; then
	ui_print " !! /data/adb/NanoDroid_Patched exists"
	ui_print " !! if you want to force patching, then"
	error    " remove this file and try again"
fi

setup_patcher

detect_sdk
detect_arch

if search_fake_package_signature; then
	error    " ROM has native signature spoofing already!"
fi

detect_odex

patch_services
install_services
install_addond

if ! ${BOOTMODE} ; then
	umount -l /system_root 2>/dev/null
	umount -l /system 2>/dev/null
	umount -l /vendor 2>/dev/null
	umount -l /dev/random 2>/dev/null
fi

ui_print " "
ui_print " > Done!"
ui_print " "
ui_print "Thanks for using NanoDroid"
ui_print " "

exit 0
