#!/bin/bash

DIRECTORY="$(dirname "$(dirname "$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )")")"

function error {
  echo -e "\\e[91m$1\\e[39m"
  exit 1
}

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

# Get dependencies
"${DIRECTORY}/pkg-install" "conky-all lua50" "$(dirname "$0")" || 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
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!"

#run conky this time
conky -q -d -p 3 &

#make autostart entry to run it next time
mkdir -p ~/.config/autostart
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!"
