#!/sbin/sh
# NetHunter kernel installer backend

## start build generated variables
generic="armhf"
## end build generated variables

if [ "$3" ]; then
	ZIPFILE=$3
	console=/proc/$$/fd/$2
	# write the location of the console buffer to /tmp/console for other scripts to use
	echo "$console" > /tmp/console
else
	console=$(cat /tmp/console)
	[ "$console" ] || console=/proc/$$/fd/1
fi

test "$ANDROID_ROOT" || ANDROID_ROOT=/system
SYSTEM="/system"

tmp=/tmp/nethunter/boot-patcher
print() {
	echo "ui_print ${1:- }" > "$console"
	echo
}

abort() {
	[ "$1" ] && {
		print "Error: $1"
		print "Aborting..."
	}
	cleanup
	print "Failed to patch boot image!"
	exit 1
}

cleanup() {
       if [ "$ZIPFILE" ]; then
       rm /tmp/console
       cd $(dirname $tmp)
       rm -rf $tmp
       fi
}

install() {
	setperm "$2" "$3" "$tmp$1"
	if [ "$4" ]; then
		cp -r "$tmp$1" "$(dirname "$4")/"
		return
	fi
	cp -r "$tmp$1" "$(dirname "$1")/"
}

extract() {
	rm -rf "$2"
	mkdir -p "$2"
	unzip -o "$1" -d "$2" ||
		abort "Unable to extract! The zip may be corrupt or your device may not have enough RAM to proceed. Consider using a smaller installer if it is available."
}

setperm() {
	find "$3" -type d -exec chmod "$1" {} \;
	find "$3" -type f -exec chmod "$2" {} \;
}

get_bb() {
    cd $tmp/tools
    BB_latest=`(ls -v busybox_nh-* 2>/dev/null || ls busybox_nh-*) | tail -n 1`
    BB=$tmp/tools/$BB_latest #Use NetHunter Busybox from tools
    chmod 755 $BB #make busybox executable
    echo $BB
    cd - >/dev/null
}


install_recovery_bb() {
    # To identify the latest busybox version, we need the command "ls -v" which is not supported by all twrp versions
    # We will pick the best available busybox version and use that to run "ls -v" to pick the latest version for the recovery partition
    cd $tmp/tools

    # Try to pick the latest version of busybox, if "ls -v" is not supported by the recovery then make an educated guess for a later version
    recovery_bb=`(ls -v busybox_nh-* 2>/dev/null || ls busybox_nh-*) | tail -n 1`
    cp $recovery_bb busybox_tmp
    setperm 0755 0755 busybox_tmp

    # Now that we picked a reasonably current busybox, use that to copy the absolute latest to the recovery partition using "ls -v"
    nethunter_bb=$(./busybox_tmp ls -v busybox_nh-* | tail -n 1)
    print "Installing $nethunter_bb applets to /sbin"
    cp $nethunter_bb /sbin/busybox_nh
    /sbin/busybox_nh --install /sbin
    cd - >/dev/null
}

if [ "$ZIPFILE" ]; then
print "##################################################"
print "##                                              ##"
print "##  88      a8P         db        88        88  ##"
print "##  88    .88'         d88b       88        88  ##"
print "##  88   88'          d8''8b      88        88  ##"
print "##  88 d88           d8'  '8b     88        88  ##"
print "##  8888'88.        d8YaaaaY8b    88        88  ##"
print "##  88P   Y8b      d8''''''''8b   88        88  ##"
print "##  88     '88.   d8'        '8b  88        88  ##"
print "##  88       Y8b d8'          '8b 888888888 88  ##"
print "##                                              ##"
print "####  ############# NetHunter ####################"

else 

print "##################################################"
print "          A/B and A Devices Boot-patcher"
if [ "$generic" ]; then
	print "  for $generic devices - ramdisk modifications only"
fi
print "##################################################"
fi

# Unpack the installer
[ "$ZIPFILE" ] && {
	print "- Unpacking the installer, this may take a while..."
	extract "$ZIPFILE" "$tmp"
	print "- Installer unpacked"
}
cd "$tmp"

BB=$(get_bb)

chmod +x env
. ./env.sh
# Install busybox applets if they haven't been installed by the NetHunter installer already
[ -e /tmp/nethunter/tools/busybox_tmp ] || install_recovery_bb

setperm 0755 0755 tools

if [ "$ZIPFILE" ]; then
print "Running busybox installer..."
sh tools/installbusybox.sh #run only 
print "- Running kernel installer..." 
else                  
print "- Running boot image patcher..."                     
fi
sh "META-INF/com/google/android/update-binary-anykernel" || abort

print "- Boot image patching complete"
# cleanup #dont clean up if not specified in anykernel.sh
