#!/bin/bash

if [[ `id -u` != 0 ]]; then
    echo "Script will work only with root rights"
    exit 1
fi

SCRIPT_DIR=/etc/mig
SCRIPT_PATH=$SCRIPT_DIR/mig-t8x-accel-sampling-freq-fix.sh

UNIT_NAME=mig-t8x-accel-sampling-fix.service
UNIT_PATH=/etc/systemd/system/$UNIT_NAME

if [ ! -d ${SCRIPT_DIR} ]; then
    echo "Creating $SCRIPT_DIR directory..."
    mkdir $SCRIPT_DIR
fi

echo 'SET_FREQ_FILE="/sys/bus/iio/devices/iio:device0/in_accel_sampling_frequency"
AV_FREQ_FILE="/sys/bus/iio/devices/iio:device0/in_accel_sampling_frequency_available"

minFreq=`cat $AV_FREQ_FILE | cut -d' ' -f1`

echo $minFreq > $SET_FREQ_FILE' > $SCRIPT_PATH


echo '[Unit]
Description=Fix accel sampling frequency for MIG T8x

[Service]
Type=simple
ExecStart=/bin/bash '$SCRIPT_PATH'

[Install]
WantedBy=multi-user.target' > $UNIT_PATH

chmod 664 $UNIT_PATH
systemctl enable $UNIT_NAME
