#!/system/bin/sh
# swap artist
# by psyke83 and androidmeda

command=$(getprop persist.sys.swapart)
setprop persist.sys.swapart ""

if [ "$1" != "" ]; then
    # command from shell (debug)
    command=$1
fi

checkswap() {
isswapenabled=$(getprop persist.sys.swap)
ramzswapstatus=$(cat /proc/swaps | grep ramz)
zramstatus=$(cat /proc/swaps | grep zram)
swapstatus=$(cat /proc/swaps | grep mmcblk)
swappriority=$(getprop persist.sys.swap_pri)
swappriority_now=$(echo $swapstatus | awk '{ print $5; }')
if [ "$swappriority" = "" ]; then
    swappriority="0"
fi

if [ "$ramzswapstatus" != "" ] || [ "$zramstatus" != "" ]; then
    iscompcacheon=1
else
    iscompcacheon=0
fi

if [ "$swapstatus" != "" ]; then
    isswapon=1
else
    isswapon=0
fi
}


swap() {
checkswap
# enable
if [ "$isswapenabled" = "1" ] && [ "$isswapon" = "0" ]; then
    echo "Enabling swap..."
    /system/bin/swapon -p $swappriority /dev/block/mmcblk0p3
fi

# disable
if [ "$isswapenabled" != "1" ] && [ "$isswapon" = "1" ]; then
    echo "Disabling swap..."
    /system/bin/swapoff /dev/block/mmcblk0p3
fi

# adjust priority (if needed)
if [ "$isswapon" = "1" ] && [ "$swappriority" != "$swappriority_now" ]; then
    echo "Re-initializing swap with new priority..."
    /system/bin/swapoff /dev/block/mmcblk0p3
    /system/bin/swapon -p $swappriority /dev/block/mmcblk0p3
fi
}

disablezram() {
iszramdisabled=$(getprop persist.sys.disable_zram)
if [ "$iszramdisabled" = "1" ]; then
    /system/bin/swapoff /dev/block/zram0
fi
}

bootcheck() {
# ensure customizations are applied after Android boot completes
swap
disablezram
}

# run command
$command

