#!/bin/bash
set -e

################
##   CONFIG   ##
################
CWD="$(pwd)" # current working directory
flash_arduino="u2" # u2 or ftdi
arduino_device="ttyACM0" # ls /dev/tty* and find your arduino
mega=2560 #2560 or 1280

script_version="2.0.0"
rdate="13/01/2022"

################
##  SOFTWARE  ##
################

clear
echo ""
echo -n -e "\e[97m\u2554"
for i in {1..34}; do echo -e -n "\u2550"; done
echo -e "\u2557\e[0m"
echo -e "\e[97m\u2551     SPI Bios Flashrom \e[93m"V$script_version"\e[0m\e[97m     \u2551\e[0m"
echo -e "\e[97m\u2551      Release date: \e[93m"$rdate"\e[0m\e[97m    \u2551\e[0m"
echo -n -e "\e[97m\u255A"
for j in {1..34}; do echo -e -n "\u2550";done
echo -e -n "\u255D\e[0m"
echo


function arduino_ {
echo -e ""
echo -e "\e[97mBuild Arduino SPI_Flash tools:\e[0m\e[24m"
if [ -a /dev/ttyACM0 ]
then
arduino_device="ttyACM0"
fi
if [ -a /dev/ttyUSB0 ]
then
arduino_device="ttyUSB0"
fi
if [ -f .lastbuild ];then rm .lastbuild;fi
cat <<'EOF'>> .lastbuild
Mode=arduino
Device=xxx
EOF
sed -i -e "s/xxx/$arduino_device/g" .lastbuild
echo -e "\nArduino device found at: \e[91m$arduino_device\e[0m"
echo -e "\n\e[93mPerforming Update :\e[0m"
sudo apt-get update
sudo apt-get install flashrom gcc-avr binutils-avr gdb-avr avr-libc avrdude libpci-dev git libusb-1.0-0-dev libftdi-dev libftdi1-dev -y
if [ -d frser-duino ]
then
rm -r -f frser-duino
fi
echo -e "\n\e[93mDownload frser-duino :\e[0m"
git clone --recursive git://github.com/urjaman/frser-duino frser-duino
cd frser-duino
if [ $arduino_device = "ttyUSB0" ]
then
sed -i -e "s/dev\/ttyACM0/dev\/$arduino_device/g" Makefile
fi
echo -e "\n\e[93mBuild frser-arduino :\e[0m"
make $flash_arduino -j$(nproc)
echo -e "\n\e[93mFlash Arduino :\e[0m"
make flash-$flash_arduino
sleep 2
}

function mega_ {
echo -e ""
echo -e "\e[93mBuild ATMEGA$mega SPIFlash tools:\e[0m\e[24m"
if [ -a /dev/ttyACM0 ]
then
arduino_device="ttyACM0"
fi
if [ -a /dev/ttyUSB0 ]
then
arduino_device="ttyUSB0"
fi
if [ -f .lastbuild ];then rm .lastbuild;fi
cat <<'EOF'>> .lastbuild
Mode=mega
Device=xxx
EOF
sed -i -e "s/xxx/$arduino_device/g" .lastbuild
echo -e ""
echo -e "Arduino device found at: \e[91m$arduino_device\e[0m"
echo -e "\n\e[93mPerforming Update :\e[0m"
sudo apt-get install flashrom gcc-avr binutils-avr gdb-avr avr-libc avrdude libpci-dev git libusb-1.0-0-dev libftdi-dev libftdi1-dev -y
if [ -d frser-duino ]
then
rm -r -f frser-duino
fi
echo -e "\n\e[93mDownload frser-mega :\e[0m"
git clone --recursive git://github.com/urjaman/frser-duino frser-duino
cd frser-duino
if [ $arduino_device = "ttyUSB0" ]
then
sed -i -e "s/dev\/dev/ttyACM0/dev\/$arduino_device/g" Makefile
fi
echo -e "\n\e[93mBuild frser-duino :\e[0m"
make mega$mega -j$(nproc)
echo -e "\n\e[93mFlash ATMega$mega :\e[0m"
make flash-mega$mega
sleep 2
}

function raspberry_ {
cat <<'EOF'>> .lastbuild
Mode=raspberry
Device=raspberry
EOF
echo -e ""
echo -e "\n\e[93mBuild Raspberry SPIFlash tools:\e[0m\e[24m"
echo -e "\n\e[93mPerforming Update :\e[0m"
sudo apt-get update
sudo apt-get upgrade -y
sudo apt-get install build-essential pciutils usbutils libpci-dev libusb-dev libusb-1.0-0 libusb-1.0-0-dev libftdi1 libftdi-dev zlib1g-dev subversion git ghex libusb-1.0-0-dev libftdi-dev libftdi1-dev libjaylink-dev -y
sudo modprobe spi_bcm2708
sudo modprobe spi_bcm2835
sudo modprobe spidev
sudo sed -i -e "s/\#dtparam=spi=on/dtparam=spi=on/g" /boot/config.txt
}

function flashrom_ {
if ! [ -x "$(command -v flashrom)" ]
then
cd $CWD
if [ ! -d flashrom ]
then
echo -e "\n\e[93mDownload flashrom :\e[0m"
git clone git://github.com/flashrom/flashrom.git
fi
cd flashrom
sed -i -e "s/WARNERROR ?= yes/WARNERROR ?= no/g" Makefile
echo -e "\n\e[93mBuild and install flashrom:\e[0m"
make -j$(nproc)
sudo make install
fi
}

function clean_ {
echo -e ""
echo -e "\e[97mCleaning:\e[0m\e[24m"
if [ -d flashrom ]; then rm -r -f flashrom; fi
if [ -d frser-duino ]; then rm -r -f frser-duino; fi
sleep 3
if [ -f .lastbuild ]
then
source .lastbuild
if [ $Mode = "mega" ]
then
avrdude -p m$mega -c stk500v2 -P /dev/$Device -F -U flash:w:.clean/Mega_blink.hex
fi
if [ $Mode = "arduino" ]
then
avrdude -p atmega328p -c arduino -P /dev/$Device -b 115200 -D -U flash:w:.clean/Arduino_blink.hex
fi
rm .lastbuild
fi
if [ -f /usr/sbin/flashrom ]
then
sudo rm /usr/sbin/flashrom
fi
echo -e "\n\e[95mCleaning...Done !\e[0m"
echo""

}

function flashrom_raspberry_check_ {
echo -e "\n\e[93mCheck flashrom :\e[0m"
flashrom -p linux_spi:dev=/dev/spidev0.0 || :
}

function flashrom_arduino_check_ {
echo -e "\n\e[93mCheck flashrom :\e[0m"
sleep 1
flashrom -p serprog:dev=/dev/$arduino_device:115200 || :
}

function flashrom_mega_check_ {
echo -e "\n\e[93mCheck flashrom :\e[0m"
flashrom -p serprog:dev=/dev/$arduino_device:115200 || :
}

function footer_ {
echo ""
echo -e "\e[92m\e[21mMemo Command line: \e[0m"
echo -e ""
echo -e "\e[93m\e[21mArduino/Mega2560/Mega1280: \e[0m"
echo -e "flashrom -p serprog:dev=/dev/$arduino_device:115200"
echo -e ""
echo -e "\e[93m\e[21mRaspberryPI/beagleBone: \e[0m"
echo -e "flashrom -p linux_spi:dev=/dev/spidev0.0,spispeed=2000"
echo -e ""
echo -e "\e[93m\e[21mCh341a: \e[0m"
echo -e "flashrom -p ch341a_spi"
echo -e ""
if  ! [ -x "$(command -v flashrom)" ]
then
echo 'Error: flashrom is not yet installed.' >&2
echo 'Build and come back to help section....' >&2
else
echo -e "\e[93m\e[21mFlashrom version: \e[0m"
flashrom -R
fi
echo ""
}

function show_help {
echo "\
Usage: $0 [--arduino] [--mega] [--raspberry] [--clean] [--help]

     --help             Display extended help message
     --arduino          Build Serial SPI flashrom tools for arduino328
     --mega             Build Serial SPI flashrom tools for arduinoMega
     --raspberry        Build Serial SPI flashrom tools for raspberryPI/ch341
     --clean       	Clean all build files

Install script Written by: wareck <wareck@gmail.com>
"
}

if [ "$#" == "0" ]; then
    $0 *
    exit 0
fi

for i in "$@"
do
    case $i in
        --help)
            show_help && footer_
            exit
            ;;
        --arduino)
            arduino_ && flashrom_ && flashrom_arduino_check_ && footer_
            ;;
        --mega)
           mega_ && flashrom_ && flashrom_mega_check_ && footer_
           ;;
        --raspberry)
            raspberry_ && flashrom_ && flashrom_raspberry_check_ && footer_
            ;;
        --clean)
            clean_
            ;;
        *)
            show_help
            exit
            ;;
    esac
done