#!/bin/sh

#FBCP install
sudo apt-get install cmake -y
sudo rm -rf rpi-fbcp
sudo git clone https://github.com/tasanakorn/rpi-fbcp
sudo mkdir ./rpi-fbcp/build
cd ./rpi-fbcp/build/
sudo cmake ..
sudo make
sudo install fbcp /usr/local/bin/fbcp
cd ../../

sudo grep 'fbcp' /etc/rc.local
ret=$?
if [ $ret -eq 1 ] ; then
	sudo cp -a /etc/rc.local /etc/rc.local.orig
	sudo sed -i 's/^exit 0/sleep 7\nfbcp \&\n\ncon2fbmap 1 0\n\nexit 0/' /etc/rc.local
fi

wget https://github.com/goodtft/LCD-show/blob/master/usr/tft35a-overlay.dtb?raw=true -O tft35a-overlay.dtb
sudo cp ./tft35a-overlay.dtb /boot/overlays/tft35a-overlay.dtb.dtbo
sudo cp ./tft35a-overlay.dtb /boot/overlays/

sudo apt install xinput-calibrator
sudo sh -c 'cat <<EOF > /usr/share/X11/xorg.conf.d/99-calibration.conf
Section "InputClass"
        Identifier      "calibration"
        MatchProduct    "ADS7846 Touchscreen"
        Option  "Calibration"   "3932 300 294 3801"
        Option  "SwapAxes"      "1"
        Option "EmulateThirdButton" "1"
        Option "EmulateThirdButtonTimeout" "1000"
        Option "EmulateThirdButtonMoveThreshold" "300"
EndSection
EOF'

sudo cp -rf /usr/share/X11/xorg.conf.d/10-evdev.conf /usr/share/X11/xorg.conf.d/45-evdev.conf

sudo grep 'dtoverlay=tft35a' /etc/rc.local
ret=$?
if [ $ret -eq 1 ] ; then
	sudo cp -a /boot/config.txt /boot/config.txt.orig
	sudo sh -c 'cat <<EOF >> /boot/config.txt
dtparam=i2c_arm=on
dtparam=spi=on
dtoverlay=tft35a
EOF'
fi

grep 'ProFont6x11' /boot/cmdline.txt
ret=$?
if [ $ret -eq 1 ] ; then
	sudo cp -a /boot/cmdline.txt /boot/cmdline.txt.orig
	sudo sed -i 's/splash/fbcon=map:10 fbcon=font:ProFont6x11 logo.nologo/' /boot/cmdline.txt
fi

echo "reboot now"
sleep 3
sudo reboot
