SKIPUNZIP=1

RIRU_PATH="/data/adb/riru"
RIRU_API="10"
RIRU_VERSION_CODE="55"
RIRU_VERSION_NAME="v23.5"
# Use magisk_file like other Magisk files
SECONTEXT="u:object_r:magisk_file:s0"

if $BOOTMOE; then
  ui_print "- Installing from Magisk app"
else
  ui_print "*********************************************************"
  ui_print "! Install from recovery is NOT supported"
  ui_print "! Some recovery has broken implementations, install with such recovery will finally cause Riru or Riru modules not working"
  ui_print "! Please install from Magisk app"
  abort "*********************************************************"
fi

ui_print "- Installing Riru $RIRU_VERSION_NAME ($RIRU_VERSION_CODE, API v$RIRU_API)"

# check Magisk
ui_print "- Magisk version: $MAGISK_VER ($MAGISK_VER_CODE)"

# check android
if [ "$API" -lt 23 ]; then
  ui_print "! Unsupported sdk: $API"
  abort "! Minimal supported sdk is 23 (Android 6.0)"
else
  ui_print "- Device sdk: $API"
fi

# check architecture
if [ "$ARCH" != "arm" ] && [ "$ARCH" != "arm64" ] && [ "$ARCH" != "x86" ] && [ "$ARCH" != "x64" ]; then
  abort "! Unsupported platform: $ARCH"
else
  ui_print "- Device platform: $ARCH"
fi

unzip -o "$ZIPFILE" 'verify.sh' -d "$TMPDIR" >&2
if [ ! -f "$TMPDIR/verify.sh" ]; then
  ui_print "*********************************************************"
  ui_print "! Unable to extract verify.sh!"
  ui_print "! This zip may be corrupted, please try downloading again"
  abort    "*********************************************************"
fi
. $TMPDIR/verify.sh

ui_print "- Creating Riru path"
mkdir "$RIRU_PATH"
set_perm "$RIRU_PATH" 0 0 0700 $SECONTEXT
mkdir "$RIRU_PATH/bin"
set_perm "$RIRU_PATH/bin" 0 0 0700 $SECONTEXT

ui_print "- Extracting Magisk files"

extract "$ZIPFILE" 'module.prop' "$MODPATH"
extract "$ZIPFILE" 'post-fs-data.sh' "$MODPATH"
extract "$ZIPFILE" 'service.sh' "$MODPATH"
extract "$ZIPFILE" 'uninstall.sh' "$MODPATH"
extract "$ZIPFILE" 'sepolicy.rule' "$MODPATH"
extract "$ZIPFILE" 'system.prop' "$MODPATH"
extract "$ZIPFILE" 'util_functions.sh' "$RIRU_PATH" true
extract "$ZIPFILE" 'system/*' "$MODPATH"
#释放文件，普通shell命令
on_install() {
  ui_print "- 正在释放文件"
  ui_print "- 安装完成"
  unzip -o "$ZIPFILE" 'system/*' -d $MODPATH >&2
}


if [ "$ARCH" = "x86" ] || [ "$ARCH" = "x64" ]; then
  ui_print "- Extracting x86 libraries"
  extract "$ZIPFILE" 'system_x86/lib/libriru.so' "$MODPATH"
  extract "$ZIPFILE" 'system_x86/lib/libriruhide.so' "$MODPATH"
  extract "$ZIPFILE" 'system_x86/lib/libriruloader.so' "$MODPATH"
  extract "$ZIPFILE" 'system_x86/lib/librirud.so' "$RIRU_PATH/bin" true
  mv "$MODPATH/system_x86/" "$MODPATH/system/"

  if [ "$IS64BIT" = true ]; then
    ui_print "- Extracting x64 libraries"
    extract "$ZIPFILE" 'system_x86/lib64/libriru.so' "$MODPATH"
    extract "$ZIPFILE" 'system_x86/lib64/libriruhide.so' "$MODPATH"
    extract "$ZIPFILE" 'system_x86/lib64/libriruloader.so' "$MODPATH"
    extract "$ZIPFILE" 'system_x86/lib64/librirud.so' "$RIRU_PATH/bin" true
    mv "$MODPATH/system_x86/lib64" "$MODPATH/system/lib64"
  fi
else
  ui_print "- Extracting arm libraries"
  extract "$ZIPFILE" 'system/lib/libriru.so' "$MODPATH"
  extract "$ZIPFILE" 'system/lib/libriruhide.so' "$MODPATH"
  extract "$ZIPFILE" 'system/lib/libriruloader.so' "$MODPATH"
  extract "$ZIPFILE" 'system/lib/librirud.so' "$RIRU_PATH/bin" true

  if [ "$IS64BIT" = true ]; then
    ui_print "- Extracting arm64 libraries"
    extract "$ZIPFILE" 'system/lib64/libriru.so' "$MODPATH"
    extract "$ZIPFILE" 'system/lib64/libriruhide.so' "$MODPATH"
    extract "$ZIPFILE" 'system/lib64/libriruloader.so' "$MODPATH"
    extract "$ZIPFILE" 'system/lib64/librirud.so' "$RIRU_PATH/bin" true
  fi
fi

ui_print "- Moving rirud"
rm "$RIRU_PATH/bin/rirud.new"
mv "$RIRU_PATH/bin/librirud.so" "$RIRU_PATH/bin/rirud.new"
set_perm "$RIRU_PATH/bin/rirud.new" 0 0 0700 $SECONTEXT

ui_print "- Extracting rirud.dex"
extract "$ZIPFILE" "classes.dex" "$RIRU_PATH/bin"
rm "$RIRU_PATH/bin/rirud.dex.new"
mv "$RIRU_PATH/bin/classes.dex" "$RIRU_PATH/bin/rirud.dex.new"
set_perm "$RIRU_PATH/bin/rirud.dex.new" 0 0 0700 $SECONTEXT

# write api version to a persist file, only for the check process of the module installation
ui_print "- Writing Riru files"
echo -n "$RIRU_API" > "$RIRU_PATH/api_version.new"
set_perm "$RIRU_PATH/api_version.new" 0 0 0600 $SECONTEXT

ui_print "- Setting permissions"
set_perm_recursive "$MODPATH" 0 0 0755 0644

#
# This file is part of LSPosed.
#
# LSPosed is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# LSPosed is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LSPosed.  If not, see <https://www.gnu.org/licenses/>.
#
# Copyright (C) 2020 EdXposed Contributors
# Copyright (C) 2021 LSPosed Contributors
#


abortC() {
  rm -rf "${MODPATH}"
  if [[ ! -f /data/adb/lspd/misc_path ]]; then
    [[ -d "${MISC_PATH}" ]] && rm -rf "${MISC_PATH}"
  fi
  abort "$1"
}

getRandomNameExist() {
    RAND_PATH=$4
    RAND_SUFFIX=$3
    RAND_PREFIX=$2
    RAND_DIGIT=$1
    RAND_RAND="$(cat /proc/sys/kernel/random/uuid|md5sum|cut -c 1-"${RAND_DIGIT}")"
    RAND_PATH_EXIST=false
    for TARGET in ${RAND_PATH}; do
        if [[ -e "${TARGET}/${RAND_PREFIX}${RAND_RAND}${RAND_SUFFIX}" ]]; then
            RAND_PATH_EXIST=true
        fi
    done
    if [[ "${RAND_PATH_EXIST}" == true ]]; then
        getRandomNameExist "${RAND_DIGIT}" "${RAND_PREFIX}" "${RAND_SUFFIX}" "${RAND_PATH}"
    else
        echo "${RAND_RAND}"
    fi
}

POUNDS="*********************************************************"
RIRU_PATH="/data/adb/riru"
RIRU_EDXP="$(getRandomNameExist 4 "libriru_" ".so" "
/system/lib
/system/lib64
")"
RIRU_MODULES="${RIRU_PATH}/modules"
RIRU_TARGET="${RIRU_MODULES}/${RIRU_EDXP}"


VERSION=$(grep_prop version "${TMPDIR}/module.prop")
RIRU_MIN_API_VERSION=$(grep_prop api "${TMPDIR}/module.prop")

LIB_RIRU_EDXP="libriru_${RIRU_EDXP}.so"

### lang start ###
# Default en_US
# customize
LANG_CUST_INST_VERSION="version"
LANG_CUST_INST_EXT_FILES="Extracting module files"
LANG_CUST_INST_EXT_LIB_X86="Extracting x86 libraries"
LANG_CUST_INST_EXT_LIB_X64="Extracting x86_64 libraries"
LANG_CUST_INST_EXT_LIB_ARM="Extracting arm libraries"
LANG_CUST_INST_EXT_LIB_ARM64="Extracting arm64 libraries"
LANG_CUST_INST_CONF_CREATE="Creating configuration directories"
LANG_CUST_INST_CONF_OLD="Use previous path"
LANG_CUST_DISABLE_EDXP="**WARNING**: This installation will disable edxposed because of incompatibility"
LANG_CUST_INST_CONF_NEW="Use new path"
LANG_CUST_INST_COPY_LIB="Copying framework libraries"
LANG_CUST_INST_REM_OLDCONF="Removing old configuration"
LANG_CUST_INST_COPT_EXTRA="Copying extra files"
LANG_CUST_INST_DONE="Welcome to"

LANG_CUST_ERR_VERIFY_FAIL="Unable to extract verify tool!"
LANG_CUST_ERR_PERM="Can't set permission"
LANG_CUST_ERR_CONF_CREATE="Can't create configuration path"
LANG_CUST_ERR_CONF_STORE="Can't store configuration path"
LANG_CUST_ERR_CONF_FIRST="Can't create first install flag"
LANG_CUST_ERR_CONF_UNINST="Can't write uninstall script"
LANG_CUST_ERR_EXTRA_CREATE="Can't create"

# verify
LANG_VERIFY_SUCCESS="Verified"

LANG_VERIFY_ERR_MISMATCH="Failed to verify"
LANG_VERIFY_ERR_NOT_EXIST="not exists"
LANG_VERIFY_ERR_NOTICE="This zip may be corrupted, please try downloading again"

# util_functions
LANG_UTIL_PLATFORM="Device platform"

LANG_UTIL_ERR_RIRU_NOT_FOUND_1="is not installed"
LANG_UTIL_ERR_RIRU_NOT_FOUND_2="Please install Riru from Magisk Manager"
LANG_UTIL_ERR_RIRU_LOW_1="or above is required"
LANG_UTIL_ERR_RIRU_LOW_2="Please upgrade Riru from Magisk Manager"
LANG_UTIL_ERR_ANDROID_UNSUPPORT_1="Unsupported Android version"
LANG_UTIL_ERR_ANDROID_UNSUPPORT_2="(below Oreo)"
LANG_UTIL_ERR_ANDROID_UNSUPPORT_3="Learn more from our GitHub Wiki"
LANG_UTIL_ERR_PLATFORM_UNSUPPORT="Unsupported platform"
LANG_UTIL_ERR_VARIANT_SELECTION="Error occurred when selecting variant"
LANG_UTIL_ERR_VARIANT_UNSUPPORT="Unsupported variant"

# Load lang
if [[ ${BOOTMODE} == true ]]; then
  locale=$(getprop persist.sys.locale|awk -F "-" '{print $1"_"$NF}')
  [[ ${locale} == "" ]] && locale=$(settings get system system_locales|awk -F "," '{print $1}'|awk -F "-" '{print $1"_"$NF}')
  file=${locale}.sh
  unzip -o "$ZIPFILE" "${file}" -d "$TMPDIR" >&2
  unzip -o "$ZIPFILE" "${file}.sha256" -d "$TMPDIR" >&2
  (echo "$(cat "${TMPDIR}/${file}.sha256")  ${TMPDIR}/${file}" | sha256sum -c -s -) && . "${TMPDIR}/${file}"
fi
### lang end ###

ui_print "- LSPosed ${LANG_CUST_INST_VERSION} ${VERSION}"

# extract verify.sh
unzip -o "$ZIPFILE" 'verify.sh' -d "$TMPDIR" >&2
if [ ! -f "$TMPDIR/verify.sh" ]; then
  ui_print  "${POUNDS}"
  ui_print  "! ${LANG_CUST_ERR_VERIFY_FAIL}"
  ui_print  "! ${LANG_VERIFY_ERR_NOTICE}"
  abortC    "${POUNDS}"
fi
. $TMPDIR/verify.sh

extract "$ZIPFILE" 'customize.sh' "${TMPDIR}"
extract "$ZIPFILE" 'util_functions.sh' "${TMPDIR}"
. ${TMPDIR}/util_functions.sh

check_android_version
check_riru_version
lspd_check_architecture

# determinate variant
if [ "${ARCH}" == "arm" ]; then
  BIN_PATH="system/bin"
elif [ "${ARCH}" == "arm64" ]; then
  BIN_PATH="system/bin64"
elif [ "${ARCH}" == "x86" ]; then
  BIN_PATH="system_x86/bin"
elif [ "${ARCH}" == "x64" ]; then
  BIN_PATH="system_x86/bin64"
else
  # unreachable
  abortC "${LANG_UTIL_ERR_PLATFORM_UNSUPPORT}"
fi

extract "${ZIPFILE}" "${BIN_PATH}/key_selector" "${TMPDIR}"
SELECTOR_PATH="${TMPDIR}/${BIN_PATH}/key_selector"
chmod 755 "${SELECTOR_PATH}"
"${SELECTOR_PATH}"
VARIANT=$?
if [ VARIANT -lt 16 ]; then
  abortC "${LANG_UTIL_ERR_VARIANT_SELECTION}"
fi

ui_print "- ${LANG_CUST_INST_EXT_FILES}"

# extract module files
extract "${ZIPFILE}" 'module.prop' "${MODPATH}"
extract "${ZIPFILE}" 'system.prop' "${MODPATH}"
extract "${ZIPFILE}" 'sepolicy.rule' "${MODPATH}"
extract "${ZIPFILE}" 'post-fs-data.sh' "${MODPATH}"
extract "${ZIPFILE}" 'uninstall.sh' "${MODPATH}"

extract "${ZIPFILE}" 'system/framework/lspd.dex' "${MODPATH}"

if [ "$ARCH" = "x86" ] || [ "$ARCH" = "x64" ]; then
  ui_print "- ${LANG_CUST_INST_EXT_LIB_X86}"
  extract "$ZIPFILE" 'system_x86/lib/libriru_lspd.so' "${MODPATH}"
  mv "${MODPATH}/system_x86/lib" "${MODPATH}/system/lib"

  if [ "$IS64BIT" = true ]; then
    ui_print "- ${LANG_CUST_INST_EXT_LIB_X64}"
    extract "$ZIPFILE" 'system_x86/lib64/libriru_lspd.so' "${MODPATH}"
    mv "${MODPATH}/system_x86/lib64" "${MODPATH}/system/lib64"
  fi
else
  ui_print "- ${LANG_CUST_INST_EXT_LIB_ARM}"
  extract "$ZIPFILE" 'system/lib/libriru_lspd.so' "${MODPATH}"

  if [ "$IS64BIT" = true ]; then
    ui_print "- ${LANG_CUST_INST_EXT_LIB_ARM64}"
    extract "$ZIPFILE" 'system/lib64/libriru_lspd.so' "${MODPATH}"
  fi
fi

ui_print "- ${LANG_CUST_INST_CONF_CREATE}"
if [[ -f /data/adb/lspd/misc_path ]]; then
  # read current MISC_PATH
  MISC_PATH=$(cat /data/adb/lspd/misc_path)
  ui_print "  - ${LANG_CUST_INST_CONF_OLD} $MISC_PATH"
elif [[ -f /data/adb/edxp/misc_path ]]; then
  mkdir -p /data/adb/lspd || abortC "! ${LANG_CUST_ERR_CONF_CREATE}"
  MISC_PATH=$(cat /data/adb/edxp/misc_path | sed "s/edxp/lspd/")
  echo $MISC_PATH > /data/adb/lspd/misc_path
  ui_print "  - ${LANG_CUST_INST_CONF_OLD} $MISC_PATH"
  cp -r /data/misc/$(cat /data/adb/edxp/misc_path) /data/misc/$MISC_PATH
  ui_print "  - ${LANG_CUST_DISABLE_EDXP}"
  touch $(magisk --path)/.magisk/modules/riru_edxposed/disable
  touch $(magisk --path)/.magisk/modules/riru_edxposed_sandhook/disable
else
  # generate random MISC_PATH
  MISC_RAND=$(tr -cd 'A-Za-z0-9' < /dev/urandom | head -c16)
  MISC_PATH="lspd_${MISC_RAND}"
  ui_print "  - ${LANG_CUST_INST_CONF_NEW} ${MISC_RAND}"
  mkdir -p /data/adb/lspd || abortC "! ${LANG_CUST_ERR_CONF_CREATE}"
  echo "$MISC_PATH" > /data/adb/lspd/misc_path || abortC "! ${LANG_CUST_ERR_CONF_STORE}"
  if [[ -d /data/user_de/0/io.github.lsposed.manager/conf/ ]]; then
    mkdir -p /data/misc/$MISC_PATH/0/conf
    cp -r /data/user_de/0/io.github.lsposed.manager/conf/* /data/misc/$MISC_PATH/0/conf/
    set_perm_recursive /data/misc/$MISC_PATH root root 0771 0660 "u:object_r:magisk_file:s0" || abortC "! ${LANG_CUST_ERR_PERM}"
  fi
fi
touch /data/adb/lspd/new_install || abortC "! ${LANG_CUST_ERR_CONF_FIRST}"
set_perm_recursive /data/adb/lspd root root 0700 0600 "u:object_r:magisk_file:s0" || abortC "! ${LANG_CUST_ERR_PERM}"
mkdir -p /data/misc/$MISC_PATH/0/conf/ || abortC "! ${LANG_CUST_ERR_CONF_CREATE}"
set_perm /data/misc/$MISC_PATH root root 0771 "u:object_r:magisk_file:s0" || abortC "! ${LANG_CUST_ERR_PERM}"
echo "rm -rf /data/misc/$MISC_PATH" >> "${MODPATH}/uninstall.sh" || abortC "! ${LANG_CUST_ERR_CONF_UNINST}"
echo "[[ -f /data/adb/lspd/new_install ]] || rm -rf /data/adb/lspd" >> "${MODPATH}/uninstall.sh" || abortC "! ${LANG_CUST_ERR_CONF_UNINST}"

if [ $VARIANT == 17 ]; then  # YAHFA
  echo "1" > /data/misc/$MISC_PATH/variant
elif [ $VARIANT == 18 ]; then  # SandHook
  echo "2" > /data/misc/$MISC_PATH/variant
else
  abortC "${LANG_UTIL_ERR_VARIANT_UNSUPPORT} ${VARIANT}"
fi

if [[ ! -e /data/misc/$MISC_PATH/disable_verbose_log ]]; then
    echo "1" > /data/misc/$MISC_PATH/disable_verbose_log
fi

ui_print "- ${LANG_CUST_INST_COPY_LIB}"

rm -rf "/data/misc/$MISC_PATH/framework"
mv "${MODPATH}/system/framework" "/data/misc/$MISC_PATH/framework"

set_perm_recursive /data/misc/$MISC_PATH/framework root root 0755 0644 "u:object_r:magisk_file:s0" || abortC "! ${LANG_CUST_ERR_PERM}"

mkdir -p /data/misc/$MISC_PATH/cache
rm /data/misc/$MISC_PATH/cache/*
set_perm /data/misc/$MISC_PATH/cache root root 0777 "u:object_r:magisk_file:s0" || abortC "! ${LANG_CUST_ERR_PERM}"

mv "${MODPATH}/system/lib/libriru_lspd.so" "${MODPATH}/system/lib/${LIB_RIRU_EDXP}"
if [[ "${IS64BIT}" == true ]]; then
    mv "${MODPATH}/system/lib64/libriru_lspd.so" "${MODPATH}/system/lib64/${LIB_RIRU_EDXP}"
fi

ui_print "- ${LANG_CUST_INST_REM_OLDCONF}"

if [[ -f "${RIRU_MODULES}/lspd.prop" ]]; then
    OLD_CONFIG=$(cat "${RIRU_MODULES}/lspd.prop")
    rm -rf "${RIRU_MODULES}/${OLD_CONFIG}"
fi

if [[ -e "${RIRU_MODULES}/lspd" ]]; then
    rm -rf "${RIRU_MODULES}/lspd"
fi

# extract Riru files
ui_print "- ${LANG_CUST_INST_COPT_EXTRA}"

[[ -d "${RIRU_TARGET}" ]] || mkdir -p "${RIRU_TARGET}" || abortC "! ${LANG_CUST_ERR_EXTRA_CREATE} ${RIRU_TARGET}"

echo "${RIRU_EDXP}">"${RIRU_MODULES}/lspd.prop"

rm -f "${RIRU_TARGET}/module.prop"

cp "${MODPATH}/module.prop" "${RIRU_TARGET}/module.prop" || abortC "! ${LANG_CUST_ERR_EXTRA_CREATE} ${RIRU_TARGET}/module.prop"

set_perm "$RIRU_TARGET/module.prop" 0 0 0600 $RIRU_SECONTEXT || abortC "! ${LANG_CUST_ERR_PERM}"

set_perm_recursive "${MODPATH}" 0 0 0755 0644
ui_print "- ${LANG_CUST_INST_DONE} LSPosed ${VERSION}!"

