#!/sbin/sh
# NetHunter kernel installer backend

## start build generated variables
kernel_string="NetHunter kernel"
kernel_author="jcadduono"
kernel_version="1.5"
device_names="herolte heroltexx heroltecan"
generic=
## end build generated variables

if [ "$3" ]; then
	zip=$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

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() {
	rm /system/.rw
	rm /data/.rw

	umount /system

	[ "$zip" ] && rm /tmp/console
}

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" {} \;
}

mount() {
	mountpoint -q "$1" || /sbin/busybox mount -o rw "$1" || abort "Unable to mount $1 as rw!"
	>> "$1/.rw" && return || /sbin/busybox mount -o remount,rw "$1"
	>> "$1/.rw" && return || abort "Unable to write to $1!"
}

print "##################################################"
print "            NetHunter Kernel Installer"
if [ "$generic" ]; then
	print "  for $generic devices - ramdisk modifications only"
else
	print "   Kernel: $kernel_string"
	print "  Version: $kernel_version"
	print "   Author: $kernel_author"
fi
print "##################################################"

[ "$device_names" ] && {
	if ! command -v getprop; then
		print "Warning: getprop not found! Skipping device check!"
		return
	fi
	print "Checking device compatibility..."
	match=0
	ro_product_device=$(getprop ro.product.device)
	ro_product_model=$(getprop ro.product.model)
	ro_build_product=$(getprop ro.build.product)
	for i in $device_names; do
		[ "$ro_product_device" = "$i" ] ||
		[ "$ro_product_model" = "$i" ] ||
		[ "$ro_build_product" = "$i" ] &&
			match=1
	done
	[ $match != 1 ] && abort "Unsupported device"
}

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

. env.sh

mount /system
mount /data

setperm 0755 0755 tools

# Install additional busybox applets to /sbin in case something is missing during installation
print "Installing busybox applets to /sbin"
cp tools/busybox /sbin/busybox_nh
/sbin/busybox_nh --install /sbin

[ -f tools/freespace.sh ] && {
	# This actually runs twice when part of the NetHunter updater zip
	print "Freeing up some space on /system"
	sh tools/freespace.sh ||
		abort "Not enough free space on /system to continue!"
}

[ -d system/etc/firmware ] && {
	print "Copying firmware to /system/etc/firmware"
	install "/system/etc/firmware" 0755 0644
}

[ -d system/etc/init.d ] && {
	print "Copying init.d scripts to /system/etc/init.d"
	install "/system/etc/init.d" 0755 0755
}

[ -d system/lib ] && {
	print "Copying 32-bit shared libraries to /system/lib"
	install "/system/lib" 0755 0644
}

[ -d system/lib64 ] && {
	print "Copying 64-bit shared libraries to /system/lib64"
	install "/system/lib64" 0755 0644
}

[ -d system/bin ] && {
	print "Installing /system/bin binaries"
	install "/system/bin" 0755 0755
}

[ -d system/xbin ] && {
	print "Installing /system/xbin binaries"
	install "/system/xbin" 0755 0755
}

[ -d data/local ] && {
	print "Copying additional files to /data/local"
	install "/data/local" 0755 0644
}

print "Running busybox installer..."
sh tools/installbusybox.sh

print "Running boot image patcher..."
sh boot-patcher.sh || abort

cleanup
print "Boot image patching complete"
