#!/bin/bash

INSTALLATIONS_DIRECTORY=~/.dotfiles/installed_packages

function get_installations()
{
	ls -l --time-style="long-iso" $INSTALLATIONS_DIRECTORY | grep -v ${HOSTNAME} | awk 'NR>1 {print $8}'
}

function concat_installation_files()
{
	DIR_TO_CHECK=$1
	OUTPUT_FILE=$2

	echo "# GENERATED BY A SCRIPT!" > $OUTPUT_FILE
	for file in $DIR_TO_CHECK/*; do
		echo "# START OF $(basename $file)" >> $OUTPUT_FILE
		cat $file >> $OUTPUT_FILE
		echo "# END OF $(basename $file)" >> $OUTPUT_FILE
	done
}

if [ -z "$1" ]
then
	if type wofi > /dev/null 2>&1
	then
		INSTALLATION=$( (echo empty; get_installations)  | wofi -dmenu -only-match -p "Select host to compare installation with:")
	else
		PS3='Please select installation to compare: '
		select opt in $(get_installations)
		do
			INSTALLATION=$opt
			break
		done
	fi
else
	INSTALLATION=$1
fi

if [ x"empty" = x"${INSTALLATION}" ]
then
	echo "No operation."
elif [ -n "${INSTALLATION}" ]
then
	concat_installation_files $INSTALLATIONS_DIRECTORY/$HOSTNAME /tmp/current_host_packages.txt
	concat_installation_files $INSTALLATIONS_DIRECTORY/$INSTALLATION /tmp/other_host_packages.txt

	vimdiff /tmp/current_host_packages.txt /tmp/other_host_packages.txt
fi