#!/system/bin/sh
# Original script and ALL credit goes to EarlyMon @ XDA, the original author of the script
# All rights recognised
# Creative Commons Attribution-ShareAlike (CC BY-SA)
# Comparable to the GPL, used by Wikipedia
# Free to reuse and build derivatives
# and license your derivatives under the same terms


# Usage: no arguments
# Run at init, late service, in a Magisk module


# This script is designed to be a universal swap assassin
# Yes, that includes zram, zram is just one type of swapregion
# But it's not limited to only zram
# And it's not limited to just a single swapregion
# The swapregion(s) must be block or logical device(s)
# swapoff is required, toybox version is fine
# awk is required, toybox is ok if it has it

# Returns
#   0 - no swap, or no swap in /dev, or time's up

# Exit if no swap in use,

ROOTPATH=/sbin/.core/img
XTRVID=redmi_note5_xtrv_tweaks

#Suicide logic in case the module is removed

if [ ! -d "$ROOTPATH/$XTRVID" ]; then
 rm -f /data/adb/service.d/ZZWipeswap
 exit 0
fi;

# Do not disable swap if Total Memory is below 4GB, just exit
MEM2="$(($(awk '/MemTotal/{print $2}' /proc/meminfo)))"
if [ $MEM2 -lt 3500000 ]; then
 #debug
 #echo $MEM2 | tee -a /cache/test.log;
 sleep 40
 sysctl -e -w vm.swappiness=40
 exit 0
fi

alias SWAPT='grep -i SwapTotal /proc/meminfo | tr -d "[a-zA-Z :]"'

TL=45
Step=3
k=0

while [ `SWAPT` -eq 0  ]
do
    k=$(( $k + $Step ))
    if [ $k -gt $TL  ] ; then
        exit 0
    fi
    sleep $Step
done

SR="\/dev\/"
PS="/proc/swap*"

if [ -f /system/bin/swapoff ] ; then
    SO="/system/bin/swapoff"
else
    SO="swapoff"
fi

# You would think that there's only ever zram0
# And you would be wrong
# Samsung had a different method at least once (vmswap) 
# HTC used four zram swap partitions at least once
# They can't even all agree if it's swap or swaps in /proc
# Find all swapregions and target each one for swapoff
# Don't assume it's in the first field of swaps, find it

DIE=`awk -v SBD="$SR" ' $0 ~ SBD {
      for ( i=1;i<=NF;i++ )
        {
          if ( $i ~ ( "^" SBD ) )
           {
              printf "%s;", $i
           }
        }
      }' $PS`

saveifs=$IFS
IFS=';'

# I could have put all this in awk and just eval'd it 
# But where's the fun in that

for i in $DIE
do
    case $i in
        *zram*)
              j=`echo $i | sed 's/.*zram//'`
             ( ( 
                 echo $j > /sys/class/zram-control/hot_remove
                 echo 1 > /sys/block/zram${j}/reset
                 $SO $i
              ) & )
              ;;
        *)
              if [ -n $i ]; then
                  ( ( $SO $i ) & ) 
              fi
              ;;
    esac
done

IFS=$saveifs

# Enjoy a better Android experience, and be kind to someone

exit 0