#!/bin/bash
#   pacdiff : a simple pacnew/pacorig/pacsave updater
#
#   Copyright (c) 2007 Aaron Griffin <aaronmgriffin@gmail.com>
#   Copyright (c) 2013 Pacman Development Team <pacman-dev@archlinux.org>
#
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of the GNU General Public License as published by
#   the Free Software Foundation; either version 2 of the License, or
#   (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.
#
#   You should have received a copy of the GNU General Public License
#   along with this program.  If not, see <http://www.gnu.org/licenses/>.
#

declare -r myname='pacdiff'
declare -r myver='@PACKAGE_VERSION@'

diffprog=${DIFFPROG:-vimdiff}
diffsearchpath=${DIFFSEARCHPATH:-/etc}
locate=0

if tput setaf 0 &>/dev/null; then
	ALL_OFF="$(tput sgr0)"
	BOLD="$(tput bold)"
	BLUE="${BOLD}$(tput setaf 4)"
	GREEN="${BOLD}$(tput setaf 2)"
	YELLOW="${BOLD}$(tput setaf 3)"
	PURPLE="${BOLD}$(tput setaf 5)"
else
	ALL_OFF="\e[1;0m"
	BOLD="\e[1;1m"
	BLUE="${BOLD}\e[1;34m"
	GREEN="${BOLD}\e[1;32m"
	YELLOW="${BOLD}\e[1;33m"
	PURPLE="${BOLD}\e[1;35m"
fi

usage() {
	echo "$myname : a simple pacnew/pacorig/pacsave updater"
	echo "Usage : $myname [-l]"
	echo "  -l/--locate makes $myname use locate rather than find"
	echo "  DIFFPROG variable allows to override the default vimdiff"
	echo "  DIFFSEARCHPATH allows to override the default /etc path"
	echo "Example : DIFFPROG=meld DIFFSEARCHPATH=\"/boot /etc /usr\" $myname"
}

version() {
	printf "%s %s\n" "$myname" "$myver"
	echo 'Copyright (C) 2007 Aaron Griffin <aaronmgriffin@gmail.com>'
	echo 'Copyright (C) 2013 Pacman Development Team <pacman-dev@archlinux.org>'
}

cmd() {
	if [ $locate -eq 1 ]; then
		locate -0 -e -b \*.pacnew \*.pacorig \*.pacsave
	else
		find $diffsearchpath \( -name \*.pacnew -o -name \*.pacorig -o -name \*.pacsave \) -print0
	fi
}

if [ $# -gt 0 ]; then
	case $1 in
		-l|--locate)
		locate=1;;
		-V|--version)
		version; exit 0;;
		-h|--help)
		usage; exit 0;;
		*)
		usage; exit 1;;
	esac
fi

# see http://mywiki.wooledge.org/BashFAQ/020
while IFS= read -u 3 -r -d '' pacfile; do
	file="${pacfile%.pac*}"
	file_type="pac${pacfile##*.pac}"

	case $file_type in
		pacnew) printf "$GREEN%s$ALL_OFF" "$file_type";;
		pacorig) printf "$YELLOW%s$ALL_OFF" "$file_type";;
		pacsave) printf "$BLUE%s$ALL_OFF" "$file_type";;
	esac

	printf " file found for $PURPLE%s$ALL_OFF\n" "$file"
	if [ ! -f "$file" ]; then
		echo "  $file does not exist"
		rm -iv "$pacfile"
		continue
	fi

	if cmp -s "$pacfile" "$file"; then
		echo "  Files are identical, removing..."
		rm -v "$pacfile"
	else
		printf "  (V)iew, (S)kip, (R)emove %s, (O)verwrite with %s: [v/s/r/o] " "$file_type" "$file_type"
		while read c; do
			case $c in
				r|R) rm -v "$pacfile"; break ;;
				o|O) mv -v "$pacfile" "$file"; break ;;
				v|V)
				$diffprog "$pacfile" "$file"
				rm -iv "$pacfile"; break ;;
				s|S) break ;;
				*) printf "  Invalid answer. Try again: [v/s/r/o] "; continue ;;
			esac
		done
	fi
done 3< <(cmd)

exit 0

# vim: set ts=2 sw=2 noet: