#!/bin/bash

echo -e "\ninstallation script for CubeSatSim v1.2\n"

FILE=/home/pi/CubeSatSim/sim.cfg
if [ -f "$FILE" ]; then
    echo "$FILE exists."
else 
    echo "creating $FILE"
    echo "AMSAT 1 0.0 0.0" > /home/pi/CubeSatSim/sim.cfg
fi

cd

CubeSatSim/config -c -n

CubeSatSim/config -l -n
  
sudo apt-get update && sudo apt-get dist-upgrade -y

# removed wiringpi and python-picamera python3-picamera 
sudo apt-get install -y git libasound2-dev i2c-tools build-essential libgd-dev libmagic-dev minicom

cd /tmp

wget https://project-downloads.drogon.net/wiringpi-latest.deb

sudo dpkg -i wiringpi-latest.deb


cd

#changed to python3-smbus
sudo apt install -y python3-pip python3-smbus

sudo pip3 install --upgrade setuptools

sudo pip3 install adafruit-blinka RPI.GPIO adafruit-extended-bus adafruit-circuitpython-ina219



cd ~/CubeSatSim

git pull

make debug

FILE=/home/pi/CubeSatSim/.mode
if [ -f "$FILE" ]; then
    echo "$FILE exists."
else 
    echo "creating $FILE"
    echo "ARG1=f" > .mode
fi

cd

git clone https://github.com/alanbjohnston/direwolf.git

cd direwolf

make -j

sudo make install

make install-rpi


cd

git clone https://github.com/alanbjohnston/pi-power-button.git

cd pi-power-button

git checkout master

./script/install


cd

git clone https://github.com/alanbjohnston/PiSSTVpp.git

cd PiSSTVpp

make pisstvpp

echo "Copying SSTV image 1"
cp /home/pi/CubeSatSim/sstv/sstv_image_1_320_x_256.jpg /home/pi/CubeSatSim/sstv_image_1_320_x_256.jpg

echo "Copying SSTV image 2"
cp /home/pi/CubeSatSim/sstv/sstv_image_2_320_x_256.jpg /home/pi/CubeSatSim/sstv_image_2_320_x_256.jpg


cd

git clone https://github.com/alanbjohnston/rpitx.git

cd rpitx

./install.sh


cd

sudo cp ~/CubeSatSim/systemd/cubesatsim.service /etc/systemd/system/cubesatsim.service

sudo systemctl enable cubesatsim

sudo cp ~/CubeSatSim/systemd/rpitx.service /etc/systemd/system/rpitx.service

sudo systemctl enable rpitx

sudo cp /boot/config.txt /boot/config.txt.0

sudo cp /boot/cmdline.txt /boot/cmdline.txt.0


sudo raspi-config nonint do_i2c 0

sudo raspi-config nonint do_camera 0

sudo raspi-config nonint do_legacy 0

#if [ "$1" = "u" ]; then
#fi

  sudo sed -i 's/console=serial0,115200 //g' /boot/cmdline.txt
  
  sudo sed -i 's/#dtparam=i2c_arm=on/dtparam=i2c_arm=on/g' /boot/config.txt
  
  if [[ $(grep 'dtoverlay=i2c-gpio,bus=3,i2c_gpio_delay_us=1,i2c_gpio_sda=23,i2c_gpio_scl=24' /boot/config.txt) ]]; then
    echo "dtoverlay=i2c-gpio already in /boot/config.txt"
  else
    echo "adding dtoverlay=i2c-gpio to /boot/config.txt"
    sudo sh -c 'echo "\ndtoverlay=i2c-gpio,bus=3,i2c_gpio_delay_us=1,i2c_gpio_sda=23,i2c_gpio_scl=24" >> /boot/config.txt'
  fi

  if [[ $(grep 'enable_uart=1' /boot/config.txt) ]]; then
    echo "enable_uart=1 already in /boot/config.txt"
  else
    echo "adding enable_uart=1 to /boot/config.txt"
    sudo sh -c 'echo "\nenable_uart=1" >> /boot/config.txt'
  fi
  
  if [[ $(grep 'dtoverlay=disable-bt' /boot/config.txt) ]]; then
    echo "dtoverlay=disable-bt already in /boot/config.txt"
  else
    echo "adding dtoverlay=disable-bt to /boot/config.txt"
    sudo sh -c 'echo "\ndtoverlay=disable-bt" >> /boot/config.txt'
  fi
  
  if [[ $(grep 'dtoverlay=dwc2' /boot/config.txt) ]]; then
    echo "dtoverlay=dwc2 aalready in /boot/config.txt"
  else
    echo "adding dtoverlay=dwc2 to /boot/config.txt"
    sudo sh -c 'echo "\ndtoverlay=dwc2" >> /boot/config.txt'
  fi

  if [[ $(grep 'modules-load=dwc2,g_ether' /boot/cmdline.txt) ]]; then
    echo "modules-load=dwc2,g_ether already in /boot/cmdline.txt"
  else
    echo "adding modules-load=dwc2,g_ether to /boot/cmdline.txt"
    sudo sed -i 's/ rootwait/ rootwait modules-load=dwc2,g_ether/g' /boot/cmdline.txt
  fi
  
echo "Would you like to reboot to complete the installation (y/n)?"

read -r ANS

if [ "$ANS" = "y" ]; then

    sudo reboot now

else

    echo "The CubeSatSim software will start next time you reboot"
    
fi
