#!/system/bin/sh

dir=${0%/*}
tmpLspdDex="/data/local/tmp/lspd.dex"
debug="false"

if [ -r $tmpLspdDex ]; then
  java_options="-Djava.class.path=$tmpLspdDex"
  java_options="$java_options -Dlsp.library.path=/data/local/tmp"
  debug="true"
elif [ -d "$dir/system" ]; then
  java_options="-Djava.class.path=$dir/system/framework/lspd.dex"
  java_options="$java_options -Dlsp.library.path=$dir"
  debug="true"
else
  java_options="-Djava.class.path=$dir/framework/lspd.dex"
  java_options="$java_options -Dlsp.library.path=$dir"
fi

if [ $debug = "true" ]; then
  os_version=$(getprop ro.build.version.sdk)
  if [ "$os_version" -eq "27" ]; then
    java_options="$java_options -Xrunjdwp:transport=dt_android_adb,suspend=n,server=y -Xcompiler-option --debuggable"
  elif [ "$os_version" -eq "28" ]; then
    java_options="$java_options -XjdwpProvider:adbconnection -XjdwpOptions:suspend=n,server=y -Xcompiler-option --debuggable"
  else
    java_options="$java_options -XjdwpProvider:adbconnection -XjdwpOptions:suspend=n,server=y"
  fi
fi

# shellcheck disable=SC2086
exec /system/bin/app_process $java_options /system/bin --nice-name=lspd org.lsposed.lspd.core.Main "$@" >/dev/null 2>&1
