#!/bin/bash

#############################################################################
# Copyright (C) 2012-2016 Franz Inc, Oakland, CA - All rights reserved.     #
#                                                                           #
# The software, data and information contained herein are proprietary       #
# to, and comprise valuable trade secrets of, Franz, Inc.  They are         #
# given in confidence by Franz, Inc. pursuant to a written license          #
# agreement, and may be stored and used only in accordance with the terms   #
# of such license.                                                          #
#                                                                           #
# Restricted Rights Legend                                                  #
# ------------------------                                                  #
# Use, duplication, and disclosure of the software, data and information    #
# contained herein by any agency, department or entity of the U.S.          #
# Government are subject to restrictions of Restricted Rights for           #
# Commercial Software developed at private expense as specified in          #
# DOD FAR Supplement 52.227-7013 (c) (1) (ii), as applicable.               #
#############################################################################

set -e

function usage {
    cat <<EOF
Usage: $0 [ --verbose ] [ --host HOST ] [ --port PORT ] --user USER --password PASSWORD [ --catalog CATALOG ] repo

HOST defaults to localhost
PORT defaults to 10035
CATALOG defaults to the root catalog

This script verifies that the triple count (as returned by the 'size'
REST endpoint) matches with the count of triples returned by a
full-scan query.  

If the counts match, the scripts terminates with
exit status 0.  If --verbose is specified, the script reports the
triple count before exiting.   

If the counts do not match, a complaint is written to stdout and the
script terminates with exit status 1.

This script is only suitable if there is no static attribute filter
in place that would prevent the accessing account from seeing all
triples in the database.

EOF
    exit 1
}

source `dirname $0`/scripts-common.sh

if [ $# -ne 1 ]; then
    usage
fi

repo="$1"
base=`make_base_url`
    
# echo repo: $repo

# Verify that something basic works
if ! do_curl --fail $base/size >/dev/null 2>&1; then
    # The request didn't work.  Reissue it w/o redirecting stdout
    # so that the caller will know what's going on.
    do_curl $base/size
    echo
    exit 1
fi

# Create a session so that we can get consistent results.
session_url=`do_curl -X POST $base/session`

# Make sure to terminate the session on exit.
trap "do_curl -X POST $session_url/session/close" EXIT

# Get triple count 
size=`do_curl $session_url/size`

# Perform a full scan to count actual triples
count=`do_curl --header "Accept: text/integer" $session_url/statements`

if [ "$size" = "$count" ]; then
    # All is well
    if [ "$verbose" ]; then
	echo Triple count ok: $size
    fi
    exit 0
else
    echo "Size $size does not match count $count"
    exit 1
fi
