#!/bin/bash

# Stop bij fout
set -e

# Kleuren voor output
green="\e[32m"
red="\e[31m"
reset="\e[0m"

echo -e "${green}GrovePi installatiescript gestart...${reset}"

# Update systeem
echo -e "${green}Systeem bijwerken...${reset}"
sudo apt update && sudo apt upgrade -y

# Benodigde pakketten installeren
echo -e "${green}Afhankelijkheden installeren...${reset}"
sudo apt install -y git python3 python3-pip python3-smbus i2c-tools raspi-config

# I2C inschakelen
echo -e "${green}I2C inschakelen...${reset}"
sudo raspi-config nonint do_i2c 0

# Installeren van avrdude
echo -e "${green}Avrdude installeren...${reset}"
sudo apt-get install bison flex byacc -y
wget http://download.savannah.gnu.org/releases/avrdude/avrdude-7.0.tar.gz
tar xfv avrdude-7.0.tar.gz && cd avrdude-7.0/
./configure --enable-linuxgpio
make
sudo make install

echo -e "${green}Clonen van GrovePi repository...${reset}"
sudo rm -rf /opt/grovepi
sudo git clone https://git.pixelhosting.nl/Blackwhitebear8/grovepi.git /opt/grovepi
cd /opt/grovepi

# Installeren van Python-pakketten
echo -e "${green}Python bibliotheken installeren...${reset}"
pip3 install --upgrade setuptools --break-system-packages
pip3 install -r /opt/grovepi/Software/Python/requirements.txt --break-system-packages

# Installeren van GrovePi firmware en bibliotheken
echo -e "${green}GrovePi installeren...${reset}"
cd /opt/grovepi/Script
sudo bash install.sh

# Controle uitvoeren
echo -e "${green}GrovePi test uitvoeren...${reset}"
cd /opt/grovepi/Software/Python
python3 grove_led_blink.py

echo -e "${green}Installatie voltooid! Herstart je Raspberry Pi.${reset}"