#!/bin/bash
# Version: 0.2.0
# Original awk code found somewhere on LQ -  credit to the original author :)
# Changes copyright (c) 2023:
#   Darren 'Tadgy' Austin <darren (at) afterdark.org.uk>
# Licensed under the terms of the GNU General Public License version 3.
#
# Colourise the Slackware ChangeLog and, optionally, 'watch' it for changes.
# Best run in a screen or tmux window with activity monitoring turned on, so you get a notification any time the ChangeLog is updated.

# Defaults.
CHANGELOG_URL="https://ftp.osuosl.org/pub/slackware/slackware64-current/ChangeLog.txt"
# CHANGELOG_URL="https://slackware.uk/slackware/slackware64-current/ChangeLog.txt"
REFRESH_INTERVAL="600"
MAX_LINES="10000000"		# Should cover the whole ChangeLog.

# Colourise at most MAX_LINES of the ChangeLog.
colourise_CL() {
  curl -sL "$CHANGELOG_URL" | head -n "$MAX_LINES" | awk '
      /^(Mon|Tue|Wed|Thu|Fri|Sat|Sun) (Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)/ {
        print "\033[1;35m"$0"\033[0m"; next
      }
      /^[^: ]+\/[^: ]+:/ {
        status = tolower($2)
        colour = "1;33m"
        if ((index(status, "added") == 1))
          colour = "1;32m"
        else if ((index(status, "removed") == 1))
          colour = "1;31m"
        else if ((index(status, "rebuilt") == 1))
          colour = "1;34m"
        $1 = "\033["colour$1"\033[0m"; print; next
      }
      /^\+---+/ { $0 = "" }{ print }'
}

# Parse command line options.
while (( $# != 0 )); do
  case "$1" in
    -h|-help|--help)
      printf "%s %s %s %s %s\\n" "${0##*/}" "[-l|--lines <max output lines>]" "[-r|--refresh <time in seconds>]" "[-u|--url <URL to ChangeLog>]" "[-w|--watch]"
      printf "%s '%s'.  %s '%s' %s.  %s '%s'.\\n" "Default -l is" "$MAX_LINES" "Default -r is" "$REFRESH_INTERVAL" "(subject to using -w)" "Default -u is" "$CHANGELOG_URL" >&2
      exit 0
      ;;
    -l|-lines|--lines)
      [[ -z "$2" ]] || [[ "$2" =~ [^[:digit:]]+ ]] && {
        printf "%s: %s: %s\\n" "${0##*/}" "invalid argument to -l" "maximum output lines required" >&2
        exit 1
      }
      MAX_LINES="$2"
      shift 2
      ;;
    -r|-refresh|--refresh)
      [[ -z "$2" ]] || [[ "$2" =~ [^[:digit:]]+ ]] && {
        printf "%s: %s: %s\\n" "${0##*/}" "invalid argument to -r" "refresh interval (seconds) required" >&2
        exit 1
      }
      REFRESH_INTERVAL="$2"
      shift 2
      ;;
    -u|-url|--url)
      [[ -z "$2" ]] && {
        printf "%s: %s: %s\\n" "${0##*/}" "invalid argument to -u" "URL to ChangeLog required" >&2
        exit 1
      }
      CHANGELOG_URL="$2"
      shift 2
      ;;
    -w|-watch|--watch)
      WATCH_MODE=1
      shift
      ;;
    --)
      break
      ;;
    *)
      printf "%s: %s: %s\\n" "${0##*/}" "invalid option" "$1" >&2
      printf "%s: %s %s\\n" "Try" "${0##*/}" "--help" >&2
      exit 1
      ;;
  esac
done

# Watch or output the coloured ChangeLog.
if (( WATCH_MODE == 1 )); then
  export CHANGELOG_URL MAX_LINES
  export -f colourise_CL
  watch -n "$REFRESH_INTERVAL" -t -c "colourise_CL"
  exit $?
else
  colourise_CL
fi

exit 0
