is_Raspberry=$(cat /proc/device-tree/model | awk  '{print $1}')
if [ "x${is_Raspberry}" != "xRaspberry" ] ; then
  echo "This script contains drivers for the Raspberry pi and will not work on the current platform."
  exit 1
fi

# sudo apt update
update_system() {
  echo "Updating system"
  sudo apt update
  sudo apt dist-upgrade
  sudo apt upgrade
  sudo apt -y install git vim zsh mplayer
}

install_screen() {
  echo "Installing screen program"
  sudo raspi-config nonint do_i2c 0
  /home/grace/.volta/bin/npm install
  /home/grace/.volta/bin/npx prisma generate && /home/grace/.volta/bin/npx prisma db push && /home/grace/.volta/bin/npx prisma db seed
}

install_sound_card_drivers() {
  echo "Installing sound card drivers"
  cd /home/grace/
  git clone https://github.com/waveshare/WM8960-Audio-HAT 
  cd WM8960-Audio-HAT
  sudo ./install.sh
}

configure_sound() {
  amixer -c 0 set 'Headphone' 100%
  amixer -c 0 set 'Speaker' 100%
  amixer -c 0 set 'Speaker AC' 100%
  amixer -c 0 set 'Speaker DC' 100%
  amixer -c 0 set 'Speaker Playback ZC' 100%
  sudo alsactl store
}

setup_daemons() {
  echo "Setting up daemons"
  sudo cp /home/grace/portal/setup/listener-puppet/screen.service /lib/systemd/system/screen.service
  sudo cp /home/grace/portal/setup/listener-puppet/player.service /lib/systemd/system/player.service
  sudo systemctl daemon-reload
  sudo systemctl enable player
  sudo systemctl enable screen
  sudo systemctl enable wm8960-soundcard.service 
  sudo systemctl start player
  sudo systemctl start screen
  sudo systemctl start wm8960-soundcard                                
}

install_ohmyzsh() {
  echo "Installing oh my zsh"
  sh -c "$(curl -fsSL https://raw.githubusercontent.com/ohmyzsh/ohmyzsh/master/tools/install.sh)"
  echo TERM=xterm > ~/.zshrc
}

install_all() {
  update_system
  install_screen
  install_sound_card_drivers
  configure_sound
  setup_daemons
  install_ohmyzsh
  sudo reboot
}

install_all

echo "------------------------------------------------------"
echo "The Black Portal listening device has been setup."
echo "This raspberry pi now rebooting in order to apply changes"
echo "------------------------------------------------------"