# fix sysfs permissions for minfree on Lollipop
chmod 755 /sys/module/lowmemorykiller/parameters/adj;
chmod 755 /sys/module/lowmemorykiller/parameters/minfree;
chmod 755 /sys/module/lowmemorykiller/parameters/lmk_fast_run 2>/dev/null;

P=/sys/module/lowmemorykiller/parameters/minfree;

read() {
  VALUE=0;
  NOW=`cat $P`;
  for LMK in ${NOW//,/ }; do
    VALUE=$((VALUE + 1));
    if [ "$VALUE" == $1 ]; then
      echo $((LMK / 256));
      return;
    fi;
  done;
}

write() {
  SET=$2;
  LMKARR="";
  VALUE=0;
  NOW=`cat $P`;
  for LMK in ${NOW//,/ }; do
    VALUE=$((VALUE + 1));
    if [ "$VALUE" == $1 ]; then
      if [ "$VALUE" == 1 ]; then
        LMKARR="$((SET * 256))";
      else
        LMKARR="$LMKARR $((SET * 256))";
      fi;
    else
      if [ "$VALUE" == 1 ]; then
        LMKARR="$LMK";
      else
        LMKARR="$LMKARR $LMK";
      fi;
    fi;
  done;
  echo ${LMKARR// /,} > $P;
}

if [[ ! -z $2 ]]; then
  write $1 $2;
fi;

echo `read $1`;

