#!/bin/bash
if [ -f "$HOME/.conkyrc" ]; then
  error "User error: Another Conky theme is already installed! Uninstall the original app (Or delete $HOME/.conkyrc), then try again."
fi

# Get dependencies
install_packages conky-all lua5.1 || exit 1
#get conkyrc
wget -O ~/.conkyrc https://raw.githubusercontent.com/Botspot/rpi_conky/master/conky_rings/.conkyrc || error "Failed to get .conkyrc!"
#get fonts to ~/.local/share/fonts
mkdir -p ~/.local/share/fonts && cd ~/.local/share/fonts || error "Failed to create and enter $HOME/.local/share/fonts folder!"
wget https://github.com/Botspot/rpi_conky/raw/master/conky_rings/fonts.tar.gz || error "Failed to download the required fonts!"
tar -xvf fonts.tar.gz || error "Failed to extract the required fonts!"
rm -f fonts.tar.gz || error "Failed to remove fonts archive!"
#remove unnecessary file extracted from the tar.gz
rm -f .uuid || error "Failed to delete unnecessary .uuid file!"

#get rings.lua
echo "Downloading config..."
wget -O ~/.conky_rings.lua https://raw.githubusercontent.com/Botspot/rpi_conky/master/conky_rings/.conky_rings.lua || error "Failed to download conky-rings.lua!"

echo "Running Conky in background..."
#run conky this time
setsid -f conky -d

echo "Making autostart entry..."
#make autostart entry to run it next time
mkdir -p ~/.config/autostart
sudo rm -f ~/.config/autostart/conky.desktop
echo "[Desktop Entry]
Name=Conky
Type=Application
Exec=bash -c 'sleep 5;conky -q -d -p 3'
Terminal=false
Comment=system monitoring tool.
Categories=Utility;" > ~/.config/autostart/conky.desktop || error "Failed to create desktop entry!"

exit 0
