#!/bin/sh

PATH='/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin'

LOG_DIR=/var/opt/maccel/logs
mkdir -p $LOG_DIR

# Setting `maccel` group for some sysfs resources
chown -v :maccel /sys/module/maccel/parameters/* &>$LOG_DIR/chown
chown -v :maccel /dev/maccel &>$LOG_DIR/chown
chmod g+r /dev/maccel &>$LOG_DIR/chmod

# For persisting parameters values across reboots
RESET_SCRIPTS_DIR=/var/opt/maccel/resets
mkdir -p $RESET_SCRIPTS_DIR &>$LOG_DIR/reset-scripts
chown -v :maccel $RESET_SCRIPTS_DIR &>$LOG_DIR/reset-scripts
chmod -v g+w "$RESET_SCRIPTS_DIR" &>$LOG_DIR/reset-scripts

for script in $(ls $RESET_SCRIPTS_DIR/set_last_*_value.sh); do
  cat $script | sh &>$LOG_DIR/reset-scripts
  chown -v :maccel $script &>$LOG_DIR/reset-scripts
  chmod -v g+w $script &>$LOG_DIR/reset-scripts
done
