#!/bin/bash

notify="off" # Can be set to: on | off

NotifySwitch(){

  if [ ${notify} = on ];then
    notify-send """$1"""
  elif [ "$notify" = off ];then
    echo -e """$1"""
  fi
}

if [ -f /usr/bin/rsstail ];then


  feedcmd='rsstail -1 -u'
  feed='https://github.com/void-linux/void-packages/commits/master.atom'
  newpkgs='grep -e New -e new'
  updatedpkgs='grep -e Update -e update'
  sysupdates="xbps-install -S | xbps-install -Snu"

    case "$1" in
    -nc)
      echo -e "Downloading feed info, please wait...\n"
      getnew=$(${feedcmd} ${feed} | ${newpkgs} | wc -l)
      NotifySwitch "There are ${getnew} new package commits in master\n"
      ;;

    -uc)
      echo -e "Downloading feed info, please wait...\n"
      getupdated=$(${feedcmd} ${feed} | ${updatedpkgs} | wc -l)
      NotifySwitch "There are ${getupdated} updated package commits in master\n"
      ;;

    -ff)
      getfull=$(${feedcmd} ${feed})
      echo -e """${getfull}\n"""
      ;;

    -n)
      fullnew=$(${feedcmd} ${feed} | ${newpkgs})
      echo """${fullnew}"""
      ;;

    -u)
      fullupdated=$(${feedcmd} ${feed} | ${updatedpkgs})
      echo "${fullupdated}"
      ;;

    -sc)
      echo -e "Downloading feed info, please wait...\n"
      xbps-install -S &> /dev/null &>2
      getsys=$(xbps-install -nu | wc -l)
      NotifySwitch "There are ${getsys} system updates available\n"
      ;;

    -r)
      echo -e "Downloading feed info, please wait...\n"

      nohup xbps-install -S 
      getsys=$(xbps-install -Snu | wc -l)
      getnew=$(${feedcmd} ${feed} | ${newpkgs} | wc -l)
      getupdated=$(${feedcmd} ${feed} | ${updatedpkgs} | wc -l)

      NotifySwitch """
      [ Void Checker Report ]

      GIT COMMITS:
      Updated Packages: ${getupdated}
      New Packages: ${getnew}

      SYSTEM UPDATES:
      Currently Available: ${getsys}\n"""
      ;;
    *)
      echo """Usage: $0 -rn | -ru | -s | -a

      -nc     Show total number of new package commits in master
      -uc     Show total number of updated package commits in master
      -u      Output all updated packages in feed
      -n      Output all new packages in feed
      -sc     Show total number of available system updates
      -r      Show a summerized count of github and system update status"""
      ;;

    esac

elif [ ! -f /usr/bin/rsstail ];then
  echo "[ERROR] This script requires the package: 'rsstail'"
  exit 0;

else
  echo "Something went wrong when checking for: /usr/bin/rsstail"
  exit 0;
fi
