#!/sbin/sh
# Copyright (C) 2018 ATG Droid 
# Copyright (C) 2018-2020 PitchBlack Recovery <pitchblackrecovery@gmail.com>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


AB_DEVICE=false;
OUTFD=/proc/self/fd/$2;
ZIP="$3";
DIR=`dirname "$ZIP"`;

ui_print() {
  until [ ! "$1" ]; do
    echo -e "ui_print $1\nui_print" > $OUTFD ;
    shift;
  done;
}

show_progress() { echo "progress $1 $2" > /proc/self/fd/$2; }
set_perm_recursive() {
  dirs=$(echo $* | awk '{ print substr($0, index($0,$5)) }');
  for i in $dirs; do
    chown -R $1.$2 $i; chown -R $1:$2 $i;
    find "$i" -type d -exec chmod $3 {} +;
    find "$i" -type f -exec chmod $4 {} +;
  done;
}
file_getprop() { grep "^$2" "$1" | cut -d= -f2; }
getprop() { test -e /sbin/getprop && /sbin/getprop $1 || file_getprop /default.prop $1; }
abort() { ui_print "$*"; exit 1; }

if [ -f "/tmp/pb" ]; then
rm -rf "/tmp/pb"
fi

mkdir -p /tmp/pb;
cd /tmp/pb;
unzip -o "$ZIP";
show_progress 0.1000000, 0;
ui_print "|---------------------------------------------|";
ui_print "|---------------------------------------------|";
ui_print "|---------PitchBlack Recovery Project---------|";
ui_print "|-------------------v2.9.1--------------------|";
ui_print "|---------------------------------------------|";
ui_print "|---------------------------------------------|";
ui_print "|--------------Brought To You By--------------|";
ui_print "|---------------------------------------------|";
ui_print "|---------------PitchBlack Team---------------|";
ui_print "|---------------------------------------------|";
ui_print "|---------------------------------------------|";
ui_print "|            Based on TWRP v3.3.1-x           |";
ui_print "|---------------------------------------------|";
/sbin/mount /system;
/sbin/mount /system_root;
PB=/tmp/pb/PBRP
IMG=/tmp/pb/TWRP/recovery.img
RAM=/tmp/pb/TWRP/ramdisk-recovery.cpio
ETC=/system/etc/install-recovery.sh
ETC_=/system_root/$ETC;
ETC__=/system/$ETC;
PB1_PATH=/sdcard/PBTWRP
PB2_PATH=/sdcard/PBRP/tools
PB3_PATH=/sdcard/PBRP/themes
UI=/sdcard/PBRP/theme/ui.zip
RES=/sdcard/PBRP/.twrps
OLD=/sdcard/TWRP/PBRP
red='\033[0;31m'
RECOVERY=/dev/recovery
tool="/tmp/pb/magiskboot"
chmod 0755 $tool
if [[ ! -f "/sbin/awk" ]]; then
cp META-INF/awk /sbin/awk
chmod 0755 /sbin/awk
fi

#AB Checking
if [[ "$AB_DEVICE" = "false" ]]; then
for PARTITION in android_recovery ANDROID_RECOVERY RECOVERY recovery; do
  RECOVERY=$(readlink /dev/block/by-name/$PARTITION || readlink /dev/block/platform/*/by-name/$PARTITION || \
              readlink /dev/block/platform/*/*/by-name/$PARTITION || readlink /dev/$PARTITION);
done;
fi

if [[ "$RECOVERY" == "/dev/recovery" && "$AB_DEVICE" = "false" ]]; then
	DD=true;
else
	DD=false;
fi

#Deletion
	rm $ETC
	rm $ETC_
	rm $ETC__
	rm -rf $PB1_PATH
	rm -rf $PB2_PATH
	rm -rf $PB3_PATH
	rm $UI
	rm -rf $RES
	rm -rf $OLD

#COMMAND=""

AB_D() {
 ui_print "Doing Work on slot $1"
 #Dump & unpack & Cp
 # dd if=/dev/block/bootdevice/by-name/$1 "of=/tmp/boot.img"
 mkdir /tmp/unpack
 dd if=/dev/block/bootdevice/by-name/$1 "of=/tmp/unpack/boot.img"
 cd /tmp/unpack
 "$tool" unpack boot.img
 "$tool" hexpatch kernel 736B69705F696E697472616D667300 77616E745F696E697472616D667300
 cp -f $RAM ramdisk.cpio
 "$tool" repack boot.img
 dd if=new-boot.img of=/dev/block/bootdevice/by-name/$1
 [ $? = 0 ] || abort "Failed to flash $1 Image Please Restore the Boot.img"
 cd /tmp/pb
 rm -rf /tmp/unpack

 #[ $? = 0 ] || abort "Failed to flash $1 Image Please Restore the Boot.img"

}

#Flashing
if [[ "$DD" = "true" ]]; then
	dd if=$IMG of="$RECOVERY"
else
	if [[ "$AB_DEVICE" = "false" ]]; then
		flash_image $RECOVERY $IMG
	else
		AB_D boot_a;
		AB_D boot_b;
	fi
fi

if [[ "$?" != "0" ]]; then
	abort "Failed to write recovery image!" 43;
fi

#Copy Specific Files
if [ -f "/sdcard/PBRP" ]; then
cp -r $PB/tools /sdcard/PBRP/
cp -r $PB/themes /sdcard/PBRP/
else
mkdir -p /sdcard/PBRP
cp -r $PB/tools /sdcard/PBRP/
cp -r $PB/themes /sdcard/PBRP/
fi

#Copy Backup from TWRP
if [ -f "/sdcard/TWRP/BACKUPS" ]; then
ui_print "Previous TWRP Backup found";
if [ -f "/sdcard/PBRP/BACKUPS" ]; then
cp -r /sdcard/TWRP/BACKUPS/* /sdcard/PBRP/BACKUPS/
else 
cp -r /sdcard/TWRP/BACKUPS /sdcard/PBRP/
fi
fi


if [[ "$AB_DEVICE" = "true" ]]; then
cp $RAM /sdcard/PBRP/ramdisk.cpio;
fi

rm -rf /sdcard/TWRP

ui_print "|---------------------------------------------|";
ui_print "|---------------------------------------------|";
ui_print "|---------------------------------------------|";
ui_print "|---------------------------------------------|";
ui_print "|                  Thank You                  |";
if [[ "$AB_DEVICE" = "false" ]]; then
ui_print "|     Reboot into PitchBlack Recovery      |";
else
ui_print "|   Reboot to PitchBlack Recovery Manually    |";
fi
ui_print "|---------------------------------------------|";
sleep 5s
show_progress 0.1000000, 90 ;
ui_print "|------------Installation finished!-----------|";
/sbin/umount /system && return 0;
/sbin/umount /system_root;
