#!/bin/bash
mapfile -t config < /etc/argon/config

sleep "${config[0]}"
while true; do
  ping -c1 archlinux.org
  online=$?
  if [[ $online == 0 ]]; then
    updates=$( (checkupdates && auracle sync) | wc -l)
    if [[ $updates == 1 ]]; then
      updateword="update"
    else
      updateword="updates"
    fi
    if [[ $updates == 0 ]]; then
      if [[ ${config[6]} == "True" ]]; then
        notify-send --hint=int:transient:1 -a Argon -i "${config[5]}" "${config[3]}"
      fi
      echo "${config[3]}" > /etc/argon/message
      sleep "${config[1]}"
    else
      updates_msg=${config[2]//NUM/$updates}
      updates_msg=${updates_msg//UPD/$updateword}
      notify-send --hint=int:transient:1 -a Argon -i "${config[4]}" "$updates_msg"
      echo "${config[2]}" > /etc/argon/message
      sleep "${config[1]}"
    fi
  else
    sleep 60
  fi
done
