#! /bin/bash

#Enabling SSH
printf "Enabling SSH "
sudo systemctl enable ssh --quiet
sudo systemctl start ssh
printf "\e[32mdone\e[39m.\n"

# Enabling serial
printf "Enabling serial "
sudo sh -c "grep -qxF 'enable_uart=1' /boot/config.txt || echo 'enable_uart=1' >> /boot/config.txt"
printf "\e[32mdone\e[39m.\n"

# Updating and installing
printf "Synchronizing program package indexes "
if sudo apt-get update -qq; then
printf "\e[32mdone\e[39m.\n"
else
printf "\e[31mfailed\e[39m.\n"
fi

printf "Updating programs (this can take several minutes) "
if sudo apt-get upgrade -y -qq && sudo apt-get dist-upgrade -y -qq; then
printf "\e[32mdone\e[39m.\n"
else
printf "\e[31mfailed\e[39m.\n"
fi

printf "Installing new programs (this can take several minutes) "
if sudo apt-get install screen arduino arduino-mk git -y -qq; then
printf "\e[32mdone\e[39m.\n"
else
printf "\e[31mfailed\e[39m.\n"
fi

printf "Cleaning program directories "
if sudo apt-get autoremove -y -qq; then
printf "\e[32mdone\e[39m.\n"
else
printf "\e[31mfailed\e[39m.\n"
fi

printf "Downloading course material "
if [ ! -d /home/pi/tnk116-course-material/.git ]
then
    git clone -b master -q https://gitlab.liu.se/rasri17/tnk116-course-material.git
    cd /home/pi/tnk116-course-material/
    git submodule update --init --recursive -q
else
    cd /home/pi/tnk116-course-material/
    git pull origin master -q
    git merge master origin/master -q
    git submodule update --init --recursive -q
fi
if [ $? -eq 0 ]; then
printf "\e[32mdone\e[39m.\n"
else
printf "\e[31mfailed\e[39m.\n"
fi
cd ~