#!/bin/bash

PKG_DIR=$1
PKG_NAME=$2

# Set setuid root permission on .sbin files
for SRC in ./util/bin/*.sbin; do
    DEST=/usr/local/bin/`basename $SRC`
    echo "Installing as setuid root: $DEST"
    sudo cp $SRC $DEST
    sudo chown root:root $DEST
    sudo chmod 4755 $DEST
done

# Copy udev rule to correct place
sudo cp ./pkg/90-raspberrystem.rules /etc/udev/rules.d/90-raspberrystem.rules

# Set up SPI, I2C, etc...  Taken from http://github.com/asb/raspi-config
CONFIG=/boot/config.txt
BLACKLIST=/etc/modprobe.d/raspi-blacklist.conf
if ! [ -e $BLACKLIST ]; then
    touch $BLACKLIST
fi

# SPI config
SETTING=on
sed $CONFIG -i -r -e "s/^((device_tree_param|dtparam)=([^,]*,)*spi)(=[^,]*)?/\1=$SETTING/"
if ! grep -q -E "^(device_tree_param|dtparam)=([^,]*,)*spi=[^,]*" $CONFIG; then
    printf "dtparam=spi=$SETTING\n" >> $CONFIG
fi
sed $BLACKLIST -i -e "s/^\(blacklist[[:space:]]*spi[-_]bcm2708\)/#\1/"
#modprobe spi-bcm2708

# I2C config
sed $CONFIG -i -r -e "s/^((device_tree_param|dtparam)=([^,]*,)*i2c(_arm)?)(=[^,]*)?/\1=$SETTING/"
if ! grep -q -E "^(device_tree_param|dtparam)=([^,]*,)*i2c(_arm)?=[^,]*" $CONFIG; then
    printf "dtparam=i2c_arm=$SETTING\n" >> $CONFIG
fi
sed $BLACKLIST -i -e "s/^\(blacklist[[:space:]]*i2c[-_]bcm2708\)/#\1/"
#modprobe i2c-bcm2708

