#!/bin/bash

# Our default user's UID is 32011
MAIN_UID=32011

# Disable VMA read-ahead to reduce unnecessary memory usage
echo "false" > /sys/kernel/mm/swap/vma_ra_enabled

# Prefer swapping cold anonymous memory to zram to avoid OOM
sysctl -w vm.swappiness=150

# Don't do swap read-ahead (zram isn't a real block device)
sysctl -w vm.page-cluster=0

# Allow FuriOS user to access flashlight
[ -e "/sys/class/flashlight/mt-flash-led1/" ] && chown $MAIN_UID:$MAIN_UID /sys/class/flashlight/mt-flash-led1/*
[ -e "/sys/class/flashlight/mt-flash-led2/" ] && chown $MAIN_UID:$MAIN_UID /sys/class/flashlight/mt-flash-led2/*

while [ ! -e /sys/class/ieee80211/phy0 ]; do
    sleep 1
done

# Disable wmt driver debugging
echo "0xFF:0x03" > /proc/net/wlan/dbgLevel

# Disable statsd since its unused
setprop ctl.stop statsd

# Wait for binder nodes to come up and give them the right permission
while [ ! -e /dev/binderfs/anbox-binder ]; do
    sleep 1
done

chmod 666 /dev/binderfs/anbox-*

chown $MAIN_UID:$MAIN_UID /dev/binderfs/anbox-*

# Wait for vibrator driver to come up and give it the right permission
while [ ! -e /sys/class/leds/vibrator/activate ]; do
    sleep 1
done

chown $MAIN_UID:$MAIN_UID /sys/class/leds/vibrator/*

# Allow to access brightness nodes
[ -e "/sys/class/flashlight_core/flashlight/" ] && chown $MAIN_UID:$MAIN_UID /sys/class/flashlight_core/flashlight/*
[ -e "/sys/class/leds/lcd-backlight/" ] && chown $MAIN_UID:$MAIN_UID /sys/class/leds/lcd-backlight/*

# Disable coredumps
sysctl -w kernel.core_pattern=|/bin/false

# Helps with VoLTE. Some carriers incorrectly send default route in the RA on
# the VoLTE network, so IPv6 traffic will try to go to VoLTE interface and fail.
# For internet access, NetworManager should step in and set default route on the
# correct interface; NM itself sets `accept_ra=0` and parses RA itself so this
# should not affect it.

sysctl -w net.ipv6.conf.all.accept_ra_defrtr=0
sysctl -w net.ipv6.conf.default.accept_ra_defrtr=0

# Make sure all nodes for call audio get the right permission
# This used to be set by udev during init, but it doesn't hurt to make sure they are correct
# These are needed for call audio to be routed correctly
chown radio:radio /dev/ccci_0_200
chmod 0660 /dev/ccci_0_200
chown radio:radio /dev/ccci_0_202
chmod 0660 /dev/ccci_0_202
chown radio:radio /dev/ccci_0_204
chmod 0660 /dev/ccci_0_204
chown system:audio /dev/ccci_aud
chmod 0660 /dev/ccci_aud
chown radio:radio /dev/ccci_bip
chmod 0660 /dev/ccci_bip
chown radio:radio /dev/ccci_c2k_agps
chmod 0660 /dev/ccci_c2k_agps
chown radio:radio /dev/ccci_c2k_ppp
chmod 0660 /dev/ccci_c2k_ppp
chown radio:radio /dev/ccci_ccb_ctrl
chmod 0660 /dev/ccci_ccb_ctrl
chown radio:radio /dev/ccci_ccb_dhl
chmod 0660 /dev/ccci_ccb_dhl
chown radio:radio /dev/ccci_ccb_md_monitor
chmod 0660 /dev/ccci_ccb_md_monitor
chown radio:radio /dev/ccci_ccb_meta
chmod 0660 /dev/ccci_ccb_meta
chown radio:radio /dev/ccci_epdg1
chmod 0660 /dev/ccci_epdg1
chown radio:radio /dev/ccci_epdg2
chmod 0660 /dev/ccci_epdg2
chown radio:radio /dev/ccci_epdg3
chmod 0660 /dev/ccci_epdg3
chown radio:radio /dev/ccci_epdg4
chmod 0660 /dev/ccci_epdg4
chown radio:radio /dev/ccci_fs
chmod 0660 /dev/ccci_fs
chown radio:radio /dev/ccci_ikeraw
chmod 0660 /dev/ccci_ikeraw
chown radio:radio /dev/ccci_imsa
chmod 0660 /dev/ccci_imsa
chown radio:radio /dev/ccci_imsc
chmod 0660 /dev/ccci_imsc
chown radio:radio /dev/ccci_imsdc
chmod 0660 /dev/ccci_imsdc
chown radio:radio /dev/ccci_imsem
chmod 0660 /dev/ccci_imsem
chown radio:radio /dev/ccci_imsm
chmod 0660 /dev/ccci_imsm
chown radio:radio /dev/ccci_imsv
chmod 0660 /dev/ccci_imsv
chown radio:radio /dev/ccci_ioctl0
chmod 0660 /dev/ccci_ioctl0
chown radio:radio /dev/ccci_ioctl1
chmod 0660 /dev/ccci_ioctl1
chown radio:radio /dev/ccci_ioctl2
chmod 0660 /dev/ccci_ioctl2
chown radio:radio /dev/ccci_ioctl3
chmod 0660 /dev/ccci_ioctl3
chown radio:radio /dev/ccci_ioctl4
chmod 0660 /dev/ccci_ioctl4
chown radio:radio /dev/ccci_ipc_1220_0
chmod 0660 /dev/ccci_ipc_1220_0
chown radio:radio /dev/ccci_ipc_2
chmod 0660 /dev/ccci_ipc_2
chown radio:radio /dev/ccci_ipc_4
chmod 0660 /dev/ccci_ipc_4
chown radio:radio /dev/ccci_ipc_5
chmod 0660 /dev/ccci_ipc_5
chown radio:radio /dev/ccci_ipc_9
chmod 0660 /dev/ccci_ipc_9
chown radio:radio /dev/ccci_it
chmod 0660 /dev/ccci_it
chown radio:radio /dev/ccci_lb_it
chmod 0660 /dev/ccci_lb_it
chown radio:radio /dev/ccci_md1_sta
chmod 0660 /dev/ccci_md1_sta
chown radio:radio /dev/ccci_md2_sta
chmod 0660 /dev/ccci_md2_sta
chown radio:radio /dev/ccci_md3_sta
chmod 0660 /dev/ccci_md3_sta
chown radio:radio /dev/ccci_mdl_monitor
chmod 0660 /dev/ccci_mdl_monitor
chown radio:radio /dev/ccci_md_log_ctrl
chmod 0660 /dev/ccci_md_log_ctrl
chown radio:radio /dev/ccci_mdx_sta
chmod 0660 /dev/ccci_mdx_sta
chown radio:radio /dev/ccci_monitor
chmod 0660 /dev/ccci_monitor
chown radio:radio /dev/ccci_pin_sta
chmod 0660 /dev/ccci_pin_sta
chown system:audio /dev/ccci_raw_audio
chmod 0660 /dev/ccci_raw_audio
chown radio:radio /dev/ccci_raw_dhl
chmod 0660 /dev/ccci_raw_dhl
chown radio:radio /dev/ccci_raw_mdm
chmod 0660 /dev/ccci_raw_mdm
chown radio:radio /dev/ccci_raw_netd
chmod 0660 /dev/ccci_raw_netd
chown radio:radio /dev/ccci_raw_usb
chmod 0660 /dev/ccci_raw_usb
chown radio:radio /dev/ccci_rpc
chmod 0660 /dev/ccci_rpc
chown radio:radio /dev/ccci_ss_xcap
chmod 0660 /dev/ccci_ss_xcap
chown radio:radio /dev/ccci_vts
chmod 0660 /dev/ccci_vts
chown radio:radio /dev/ccci_wifi_proxy
chmod 0660 /dev/ccci_wifi_proxy
chown radio:radio /dev/ccci_woa
chmod 0660 /dev/ccci_woa

chmod 0666 /dev/ion

# Make systemd happy
chmod 444 /proc/cmdline

# Performance tweaks
mkdir -p /sys/fs/cgroup/schedtune

mount -t cgroup -o schedtune stune /sys/fs/cgroup/schedtune

[ -f /proc/gpufreq/gpufreq_opp_stress_test ] && echo 0 > /proc/gpufreq/gpufreq_opp_stress_test
[ -f /proc/ppm/enabled ] && echo 0 > /proc/ppm/enabled # messes up cpu scaling frequency and offline cores
[ -f /proc/m4u_dbg/log_level ] && echo 0 > /proc/m4u_dbg/log_level

exit 0
