#!/bin/bash
#
#   pacman-optimize
#   @configure_input@
#
#   Copyright (c) 2002-2007 by Judd Vinet <jvinet@zeroflux.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, write to the Free Software
#   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
#   USA.
#

# gettext initialization
export TEXTDOMAIN='pacman'
export TEXTDOMAINDIR='@localedir@'

myver='@PACKAGE_VERSION@'
dbroot='@localstatedir@/lib/pacman/'
lockfile="${dbroot}db.lck"

msg() {
	local mesg=$1; shift
	printf "==> ${mesg}\n" "$@" >&2
}

error () {
	local mesg=$1; shift
	printf "==> ERROR: ${mesg}\n" "$@" >&2
}

usage() {
	printf "pacman-optimize (pacman) %s\n\n" "$myver"
	printf "$(gettext "Usage: %s [pacman_db_root]")\n\n" "$0"
	printf "$(gettext "\
pacman-optimize is a little hack that should improve the performance\n\
of pacman when reading/writing to its filesystem-based database.\n\n")"
	printf "$(gettext "\
Because pacman uses many small files to keep track of packages,\n\
there is a tendency for these files to become fragmented over time.\n\
This script attempts to relocate these small files into one\n\
continuous location on your hard drive. The result is that the hard\n\
drive should be able to read them faster, since the hard drive head\n\
does not have to move around the disk as much.\n")"
}

version() {
	printf "pacman-optimize (pacman) %s\n" "$myver"
	printf "$(gettext "\
Copyright (C) 2002-2007 Judd Vinet <jvinet@zeroflux.org>.\n\n\
This is free software; see the source for copying conditions.\n\
There is NO WARRANTY, to the extent permitted by law.\n")"
}

die() {
	error "$@"
	exit 1
}

die_r() {
	rm -f "$lockfile"
	die "$@"
}

if [ "$1" = "-h" -o "$1" = "--help" ]; then
	usage
	exit 0
fi

if [ "$1" = "-V" -o "$1" = "--version" ]; then
	version
	exit 0
fi

if [ "$1" != "" ]; then
	dbroot="$1"
fi

# make sure diff is installed
if ! type diff >/dev/null 2>&1; then
	die "$(gettext "diff tool was not found, please install diffutils.")"
fi

# make sure pacman isn't running
if [ -f "$lockfile" ]; then
	die "$(gettext "Pacman lock file was found. Cannot run while pacman is running.")"
fi

if [ ! -d "$dbroot" ]; then
	die "$(gettext "%s does not exist or is not a directory.")" "$dbroot"
fi

if [ ! -w "$dbroot" ]; then
	die "$(gettext "You must have correct permissions to optimize the database.")"
fi

workdir=$(mktemp -d /tmp/pacman-optimize.XXXXXXXXXX) ||
	die_r "$(gettext "ERROR: Can not create temp directory for database building.")\n" >&2

# do not let pacman run while we do this
touch "$lockfile"

# step 1: sum the old db
msg "$(gettext "MD5sum'ing the old database...")"
find "$dbroot" -type f | sort | xargs md5sum > "$workdir/pacsums.old"

# step 2: tar it up
msg "$(gettext "Tar'ing up %s...")" "$dbroot"
cd "$dbroot"
bsdtar -czf "$workdir/pacmanDB.tgz" ./
if [ $? -ne 0 ]; then
	rm -rf "$workdir"
	die_r "$(gettext "Tar'ing up %s failed.")" "$dbroot"
fi

# step 3: make and sum the new db
msg "$(gettext "Making and MD5sum'ing the new db...")"
mkdir "$dbroot.new"
bsdtar -zxpf "$workdir/pacmanDB.tgz" -C "$dbroot.new/"
if [ $? -ne 0 ]; then
       rm -rf "$workdir"
       die_r "$(gettext "Untar'ing %s failed.")" "$dbroot"
fi
find "$dbroot.new" -type f | sort | \
		xargs md5sum | sed 's#.new/##' > "$workdir/pacsums.new"

# step 4: compare the sums
msg "$(gettext "Checking integrity...")"
diff "$workdir/pacsums.old" "$workdir/pacsums.new" >/dev/null 2>&1
if [ $? -ne 0 ]; then
	# failed
	# leave /tmp/pacsums.old and .new for checking to see what doesn't match up
	rm -rf "$dbroot.new"
	die_r "$(gettext "Integrity check FAILED, reverting to old database.")"
fi

# step 5: remove the new temporary database and the old one
#         and use the .tgz to replace the old one
msg "$(gettext "Putting the new database in place...")"
rm -rf "$dbroot.new" "$dbroot"/*
bsdtar -zxpf "$workdir/pacmanDB.tgz" -C "$dbroot/"

# remove the lock file, sum files, and .tgz of database
rm -f "$lockfile"
rm -rf "$workdir"

echo
msg "$(gettext "Finished. Your pacman database has been optimized.")"
echo

exit 0

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