#!/usr/bin/env bash
set -euo pipefail

APT_HISTORY_LOG="/var/log/apt/history.log"
INSTALLED_HISTORY="/var/log/apt/package_installs.log"
REMOVED_HISTORY="/var/log/apt/package_removals.log"

echo "==> Logging Package Changes"

last_log_date=$(grep Start-Date: $APT_HISTORY_LOG | tail -n1)
last_command_line=$(grep Commandline $APT_HISTORY_LOG | sed 's/Commandline://g' | tail -n1)

if [[ $last_command_line == *"install"* ]]; then
	printf "$last_log_date\n$last_command_line\n" >> $INSTALLED_HISTORY
fi

if [[ $last_command_line == *"remove"* ]]; then
	printf "$last_log_date\n$last_command_line\n" >> $REMOVED_HISTORY
fi
