# libchop -- a utility library for distributed storage and data backup
# Copyright (C) 2011, 2012  Ludovic Courtès <ludo@gnu.org>
#
# Libchop 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 3 of the License, or
# (at your option) any later version.
#
# Libchop 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 libchop.  If not, see <http://www.gnu.org/licenses/>.

# Test the `chop-backup' command.

source "${srcdir:-$PWD}/lib.sh"

DB_FILE=",,backup.db"
TMP_FILE=",,backup.tmp"
XDG_CACHE_HOME="$PWD/backup-command-cache"
export XDG_CACHE_HOME

chop_CLEANUP_HOOK='rm -rf "$XDG_CACHE_HOME" "$DB_FILE" "$TMP_FILE"'

# Start from a clean state.
chop_cleanup

chop-backup --version

index=`chop-backup "$DB_FILE" "$top_srcdir/include"`
chop_fail_if ! chop-backup --list "$DB_FILE" | grep '^d.*chop$'
chop_fail_if ! chop-backup --list=recursive "$DB_FILE" '"$index"' | \
    grep '^ *-r.* indexers\.h$'
chop_fail_if ! chop-backup --show "$DB_FILE" | grep '^(directory'
chop_fail_if ! chop-backup --show "$DB_FILE" '"$index"' | grep '^(directory'
chop_fail_if ! chop-backup --check "$DB_FILE"
chop_fail_if ! chop-backup --check "$DB_FILE" '"$index"'

index_makefile_am=`chop-backup -lv "$DB_FILE" | grep ' Makefile\.am' | \
  sed -e's/^.* Makefile\.am \(.*\)$/\1/g'`
chop_fail_if ! chop-backup -s "$DB_FILE" '"$index_makefile_am"' > "$TMP_FILE"
chop_fail_if ! cmp "$TMP_FILE" "$top_srcdir/include/Makefile.am"

index_indexers_h=`chop-backup -lv,r "$DB_FILE" | grep ' indexers\.h' | \
  sed -e's/^.* indexers\.h \(.*\)$/\1/g'`
chop_fail_if ! chop-backup -s "$DB_FILE" '"$index_indexers_h"' > "$TMP_FILE"
chop_fail_if ! cmp "$TMP_FILE" "$top_srcdir/include/chop/indexers.h"

rm -f "$TMP_FILE"
chop_fail_if ! chop-backup -r "$DB_FILE" "$TMP_FILE"
find "$TMP_FILE" | xargs chmod +w && rm -rf "$TMP_FILE"
chop_fail_if ! chop-backup -r "$DB_FILE" '"$index"' "$TMP_FILE"

# Select all the files that were restored, along with a fixed list of
# files to make sure $TMP_FILE is not empty or lacking.
for file in Makefile.am chop/objects.h chop/block-indexers.h chop/cipher.h \
    `cd "$TMP_FILE" ; find -type f`
do
    chop_fail_if ! cmp "$TMP_FILE/$file" "$top_srcdir/include/$file"
done

# Doing it a second and third time should yield the same index
# (assuming the directory and its meta-data were left unchanged.)
index2=`chop-backup "$DB_FILE" "$top_srcdir/include"`
index3=`chop-backup -bv "$DB_FILE" "$top_srcdir/include" 2> ,,backup-verbose-output`
chop_fail_if test '"$index2"' != '"$index"'
chop_fail_if test '"$index3"' != '"$index"'
chop_fail_if ! grep '"chop.h.*->.*unchanged"' ,,backup-verbose-output
rm -rf ,,backup-verbose-output

# Thus, the `include' directory should appear only once in the recent
# backup list.
chop_fail_if test '`chop-backup --recent | grep /include | wc -l`' -ne 1

# Remove the root block.
root_block="`echo $index | sed -es'|^.*,\([a-z2-7]\+\)/.*$|\1|g'`"
rm "$DB_FILE/`echo $root_block | cut -c 1-2`/`echo $root_block | cut -c 3-`"
chop_fail_if chop-backup --check '"$DB_FILE"' '"$index"'

# Re-run the backup: nothing has changed, so it should just re-create
# the root block, under the same name.
index4=`chop-backup "$DB_FILE" "$top_srcdir/include"`
chop_fail_if test '"$index4"' != '"$index"'
chop_fail_if ! chop-backup --check '"$DB_FILE"' '"$index"'

# Remove blocks at random.
rm -f "$DB_FILE/"*/*2*

# Repair them.
index5=`chop-backup -br "$DB_FILE" "$top_srcdir/include"`
chop_fail_if '"$index5"' != '"$index"'
chop_fail_if ! chop-backup --check '"$DB_FILE"' '"$index"'

# At this point, $TMP_FILE contains a copy of $top_srcdir/include.
# Create a backup of it, and add/delete files.
index=`chop-backup "$DB_FILE" "$TMP_FILE"`

chmod +w "$TMP_FILE"
echo "nouveau" > "$TMP_FILE/added-file"
rm "$TMP_FILE/Makefile.am"
chmod 700 "$TMP_FILE/chop"

index2=`chop-backup "$DB_FILE" "$TMP_FILE"`

added="`chop-backup --log "$DB_FILE" | grep 'A .* added-file'`"
deleted="`chop-backup --log "$DB_FILE" | grep 'D .* Makefile.am'`"
perms="`chop-backup --log "$DB_FILE" "$index2" | grep 'P .* chop'`"

chop_fail_if test '"x$added"' = "x"
chop_fail_if test '"x$deleted"' = "x"
chop_fail_if test '"x$perms"' = "x"

find "$TMP_FILE" | xargs chmod +w && rm -rf "$TMP_FILE"

chop_cleanup
