#!/bin/bash

echo -e "\nupdate script for CubeSatSim v1.1\n"

sudo rm /home/pi/CubeSatSim/morse.wav /home/pi/CubeSatSim/id.txt /home/pi/CubeSatSim/cw.txt > /dev/null 2>&1 

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

  sudo apt-get update && sudo apt-get dist-upgrade -y

  sudo apt-get install -y wiringpi git libasound2-dev i2c-tools build-essential libgd-dev libmagic-dev python3-pip minicom

fi

sudo apt-get install -y python3-smbus

sudo sed -i 's/update.sh/update /g' /etc/motd

sudo sed -i 's/installed and/installed\nand/g' /etc/motd

sudo sed -i 's/more information/more\ninformation/g' /etc/motd

sudo sed -i 's/update to/update\nto/g' /etc/motd

cd /home/pi/CubeSatSim

git pull > .updated

make debug

FLAG=0

if [[ $(diff systemd/cubesatsim.service /etc/systemd/system/cubesatsim.service) ]]; then
  echo "changed cubesatsim.service."
  sudo cp /home/pi/CubeSatSim/systemd/cubesatsim.service /etc/systemd/system/cubesatsim.service
  FLAG=1
else
  echo "no changes to cubesatsim.service."
fi

if [[ $(diff systemd/rpitx.service /etc/systemd/system/rpitx.service) ]]; then
  echo "changed rpitx.service."
  sudo cp /home/pi/CubeSatSim/systemd/rpitx.service /etc/systemd/system/rpitx.service
  FLAG=1
else
  echo "no changes to rpitx.service."
fi

FILE=/home/pi/CubeSatSim/sstv_image_1_320_x_256.jpg
if [ ! -f "$FILE" ]; then
    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
fi
    
FILE=/home/pi/CubeSatSim/sstv_image_2_320_x_256.jpg
if [ ! -f "$FILE" ]; then
    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
fi
      
grep 'update' /home/pi/CubeSatSim/.updated
if [[ $(grep 'update' /home/pi/CubeSatSim/.updated) ]]; then
  echo "update script updated, running again"
  /home/pi/CubeSatSim/update
fi

if [ $FLAG -eq 1 ]; then
  echo "systemctl daemon-reload and restart"
  sudo systemctl daemon-reload 
  sudo systemctl restart cubesatsim
else
  grep 'changed' /home/pi/CubeSatSim/.updated
  if [[ $(grep 'changed' /home/pi/CubeSatSim/.updated) ]]; then
    echo "systemctl restart cubesatsim"
    sudo systemctl restart cubesatsim
  else
    echo "nothing to do."
  fi  
fi

if [ ! -d "/home/pi/PiSSTVpp" ]; then

  sudo apt-get update && sudo apt-get dist-upgrade -y

  sudo apt-get install -y python-picamera python3-picamera build-essential libgd-dev libmagic-dev


  cd

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

  cd PiSSTVpp

  make pisstvpp

  cd
  
  
  sudo raspi-config nonint do_camera 0
  

  FILE=/home/pi/CubeSatSim/sstv_image_1_320_x_256.jpg
  if [ ! -f "$FILE" ]; then
      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
  fi

  FILE=/home/pi/CubeSatSim/sstv_image_2_320_x_256.jpg
  if [ ! -f "$FILE" ]; then
      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
  fi
  
#  echo "You need to reboot to complete this update.  Reboot now (y/n)?"

#  read -r ANS

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

#    sudo reboot now

#  else

#    echo "The CubeSatSim software may not work correctly until you reboot."

#  fi 
  
fi

cd /home/pi/pi-power-button

git pull > .updated_p

  grep 'changed' /home/pi/pi-power-button/.updated_p
  if [[ $(grep 'changed' /home/pi/pi-power-button/.updated_p) ]]; then
  
    echo "updating pi-power-button."
    
    script/install
    
    echo "You need to reboot to complete this update.  Reboot now (y/n)?"

    read -r ANS

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

      sudo reboot now

    else

      echo "The CubeSatSim software may not work correctly until you reboot."

  fi  

  else
    echo "nothing to do for pi-power-button."
  fi 
  
  echo "CubeSatSim update complete."
