#!/system/bin/sh

BUSYBOX="/data/data/com.paget96.lsandroid/files/binary/busybox"
LOG="/data/data/com.paget96.lsandroid/files/log/mainLog"
ACTIVATED_TWEAKS="/data/data/com.paget96.lsandroid/shared_prefs/act_scripts.xml"
divider="==============================================="

sendToLog() {
  timeZone=$(getprop persist.sys.timezone)
  printDate=$(TZ="$timeZone" $BUSYBOX date +"%H:%M:%S:%3N %d-%m-%Y")

  echo "[$printDate] $1" >>$LOG
}

write() {
  #chmod 0644 "$1"
  echo "$2" >"$1"
}

sendToLog "Activating performance I/O blocks optimization..."

blocks="/sys/block/*"
for i in $blocks; do

	available_scheduler="$(cat "$i/queue/scheduler")"

  case "$available_scheduler" in
  *cfq*)
    write "$i/queue/scheduler" "cfq"
    ;;
  *noop*)
    write "$i/queue/scheduler" "noop"
    ;;
  *kyber*)
    write "$i/queue/scheduler" "kyber"
    ;;
  *bfq*)
    write "$i/queue/scheduler" "bfq"
    ;;
  *mq-deadline*)
    write "$i/queue/scheduler" "mq-deadline"
    ;;
  *none*)
    write "$i/queue/scheduler" "none"
    ;;
  *) echo no ;;
  esac

  #This file allows to turn off the disk entropy contribution. Default
  #value of this file is '1'(on).
  if [ -e "$i/queue/add_random" ]; then
    write "$i/queue/add_random" "0"
    sendToLog "add_random=0 in $i"
  fi

  #This enables the user to disable the lookup logic involved with IO
  #merging requests in the block layer. By default (0) all merges are
  #enabled. When set to 1 only simple one-hit merges will be tried. When
  #set to 2 no merge algorithms will be tried (including one-hit or more
  #complex tree/hash lookups).
  # No need for changing
  #if [ -e "$i/queue/nomerges" ]; then
  #  write "$i/queue/nomerges" "2"
  #  sendToLog "nomerges=2 in $i"
  #fi

  #If this option is '1', the block layer will migrate request completions to the
  #cpu "group" that originally submitted the request. For some workloads this
  #provides a significant reduction in CPU cycles due to caching effects.
  #For storage configurations that need to maximize distribution of completion
  #processing setting this option to '2' forces the completion to run on the
  #requesting cpu (bypassing the "group" aggregation logic).
  if [ -e "$i/queue/rq_affinity" ]; then
    write "$i/queue/rq_affinity" "2"
    sendToLog "rq_affinity=2 in $i"
  fi

  #This controls how many requests may be allocated in the block layer for
  #read or write requests. Note that the total allocated number may be twice
  #this amount, since it applies only to reads or writes (not the accumulated
  #sum).
  #To avoid priority inversion through request starvation, a request
  #queue maintains a separate request pool per each cgroup when
  #CONFIG_BLK_CGROUP is enabled, and this parameter applies to each such
  #per-block-cgroup request pool.  IOW, if there are N block cgroups,
  #each request queue may have up to N request pools, each independently
  #regulated by nr_requests.
  if [ -e "$i/queue/nr_requests" ] && ! $BUSYBOX grep "IO_Extended_Queue" "$ACTIVATED_TWEAKS"; then
    write "$i/queue/nr_requests" "32"
    sendToLog "nr_requests=32 in $i"
  fi

  #Maximum number of kilobytes to read-ahead for filesystems on this block
  #device.
  case $i in
  *"/sys/block/sd"*)
    if [ -e "$i/queue/read_ahead_kb" ]; then
      write "$i/queue/read_ahead_kb" "128"
      sendToLog "read_ahead_kb=128 in $i"
    fi

    ;;
  *"/sys/block/dm-"*)
    if [ -e "$i/queue/read_ahead_kb" ]; then
      write "$i/queue/read_ahead_kb" "128"
      sendToLog "read_ahead_kb=128 in $i"
    fi
    ;;
  "/sys/block/mmcblk0")
    if [ -e "$i/queue/read_ahead_kb" ]; then
      write "$i/queue/read_ahead_kb" "128"
      sendToLog "read_ahead_kb=128 in $i"
    fi
    ;;
  "/sys/block/mmcblk1")
    if [ -e "$i/queue/read_ahead_kb" ]; then
      write "$i/queue/read_ahead_kb" "128"
      sendToLog "read_ahead_kb=128 in $i"
    fi
    ;;
  *)
    if [ -e "$i/queue/read_ahead_kb" ]; then
      write "$i/queue/read_ahead_kb" "64"
      sendToLog "read_ahead_kb=64 in $i"
    fi
    ;;
  esac

done

sendToLog "Activating of performance I/O blocks optimization is successful"
sendToLog "$divider"
