#!/sbin/sh

set -e

interface="${1}"
updater_fd="${2}"
zipfile="${3}"

mmr_dir=/tmp/mmr

ui_print() {
    echo -en "ui_print ${1}\n" > /proc/self/fd/"${updater_fd}"
    echo -en "ui_print\n" > /proc/self/fd/"${updater_fd}"
}

abort() {
    ui_print " "
    ui_print "$*"
    ui_print " "
    exit 1
}

install_bb() {
    local magisk_bb=/data/adb/magisk/busybox
    local tmp_bb=/tmp/bb
    [ -f $magisk_bb ] || abort "Error: Please install or upgrade Magisk."
    [ -d $tmp_bb ] && [ -f ${tmp_bb}/test ] || {
        rm -rf $tmp_bb
        mkdir -p $tmp_bb
        ln -s $magisk_bb ${tmp_bb}/busybox
        $magisk_bb --install -s $tmp_bb
    }
    echo $PATH | grep -q "^$tmp_bb" || export PATH=${tmp_bb}:${PATH}
}

# mount & umount
mount /cache || :
mount /data || :
for partition in /system /system_root /vendor /product /odm; do
  mount | grep -q " $partition " && umount $partition
done

# make sure the Magisk internal busybox path is PREPENDED to PATH
install_bb

# check arch
ARCH=arm
ABI=$(getprop ro.product.cpu.abi)
ABI2=$(getprop ro.product.cpu.abi2)
[ "$ABI" = "x86" ] && ARCH="x86"
[ "$ABI" = "x86_64" ] && ARCH="x86_64"
[ "$ABI2" = "x86" ] && ARCH="x86"
[ "$ARCH" = "x86" ] && abort "Error: X86 arch is not supported"
[ "$ARCH" = "x86_64" ] && abort "Error: X64 arch is not supported"
[ "$ABI" = "arm64-v8a" ] && ARCH="arm64"

# find modules path or magisk.img
imagelessPath=/data/adb/modules
imagePath=/data/adb/magisk.img
MAGISK_VER_CODE=$(grep "^MAGISK_VER_CODE=" /data/adb/magisk/util_functions.sh | head -n1 | cut -d= -f2)
[ -z "$MAGISK_VER_CODE" ] && abort "Error: Please install or upgrade Magisk."
ui_print "Installed Magisk version: $MAGISK_VER_CODE"
if [ "$MAGISK_VER_CODE" -gt 18100 ]; then
    mkdir -p $imagelessPath
    imageless_magisk=true
else
    if ! [ -f "$imagePath" ]; then
        ui_print "$imagePath not found."
        ui_print "Creating one with size 64M."
        make_ext4fs -l "64M" $imagePath || abort "Error: magisk.img creation failed."
    fi
    imageless_magisk=false
fi

rm -rf $mmr_dir
mkdir -p $mmr_dir
cd $mmr_dir
unzip "${zipfile}"

chmod 0755 ./bin/*
chmod 0755 ./script/*

. ./script/common.sh

mkdir -p $module_backup_path

if $imageless_magisk; then
    ui_print "Symlink $imagelessPath to $workPath"
    ./script/symlink-magisk.sh $imagelessPath $workPath || abort "Error: Failed to symlink modules path!"
else
    ui_print "Mount $imagePath to $workPath"
    ./script/mount-magisk.sh $imagePath $workPath || abort "Error: Failed to mount $imagePath!"
fi

ui_print "Generating AROMA config"
${mmr_dir}/bin/micropython ${mmr_dir}/script/_gen_operations_edify.py
${mmr_dir}/bin/micropython ${mmr_dir}/script/_gen_magisksu_apps_edify.py
${mmr_dir}/bin/micropython ${mmr_dir}/script/_gen_module_backup_list_edify.py

cd ${mmr_dir}/template
${mmr_dir}/bin/7za a -tzip -mx=0 ${mmr_dir}/aroma.zip ./META-INF || :
[ -f ${mmr_dir}/aroma.zip ] || abort "Error: Cannot generate aroma.zip!"

ui_print "OK. Now starting AROMA"
cd /
chmod 0755 ${mmr_dir}/template/META-INF/com/google/android/update-binary
exec ${mmr_dir}/template/META-INF/com/google/android/update-binary "${interface}" "${updater_fd}" ${mmr_dir}/aroma.zip
