SHARE
TWEET

function.sh modified

a guest Jan 24th, 2020 68 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ##########################################################################################
  2. #
  3. # MMT Extended Utility Functions
  4. #
  5. ##########################################################################################
  6.  
  7. abort() {
  8.   ui_print "$1"
  9.   rm -rf $MODPATH 2>/dev/null
  10.   cleanup
  11.   rm -rf $TMPDIR 2>/dev/null
  12.   exit 1
  13. }
  14.  
  15. cleanup() {
  16.   rm -rf $MODPATH/common 2>/dev/null
  17.   ui_print " "
  18.   ui_print "    **************************************"
  19.   ui_print "    *   MMT Extended by Zackptg5 @ XDA   *"
  20.   ui_print "    **************************************"
  21.   ui_print " "
  22.   $DEBUG && debug_log
  23. }
  24.  
  25. debug_log() {
  26.   set +x
  27.   echo -e "***---Device Info---***" > $LOGFILE-tmp.log
  28.   echo -e "\n---Props---\n" >> $LOGFILE-tmp.log
  29.   getprop >> $LOGFILE-tmp.log
  30.   echo -e "\n\n***---Magisk Info---***" >> $LOGFILE-tmp.log
  31.   echo -e "\n---Magisk Version---\n\n$MAGISK_VER_CODE" >> $LOGFILE-tmp.log
  32.   echo -e "\n---Installed Modules---\n" >> $LOGFILE-tmp.log
  33.   ls $NVBASE/modules >> $LOGFILE-tmp.log
  34.   echo -e "\n---Last Magisk Log---\n" >> $LOGFILE-tmp.log
  35.   cat /cache/magisk.log >> $LOGFILE-tmp.log
  36.   echo -e "\n\n***---MMT Extended Debug Info---***" >> $LOGFILE-tmp.log
  37.   if [ -d "$MODPATH" ]; then
  38.     echo -e "\n---Installed Files---\n" >> $LOGFILE-tmp.log
  39.     grep "^+* cp_ch" $LOGFILE.log | sed 's/.* //g' >> $LOGFILE-tmp.log
  40.     sed -i -e "\|$TMPDIR/|d" -e "\|$MODPATH|d" $LOGFILE-tmp.log
  41.     find $MODPATH -type f >> $LOGFILE-tmp.log
  42.     echo -e "\n---Installed Boot Scripts---\n" >> $LOGFILE-tmp.log
  43.     grep "^+* install_script" $LOGFILE.log | sed -e 's/.* //g' -e 's/^-.* //g' >> $LOGFILE-tmp.log
  44.     echo -e "\n---Installed Prop Files---\n" >> $LOGFILE-tmp.log
  45.     grep "^+* prop_process" $LOGFILE.log | sed 's/.* //g' >> $LOGFILE-tmp.log
  46.   fi
  47.   echo -e "\n---Shell & MMT Extended Variables---\n" >> $LOGFILE-tmp.log
  48.   (set) >> $LOGFILE-tmp.log
  49.   echo -e "\n---(Un)Install Log---\n" >> $LOGFILE-tmp.log
  50.   echo "$(cat $LOGFILE.log)" >> $LOGFILE-tmp.log
  51.   mv -f $LOGFILE-tmp.log $LOGFILE.log
  52. }
  53.  
  54. device_check() {
  55.   local PROP=$(echo "$1" | tr '[:upper:]' '[:lower:]') i
  56.   for i in /system /vendor /odm /product; do
  57.     if [ -f $i/build.prop ]; then
  58.       for j in "ro.product.device" "ro.build.product" "ro.product.vendor.device" "ro.vendor.product.device"; do
  59.         [ "$(sed -n "s/^$j=//p" $i/build.prop 2>/dev/null | head -n 1 | tr '[:upper:]' '[:lower:]')" == "$PROP" ] && return 0
  60.       done
  61.     fi
  62.   done
  63.   return 1
  64. }
  65.  
  66. run_addons() {
  67.   local OPT=`getopt -o mpi -- "$@"` NAME PNAME
  68.   eval set -- "$OPT"
  69.   while true; do
  70.     case "$1" in
  71.       -m) NAME=main; shift;;
  72.       -p) NAME=preinstall; PNAME="Preinstall"; shift;;
  73.       -i) NAME=install; PNAME="Install"; shift;;
  74.       --) shift; break;;
  75.     esac
  76.   done
  77.   if [ "$(ls -A $MODPATH/common/addon/*/$NAME.sh 2>/dev/null)" ]; then
  78.     [ -z $PNAME ] || { ui_print " "; ui_print "- Running $PNAME Addons -"; }
  79.     for i in $MODPATH/common/addon/*/$NAME.sh; do
  80.       ui_print "  Running $(echo $i | sed -r "s|$MODPATH/common/addon/(.*)/$NAME.sh|\1|")..."
  81.       . $i
  82.     done
  83.     [ -z $PNAME ] || { ui_print " "; ui_print "- `echo $PNAME`ing (cont) -"; }
  84.   fi
  85. }
  86.  
  87. cp_ch() {
  88.   local OPT=`getopt -o inr -- "$@"` BAK=true UBAK=true FOL=false
  89.   eval set -- "$OPT"
  90.   while true; do
  91.     case "$1" in
  92.       -n) UBAK=false; shift;;
  93.       -r) FOL=true; shift;;
  94.       --) shift; break;;
  95.       *) abort "Invalid cp_ch argument $1! Aborting!";;
  96.     esac
  97.   done
  98.   local SRC="$1" DEST="$2" OFILES="$1"
  99.   $FOL && local OFILES=$(find $SRC -type f 2>/dev/null)
  100.   [ -z $3 ] && PERM=0644 || PERM=$3
  101.   case "$DEST" in
  102.     $TMPDIR/*|$MODULEROOT/*|$NVBASE/modules/$MODID/*) BAK=false;;
  103.   esac
  104.   for OFILE in ${OFILES}; do
  105.     if $FOL; then
  106.       if [ "$(basename $SRC)" == "$(basename $DEST)" ]; then
  107.         local FILE=$(echo $OFILE | sed "s|$SRC|$DEST|")
  108.       else
  109.         local FILE=$(echo $OFILE | sed "s|$SRC|$DEST/$(basename $SRC)|")
  110.       fi
  111.     else
  112.       [ -d "$DEST" ] && local FILE="$DEST/$(basename $SRC)" || local FILE="$DEST"
  113.     fi
  114.     if $BAK && $UBAK; then
  115.       [ ! "$(grep "$FILE$" $INFO 2>/dev/null)" ] && echo "$FILE" >> $INFO
  116.       [ -f "$FILE" -a ! -f "$FILE~" ] && { mv -f $FILE $FILE~; echo "$FILE~" >> $INFO; }
  117.     elif $BAK; then
  118.       [ ! "$(grep "$FILE$" $INFO 2>/dev/null)" ] && echo "$FILE" >> $INFO
  119.     fi
  120.     install -D -m $PERM "$OFILE" "$FILE"
  121.   done
  122. }
  123.  
  124. install_script() {
  125.   case "$1" in
  126.     -l) shift; local INPATH=$NVBASE/service.d;;
  127.     -p) shift; local INPATH=$NVBASE/post-fs-data.d;;
  128.     *) local INPATH=$NVBASE/service.d;;
  129.   esac
  130.   [ "$(grep "#!/system/bin/sh" $1)" ] || sed -i "1i #!/system/bin/sh" $1
  131.   local i; for i in "MODPATH" "LIBDIR" "MODID" "INFO" "MODDIR"; do
  132.     case $i in
  133.       "MODPATH") sed -i "1a $i=$NVBASE/modules/$MODID" $1;;
  134.       "MODDIR") sed -i "1a $i=\${0%/*}" $1;;
  135.       *) sed -i "1a $i=$(eval echo \$$i)" $1;;
  136.     esac
  137.   done
  138.   [ "$1" == "$MODPATH/uninstall.sh" ] && return 0
  139.   case $(basename $1) in
  140.     post-fs-data.sh|service.sh) ;;
  141.     *) cp_ch -n $1 $INPATH/$(basename $1) 0755;;
  142.   esac
  143. }
  144.  
  145. prop_process() {
  146.   sed -i -e "/^#/d" -e "/^ *$/d" $1
  147.   [ -f $MODPATH/system.prop ] || mktouch $MODPATH/system.prop
  148.   while read LINE; do
  149.     echo "$LINE" >> $MODPATH/system.prop
  150.   done < $1
  151. }
  152.  
  153. # Check for min/max api version
  154. [ -z $MINAPI ] || { [ $API -lt $MINAPI ] && abort "! Your system API of $API is less than the minimum api of $MINAPI! Aborting!"; }
  155. [ -z $MAXAPI ] || { [ $API -gt $MAXAPI ] && abort "! Your system API of $API is greater than the maximum api of $MAXAPI! Aborting!"; }
  156.  
  157. # Set variables
  158. [ $API -lt 26 ] && DYNLIB=false
  159. [ -z $DYNLIB ] && DYNLIB=false
  160. [ -z $DEBUG ] && DEBUG=false
  161. [ -e "$PERSISTDIR" ] && PERSISTMOD=$PERSISTDIR/magisk/$MODID
  162. INFO=$NVBASE/modules/.$MODID-files
  163. ORIGDIR="$MAGISKTMP/mirror"
  164. if $DYNLIB; then
  165.   LIBPATCH="\/vendor"
  166.   LIBDIR=/system/vendor
  167. else
  168.   LIBPATCH="\/system"
  169.   LIBDIR=/system
  170. fi
  171. if ! $BOOTMODE; then
  172.   ui_print "- Only uninstall is supported in recovery"
  173.   ui_print "  Uninstalling!"
  174.   touch $MODPATH/remove
  175.   [ -s $INFO ] && install_script $MODPATH/uninstall.sh || rm -f $INFO $MODPATH/uninstall.sh
  176.   recovery_cleanup
  177.   cleanup
  178.   rm -rf $NVBASE/modules_update/$MODID $TMPDIR 2>/dev/null
  179.   exit 0
  180. fi
  181.  
  182. # Debug
  183. if $DEBUG; then
  184.   ui_print "- Debug mode"
  185.   LOGFILE=/storage/emulated/0/Download/$MODID-debug.log
  186.   ui_print "  Debug log will be written to: $LOGFILEFILE"
  187.   exec 2>$LOGFILEFILE
  188.   set -x
  189. fi
  190.  
  191. # Extract files
  192. ui_print "- Extracting module files"
  193. unzip -o "$ZIPFILE" -x 'META-INF/*' 'common/functions.sh' -d $MODPATH >&2
  194. [ -f "$MODPATH/common/addon.tar.xz" ] && tar -xf $MODPATH/common/addon.tar.xz -C $MODPATH/common 2>/dev/null
  195.  
  196. # Main addons
  197. run_addons -m
  198.  
  199. # Remove files outside of module directory
  200. ui_print "- Removing old files"
  201.  
  202. if [ -f $INFO ]; then
  203.   while read LINE; do
  204.     if [ "$(echo -n $LINE | tail -c 1)" == "~" ]; then
  205.       continue
  206.     elif [ -f "$LINE~" ]; then
  207.       mv -f $LINE~ $LINE
  208.     else
  209.       rm -f $LINE
  210.       while true; do
  211.         LINE=$(dirname $LINE)
  212.         [ "$(ls -A $LINE 2>/dev/null)" ] && break 1 || rm -rf $LINE
  213.       done
  214.     fi
  215.   done < $INFO
  216.   rm -f $INFO
  217. fi
  218.  
  219. ### Install
  220. ui_print "- Installing"
  221.  
  222. run_addons -p
  223. [ -f "$MODPATH/common/install.sh" ] && . $MODPATH/common/install.sh
  224. run_addons -i
  225.  
  226. ui_print "   Installing for $ARCH SDK $API device..."
  227. # Remove comments from files and place them, add blank line to end if not already present
  228. for i in $(find $MODPATH -type f -name "*.sh" -o -name "*.prop" -o -name "*.rule"); do
  229.   [ -f $i ] && { sed -i -e "/^#/d" -e "/^ *$/d" $i; [ "$(tail -1 $i)" ] && echo "" >> $i; } || continue
  230.   case $i in
  231.     "$MODPATH/service.sh") install_script -l $i;;
  232.     "$MODPATH/post-fs-data.sh") install_script -p $i;;
  233.     "$MODPATH/uninstall.sh") [ -s $INFO ] && install_script $MODPATH/uninstall.sh || rm -f $INFO $MODPATH/uninstall.sh;;
  234.   esac
  235. done
  236.  
  237. $IS64BIT || for i in $(find $MODPATH/system -type d -name "lib64"); do rm -rf $i 2>/dev/null; done  
  238. [ -d "/system/priv-app" ] || mv -f $MODPATH/system/priv-app $MODPATH/system/app 2>/dev/null
  239. [ -d "/system/xbin" ] || mv -f $MODPATH/system/xbin $MODPATH/system/bin 2>/dev/null
  240. if $DYNLIB; then
  241.   for FILE in $(find $MODPATH/system/lib* -type f 2>/dev/null | sed "s|$MODPATH/system/||"); do
  242.     [ -s $MODPATH/system/$FILE ] || continue
  243.     case $FILE in
  244.       lib*/modules/*) continue;;
  245.     esac
  246.     mkdir -p $(dirname $MODPATH/system/vendor/$FILE)
  247.     mv -f $MODPATH/system/$FILE $MODPATH/system/vendor/$FILE
  248.     [ "$(ls -A `dirname $MODPATH/system/$FILE`)" ] || rm -rf `dirname $MODPATH/system/$FILE`
  249.   done
  250.   # Delete empty lib folders (busybox find doesn't have this capability)
  251.   toybox find $MODPATH/system/lib* -type d -empty -delete >/dev/null 2>&1
  252. fi
  253.  
  254. # Set permissions
  255. ui_print " "
  256. ui_print "- Setting Permissions"
  257. set_perm_recursive $MODPATH 0 0 0755 0644
  258. set_permissions
  259.  
  260. # Complete install
  261. cleanup
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top