#!/bin/sh -e
#
# $Id: sisyphus_cleanup_dups,v 1.11 2005/07/01 14:59:29 ldv Exp $
#
# Copyright (C) 2003-2005  Stanislav Ievlev <inger@altlinux.org>,
#                          Dmitry V. Levin <ldv@altlinux.org>,
#                          Alexey Gladkov <legion@altlinux.org>
# 
# sisyphus_cleanup_dups script.
#
# This file 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA.
#

. /etc/sisyphus/config
. /etc/sisyphus/functions

component=classic
force_yes=
dereference=1

show_help()
{
	cat <<EOF
Usage: $PROG [options]

Valid options are:
  -p, --prefix=DIR          path to repository location;
  -c, --component=NAME      repository component to use;
  -a, --architectures=LIST  architectures to handle;
  -f, --force-yes           force yes answer;
  -P, --no-dereference      do not dereference symbolic links;
  -h, --help                show this text and exit.
EOF
	exit
}

show_usage()
{
	[ -z "$*" ] || Info "$*"
	echo "Try \`$PROG --help' for more information." >&2
	exit 1
}

WORKDIR=
exit_handler()
{
	local rc=$?
	trap - EXIT
	[ -z "$WORKDIR" ] || rm -rf -- "$WORKDIR"
	exit $rc
}

TEMP=`getopt -n $PROG -o a:,c:,f,h,p:,P -l architectures:,apt-config:,component:,force-yes,help,no-dereference,prefix: -- "$@"` || show_usage
eval set -- "$TEMP"

while :; do
	case "$1" in
		-a|--architectures) shift
			ARCHITECTURES="$1"
			;;
		-c|--component) shift
			component="$1"
			;;
		-p|--prefix) shift
			PREFIX="$(readlink -ev "$1")"
			;;
		-f|--force-yes) force_yes=1
			;;
		-P|--no-dereference) dereference=
			;;
		-h|--help) show_help
			;;
		--) shift; break
			;;
		*) Fatal "unrecognized option: $1"
			;;
	esac
	shift
done

[ -n "$ARCHITECTURES" ] || Fatal 'Invalid ARCHITECTURES.'
[ -n "$component" ] || Fatal 'Invalid component.'

trap exit_handler HUP INT QUIT TERM EXIT
WORKDIR="$(mktemp -dt "$PROG.XXXXXXXXXX")"

echo 'Making dups in repositories...'

process_dups()
{
	local rep arch
	rep="$1" && shift
	arch="$1" && shift

	mkdir -p -m700 -- "$WORKDIR/$rep"
	cd "$PREFIX/$rep"
	echo -n "Processing $rep: "
	>"$WORKDIR/$rep/dups" || Fatal "$rep: failed"
	for n in `ls -1 |grep ".$arch.rpm\$" |cut -c1 |LC_COLLATE=C sort -u`; do
		printf %s "$n"
		rpmrdups "$n"*."$arch".rpm >>"$WORKDIR/$rep/dups" ||
			Fatal "$rep/$n: failed"
	done
	echo
}

reps=
for arch in $ARCHITECTURES; do
	reps="$reps $arch/RPMS.$component $arch/SRPMS.$component"
	process_dups "$arch/RPMS.$component" "$arch"
	process_dups "$arch/SRPMS.$component" "src"
done

for rep in $reps; do
	if [ -s "$WORKDIR/$rep/dups" ]; then
		echo "Duplicated files found in \"$rep\" repository:"
		cat -- "$WORKDIR/$rep/dups"
		echo

		cd "$PREFIX/$rep"
		if [ -z "$force_yes" ]; then
			cut -d\  -f2- -- "$WORKDIR/$rep/dups" |
				xargs -r ls -Llt --
			while :; do
				echo "Really purge files listed above? (yes/no)"
				read
				if [ "$REPLY" = no ]; then
					echo Cancelled!
					continue 2;
				fi
				if [ "$REPLY" = yes ]; then
					break;
				fi
			done
		fi
		if [ -n "$dereference" ]; then
			cut -d\  -f2- -- "$WORKDIR/$rep/dups" |
				xargs -r realpath |
				xargs -r rm -v --
		else
			cut -d\  -f2- -- "$WORKDIR/$rep/dups" |
				xargs -r rm -v --		
		fi
	fi
done
