#!/bin/bash
function print_centered {
     [[ $# == 0 ]] && return 1

     declare -i TERM_COLS="$(tput cols)"
     declare -i str_len="${#1}"
     [[ $str_len -ge $TERM_COLS ]] && {
          echo "$1";
          return 0;
     }

     declare -i filler_len="$(( (TERM_COLS - str_len) / 2 ))"
     [[ $# -ge 2 ]] && ch="${2:0:1}" || ch=" "
     filler=""
     for (( i = 0; i < filler_len; i++ )); do
          filler="${filler}${ch}"
     done

     printf "%s%s%s" "$filler" "$1" "$filler"
     [[ $(( (TERM_COLS - str_len) % 2 )) -ne 0 ]] && printf "%s" "${ch}"
     printf "\n"

     return 0
}
print_centered "$(tput setaf 10)"

print_centered ""
print_centered "  ----  "
print_centered "-|    |-"
print_centered "-|    |-"
print_centered "-|    |-"
print_centered "-|    |-"
print_centered "  ----  "
print_centered "ChipTools v1.0"
echo -n "$(tput setaf 7)"

sudo apt install libudev-dev curl libelf-dev libusb-1.0-0-dev libusb-dev libftdi-dev libftdi1-dev libhidapi-dev build-essential -y
curl http://wareck.free.fr/cross_compilation/avr_toolchain/avr_toolchain.tar.xz | tar -xJ
sudo cp -r avr /usr/local
rm -r -f avr
cd ~
git clone https://github.com/flashrom/flashrom.git
cd flashrom
make
sudo make install
cd ~
rm -r -f flashrom

if ! grep "/usr/local/avr/bin" ~/.bashrc >/dev/null
        then
        echo "export PATH=XXXXPATH::/usr/local/avr/bin" >> ~/.bashrc
        sed -i -e "s/XXXX/$/g" ~/.bashrc
        source ~/.bashrc
fi
echo -e "\nDone."
