#!/sbin/sh
# Kali NetHunter installer

## Detect bootmode.
## "BOOTMODE=true"  = Device has booted into system (NetHunter installated as Magisk module)
## "BOOTMODE=false" = Device has booted into recovery (NetHunter installed through TWRP)
BOOTMODE=false;
ps | grep zygote | grep -v grep >/dev/null && BOOTMODE=true;
$BOOTMODE || ps -A 2>/dev/null | grep zygote | grep -v grep >/dev/null && BOOTMODE=true;


###################################
##                               ##
##  Install in Magisk            ##
##                               ##
###################################

if $BOOTMODE; then

    #################
    # Initialization
    #################

    umask 022

    require_new_magisk() {
        echo "*******************************"
        echo " Please install Magisk v20.4+! "
        echo "*******************************"
        exit 1
    }

    #########################
    # Load util_functions.sh
    #########################

    OUTFD=$2
    ZIPFILE=$3

    mount /data 2>/dev/null

    [ -f /data/adb/magisk/util_functions.sh ] || require_new_magisk
    . /data/adb/magisk/util_functions.sh
    [ $MAGISK_VER_CODE -lt 20400 ] && require_new_magisk

    #########################
    # Install module
    #########################
    rm -rf $TMPDIR
    mkdir -p $TMPDIR

    setup_flashable
    mount_partitions
    api_level_arch_detect

    # Setup busybox and binaries
    boot_actions

    # Extract prop file
    unzip -jo "$ZIPFILE" tools/module.prop -d $TMPDIR >&2
    [ ! -f $TMPDIR/module.prop ] && abort "! Unable to extract zip file!"

    MODDIRNAME=modules_update
    MODID=`grep_prop id $TMPDIR/module.prop`
    MODNAME=`grep_prop name $TMPDIR/module.prop`
    MODAUTH=`grep_prop author $TMPDIR/module.prop`
    MODPATH=$NVBASE/$MODDIRNAME/$MODID

    # Create mod paths
    rm -rf $MODPATH
    mkdir -p $MODPATH

    ## Install NetHunter
    unzip -jo "$ZIPFILE" META-INF/com/google/android/update-magisk -d $MODPATH >&2
    set_perm_recursive $MODPATH 0 0 0755 0644
    [ -f $MODPATH/update-magisk ] && . $MODPATH/update-magisk

    # Clean up
    rm -rf $MODPATH/update-magisk \
    $MODPATH/system/placeholder $MODPATH/customize.sh \
    $MODPATH/README.md $MODPATH/.git*
    rm -rf $TMPDIR
    cd /

    exit 0
fi

###################################
##                               ##
##  Install in TWRP              ##
##                               ##
###################################

## start build generated variables
supersu=
## 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
tmp=/tmp/nethunter
patchtmp=$tmp/boot-patcher
export home=$patchtmp
sutmp=/tmp/supersu

progress() {
	echo "set_progress $1" > "$console"
}

print() {
	echo "ui_print ${1:- }" > "$console"
	echo
}

abort() {
	[ "$1" ] && {
		print "Error: $1"
		print "Aborting..."
	}
	restore_env
	print "Failed to install Kali NetHunter!"
	exit 1
}

cleanup() {
    [ "$ZIPFILE" ] && {
       rm /tmp/console 
    }
    cd $(dirname $tmp)
    rm -rf $tmp
}

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

# installapp "App Name" "appfile.apk" "play.store.package.name"
# -> if the app package is found in the play store app database, then don't install it
installapp() {
	installto=/data/app/
	if [ "$3" ]; then
		for appdir in "/data/app/$3-"*; do
			[ -d "$appdir" ] || continue
			echo "Found app directory: $appdir"
			if [ ! -f /data/data/com.android.vending/databases/localappstate.db ]; then
				echo "Could not find Play Store app database!"
			# this should also catch paid/alternative versions if they are suffixed
			elif strings /data/data/com.android.vending/databases/localappstate.db | grep -q "^$3"; then
				rm -f "/data/app/$2"
				print "- Found Play Store installed $1"
				return 0
			fi
			rm -f "/data/app/$2"
			installto=$appdir/base.apk
			break
		done
	fi
	echo "Installing $1 to $installto"
	print "- Installing $1"
	cp -f "$tmp/data/app/$2" "$installto" && return 0
	print "- Failed to install $1!" && return 1
}

extract() {
	rm -rf "$2"
	mkdir -p "$2"
	unzip -o "$1" -d "$2" -x "$3" ||
		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" {} \;
}

symlink() {
	rm "$2"
	ln -s "$1" "$2"
}

setup_mountpoint() {
  [ -L $1 ] && $BB mv -f $1 ${1}_link;
  if [ ! -d $1 ]; then
    $BB rm -f $1;
    $BB mkdir -p $1;
  fi;
}
is_mounted() { $BB mount | $BB grep -q " $1 "; }
mount_apex() {
  [ -d /system_root/system/apex ] || return 1;
  local apex dest loop minorx num var;
  setup_mountpoint /apex;
  minorx=1;
  [ -e /dev/block/loop1 ] && minorx=$($BB ls -l /dev/block/loop1 | $BB awk '{ print $6 }');
  num=0;
  for apex in /system_root/system/apex/*; do
    dest=/apex/$($BB basename $apex .apex);
    case $dest in
      *.current|*.release) dest=$(echo $dest | $BB rev | $BB cut -d. -f2- | $BB rev);;
    esac;
    $BB mkdir -p $dest;
    case $apex in
      *.apex)
        $BB unzip -qo $apex apex_payload.img -d /apex;
        $BB mv -f /apex/apex_payload.img $dest.img;
        $BB mount -t ext4 -o ro,noatime $dest.img $dest 2>/dev/null;
        if [ $? != 0 ]; then
          while [ $num -lt 64 ]; do
            loop=/dev/block/loop$num;
            ($BB mknod $loop b 7 $((num * minorx));
            $BB losetup $loop $dest.img) 2>/dev/null;
            num=$((num + 1));
            $BB losetup $loop | $BB grep -q $dest.img && break;
          done;
          $BB mount -t ext4 -o ro,loop,noatime $loop $dest;
          if [ $? != 0 ]; then
            $BB losetup -d $loop 2>/dev/null;
          fi;
        fi;
      ;;
      *) $BB mount -o bind $apex $dest;;
    esac;
  done;
  for var in $($BB grep -o 'export .* /.*' /system_root/init.environ.rc | $BB awk '{ print $2 }'); do
    eval OLD_${var}=\$$var;
  done;
  $($BB grep -o 'export .* /.*' /system_root/init.environ.rc | $BB sed 's; /;=/;'); unset export;
}
umount_apex() {
  [ -d /apex/com.android.runtime ] || return 1;
  local dest loop var;
  for var in $($BB grep -o 'export .* /.*' /system_root/init.environ.rc | $BB awk '{ print $2 }'); do
    if [ "$(eval echo \$OLD_$var)" ]; then
      eval $var=\$OLD_${var};
    else
      eval unset $var;
    fi;
    unset OLD_${var};
  done;
  for dest in $($BB find /apex -type d -mindepth 1 -maxdepth 1); do
    if [ -f $dest.img ]; then
      loop=$($BB mount | $BB grep $dest | $BB cut -d\  -f1);
    fi;
    ($BB umount -l $dest;
    $BB losetup -d $loop) 2>/dev/null;
  done;
  $BB rm -rf /apex 2>/dev/null;
}

find_slot() {
  local slot=$(getprop ro.boot.slot_suffix 2>/dev/null);
  [ "$slot" ] || slot=$($BB grep -o 'androidboot.slot_suffix=.*$' /proc/cmdline | $BB cut -d\  -f1 | $BB cut -d= -f2);
  if [ ! "$slot" ]; then
    slot=$(getprop ro.boot.slot 2>/dev/null);
    [ "$slot" ] || slot=$($BB grep -o 'androidboot.slot=.*$' /proc/cmdline | $BB cut -d\  -f1 | $BB cut -d= -f2);
    [ "$slot" ] && slot=_$slot;
  fi;
  [ "$slot" ] && echo "$slot";
}

mount_all() {
  if ! is_mounted /cache; then
    $BB mount /cache 2>/dev/null
  fi;
  if ! is_mounted /data; then
    $BB mount /data
  fi;
  ($BB mount -o ro -t auto /vendor;
  $BB mount -o ro -t auto /product;
  $BB mount -o ro -t auto /persist) 2>/dev/null;
  setup_mountpoint $ANDROID_ROOT;
  if ! is_mounted $ANDROID_ROOT; then
    $BB mount -o ro -t auto $ANDROID_ROOT 2>/dev/null;
  fi;
  case $ANDROID_ROOT in
    /system_root) setup_mountpoint /system;;
    /system)
      if ! is_mounted /system && ! is_mounted /system_root; then
        setup_mountpoint /system_root;
        $BB mount -o ro -t auto /system_root;
      elif [ -f /system/system/build.prop ]; then
        setup_mountpoint /system_root;
        $BB mount --move /system /system_root;
      fi;
      if [ $? != 0 ]; then
        ($BB umount /system;
        $BB umount -l /system) 2>/dev/null;
        if [ -d /dev/block/mapper ]; then
          [ -e /dev/block/mapper/system ] || local slot=$(find_slot);
          $BB mount -o ro -t auto /dev/block/mapper/vendor$slot /vendor;
          $BB mount -o ro -t auto /dev/block/mapper/product$slot /product 2>/dev/null;
          $BB mount -o ro -t auto /dev/block/mapper/system$slot /system_root;
        else
          [ -e /dev/block/bootdevice/by-name/system ] || local slot=$(find_slot);
          ($BB mount -o ro -t auto /dev/block/bootdevice/by-name/vendor$slot /vendor;
          $BB mount -o ro -t auto /dev/block/bootdevice/by-name/product$slot /product;
          $BB mount -o ro -t auto /dev/block/bootdevice/by-name/persist$slot /persist) 2>/dev/null;
          $BB mount -o ro -t auto /dev/block/bootdevice/by-name/system$slot /system_root;
        fi;
      fi;
    ;;
  esac;
  if is_mounted /system_root; then
    mount_apex;
    if [ -f /system_root/build.prop ]; then
      $BB mount -o bind /system_root /system;
    else
      $BB mount -o bind /system_root/system /system;
    fi;
  fi;
}
umount_all() {
  local mount;
  (if is_mounted /system; then
    $BB umount /system;
    $BB umount -l /system;
  fi) 2>/dev/null;
  umount_apex;
  (if [ -e /system_root ]; then
    $BB umount /system_root;
    $BB umount -l /system_root;
  fi;
  umount /vendor; # busybox umount /vendor breaks recovery on some hacky devices
  umount -l /vendor;
  for mount in /mnt/system /mnt/vendor /product /mnt/product /persist; do
    $BB umount $mount;
    $BB umount -l $mount;
  done;
  if is_mounted /data; then
    $BB umount /data;
    $BB umount -l /data;
  fi;
  if is_mounted /cache; then
    $BB umount /cache;
    $BB umount -l /cache;
  fi) 2>/dev/null;
}
setup_env() {
  $BB mount -o bind /dev/urandom /dev/random;
  if [ -L /etc ]; then
    setup_mountpoint /etc;
    $BB cp -af /etc_link/* /etc;
    $BB sed -i 's; / ; /system_root ;' /etc/fstab;
  fi;
  umount_all;
  mount_all;
  if [ -d /dev/block/mapper ]; then
    for slot in "" _a _b; do
      $BB blockdev --setrw /dev/block/mapper/system$slot 2>/dev/null
    done
  fi
  $BB mount -o rw,remount -t auto /system 2>/dev/null || $BB mount -o rw,remount -t auto / 2>/dev/null
  OLD_LD_PATH=$LD_LIBRARY_PATH;
  OLD_LD_PRE=$LD_PRELOAD;
  OLD_LD_CFG=$LD_CONFIG_FILE;
  unset LD_LIBRARY_PATH LD_PRELOAD LD_CONFIG_FILE;
}
restore_env() {
  local dir;
  [ "$OLD_LD_PATH" ] && export LD_LIBRARY_PATH=$OLD_LD_PATH;
  [ "$OLD_LD_PRE" ] && export LD_PRELOAD=$OLD_LD_PRE;
  [ "$OLD_LD_CFG" ] && export LD_CONFIG_FILE=$OLD_LD_CFG;
  unset OLD_LD_PATH OLD_LD_PRE OLD_LD_CFG;
  umount_all;
  [ -L /etc_link ] && $BB rm -rf /etc/*;
  (for dir in /apex /system /system_root /etc; do
    if [ -L "${dir}_link" ]; then
      rmdir $dir;
      $BB mv -f ${dir}_link $dir;
    fi;
  done;
  $BB umount -l /dev/random) 2>/dev/null; 

  print "Cleaning Up..."
  cleanup
}

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
}

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 ####################"

# Unpack the installer
[ "$ZIPFILE" ] && {
	print "Unpacking the installer..."
	extract "$ZIPFILE" "$tmp" "kalifs-*"
}
cd "$tmp"

. ./env.sh

[ -e /dev/block/mapper ] && {
    print " "
    print "********** Dynamic Device Detected **********"
    print " "
    print "This NetHunter package cannot be"
    print "installed on this device via recovery"
    print "Please Flash this file via Magisk Manager"
    print "as Module And ensure, You have USB DEBUGGING"
    print "enabled for using ADB"
    print " "
    print "******* NetHunter Installation failed *******"
    cleanup 
    exit 1
}


progress 0.0
print "Starting the installation"

setperm 0755 0755 tools
BB=$(get_bb) #get latest busybox
install_recovery_bb

setup_env #setup environment for nethunter installation
SYSTEM="/system" 

progress 0.1

[ -d /data/data ] || {
	abort "Your data partition appears to be empty. Please complete the Android setup wizard before installing Kali NetHunter!"
}


print "Checking for previous versions of NetHunter"
sh tools/previnstall.sh

progress 0.2

[ -f supersu.zip ] && {
	print "Extracting SuperSU zip..."
	extract supersu.zip "$sutmp"

	progress 0.3

	sh tools/installsu.sh "$sutmp" "$supersu"

}

progress 0.4

SDK="$(grep 'ro.build.version.sdk' ${SYSTEM}/build.prop | cut -d'=' -f2)"
print "SDK Version: $SDK"

print "Installing apps:"

if [ $SDK -ge 26 ]; then
    # Starting with Oreo we can no longer install user apps so we install NetHunter.apk as system app
    print "- Installing NetHunter.apk"
    mkdir -p ${SYSTEM}/app/NetHunter
    cp $tmp/data/app/NetHunter.apk ${SYSTEM}/app/NetHunter/
    # and NetHunterTerminal.apk because nethunter.apk depends on it
    print "- Installing NetHunterTerminal.apk"
    mkdir -p ${SYSTEM}/app/NetHunter-Terminal
    cp $tmp/data/app/NetHunterTerminal.apk ${SYSTEM}/app/NetHunter-Terminal/
    unzip -qo ${SYSTEM}/app/NetHunter-Terminal/NetHunterTerminal.apk "lib/*" -d ${SYSTEM}/app/NetHunter-Terminal/
    ## Some newer TWRP versions ship an unzip that does not support the above line so we might need plan B
    [ -d ${SYSTEM}/app/NetHunter-Terminal/lib ] || {
        mkdir -p /tmp/NetHunter-Terminal/
        unzip -qo ${SYSTEM}/app/NetHunter-Terminal/NetHunterTerminal.apk -d /tmp/NetHunter-Terminal/
        mv /tmp/NetHunter-Terminal/lib ${SYSTEM}/app/NetHunter-Terminal/
    }
    mv ${SYSTEM}/app/NetHunter-Terminal/lib/armeabi-v7a ${SYSTEM}/app/NetHunter-Terminal/lib/arm
    mv ${SYSTEM}/app/NetHunter-Terminal/lib/arm64-v8a ${SYSTEM}/app/NetHunter-Terminal/lib/arm64

    # and NetHunterKeX.apk because nethunter.apk depends on it
    print "- Installing NetHunter-KeX.apk"
    mkdir -p ${SYSTEM}/app/NetHunter-KeX
    cp $tmp/data/app/NetHunterKeX.apk ${SYSTEM}/app/NetHunter-KeX/
    unzip -qo ${SYSTEM}/app/NetHunter-KeX/NetHunterKeX.apk "lib/*" -d ${SYSTEM}/app/NetHunter-KeX/
    ## Some newer TWRP versions ship an unzip that does not support the above line so we might need plan B
    [ -d ${SYSTEM}/app/NetHunter-KeX/lib ] || {
        mkdir -p /tmp/NetHunter-KeX/
        unzip -qo ${SYSTEM}/app/NetHunter-KeX/NetHunterKeX.apk -d /tmp/NetHunter-KeX/
        mv /tmp/NetHunter-KeX/lib ${SYSTEM}/app/NetHunter-KeX/
    }
    mv ${SYSTEM}/app/NetHunter-KeX/lib/armeabi-v7a ${SYSTEM}/app/NetHunter-KeX/lib/arm
    mv ${SYSTEM}/app/NetHunter-KeX/lib/arm64-v8a ${SYSTEM}/app/NetHunter-KeX/lib/arm64


    # and NetHunterStore.apk because we need it 
    print "- Installing NetHunter-Store.apk"
    mkdir -p ${SYSTEM}/app/NetHunter-Store
    cp $tmp/data/app/NetHunterStore.apk ${SYSTEM}/app/NetHunter-Store/

else
    installapp "NetHunter App" "NetHunter.apk" "com.offsec.nethunter"
    installapp "NetHunter Terminal" "NetHunterTerminal.apk" "com.offsec.nhterm"
    installapp "NetHunter KeX" "NetHunterKeX.apk" "com.offsec.nethunter.kex"
    installapp "NetHunter Store" "NetHunterStore.apk" "com.offsec.nethunter.store"
fi
## Installing privileged extension
print "- Installing NetHunterStorePrivilegedExtension.apk"
mkdir -p ${SYSTEM}/priv-app/NetHunterStorePrivilegedExtension
cp $tmp/data/app/NetHunterStorePrivilegedExtension.apk ${SYSTEM}/priv-app/NetHunterStorePrivilegedExtension/
if [ $SDK -ge 26 ]
then
	mkdir ${SYSTEM}/etc/permissions 
	chmod 755 ${SYSTEM}/etc/permissions 
        [ -f system/etc/permissions/com.offsec.nethunter.store.privileged.xml ] && {
            install "/system/etc/permissions/com.offsec.nethunter.store.privileged.xml" 0755 0644 "${SYSTEM}/etc/permissions/com.offsec.nethunter.store.privileged.xml"
        }

fi
print "Done installing apps"

progress 0.5

[ -f tools/freespace.sh ] && {
	# This actually runs twice when the NetHunter kernel zip is included
 sh tools/freespace.sh || abort "Not enough free space on ${SYSTEM} to continue!"
 
}

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

progress 0.6

[ -d wallpaper ] && {
	print "Installing NetHunter wallpaper"
	sh wallpaper/setwallpaper.sh
}

[ -f system/media/bootanimation.zip ] && {
	print "Installing NetHunter boot animation"
	install "/system/media/bootanimation.zip" 0755 0644 "${SYSTEM}/media/bootanimation.zip"
}

progress 0.7

[ -d system/etc/nano ] && {
	print "Copying nano highlights to ${SYSTEM}/etc/nano"
	install "/system/etc/nano" 0755 0644 "${SYSTEM}/etc/nano" 
}

[ -d system/etc/terminfo ] && {
	print "Copying terminfo files to ${SYSTEM}/etc/terminfo"
	install "/system/etc/terminfo" 0755 0644 "${SYSTEM}/etc/terminfo"
}

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

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

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

[ -d system/xbin ] && {
	print "Installing ${SYSTEM}/xbin binaries"
        [ -d ${SYSTEM}/xbin ] || mkdir -p ${SYSTEM}/xbin
	install "/system/xbin" 0755 0755 "${SYSTEM}/xbin"
}

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

[ -d system/etc/init.d ] && {
	print "Installing init.d scripts"
	install "/system/etc/init.d" 0755 0755 "${SYSTEM}/etc/init.d"
	# Create userinit.d and userinit.sh if they don't already exist
	mkdir -p "/data/local/userinit.d"
	setperm 0755 0755 "/data/local/userinit.d"
	[ -f "/data/local/userinit.sh" ] || echo "#!/system/bin/sh" > "/data/local/userinit.sh"
	chmod 0755 "/data/local/userinit.sh"
}
 
[ -d system/addon.d/80-nethunter.sh ] && {
		print "Installing ${SYSTEM}/addon.d backup scripts"
	install "/system/80-nethunter.sh" 0755 0755 "${SYSTEM}/80-nethunter.sh"
}

print "Symlinking Kali boot scripts"
symlink "/data/data/com.offsec.nethunter/files/scripts/bootkali" "${SYSTEM}/bin/bootkali"
symlink "/data/data/com.offsec.nethunter/files/scripts/bootkali_init" "${SYSTEM}/bin/bootkali_init"
symlink "/data/data/com.offsec.nethunter/files/scripts/bootkali_login" "${SYSTEM}/bin/bootkali_login"
symlink "/data/data/com.offsec.nethunter/files/scripts/bootkali_bash" "${SYSTEM}/bin/bootkali_bash"
symlink "/data/data/com.offsec.nethunter/files/scripts/killkali" "${SYSTEM}/bin/killkali"

progress 0.8

[ -d "$patchtmp" ] && {
	print "Running kernel installer..."
	sh "$patchtmp/META-INF/com/google/android/update-binary"
        # setup environment again (all are restored by boot-patcher)
        setup_env
}

progress 0.9

print "Running Kali chroot installer..."
sh "$tmp/tools/installchroot.sh" "$ZIPFILE"

restore_env #restore environment after installation

print " "
print "************************************************"
print "*       Kali NetHunter is now installed!       *"
print "*==============================================*"
print "*   Please update the NetHunter app via the    *"
print "*   NetHunter Store to work around an Android  *"
print "*   permission issue and run the NetHunter app *"
print "*       to finish setting everything up!       *"
print "************************************************"
print " "

progress 1.0
