#! /bin/sh

# Copyright (C) 2007-2020 Free Software Foundation, Inc.

# 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 3 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/>.

export LANG; LANG=C

lang_map="$abs_top_srcdir/libidu/id-lang.map"

errors=

if mkid -m "$lang_map" $abs_top_srcdir/src; then
    files="`fnid | grep -v single_file_token_bug`"

    if xtokid -m "$lang_map" $files |sort -u >ID.xti &&
	lid |sed -e 's/[	 ].*//' |sort -u >ID.lid &&
	cmp -s ID.xti ID.lid
    then
	rm -f ID.xti ID.lid
	echo "Good.  xtokid and lid agree."
    else
	mv ID.xti ID.bad-xti
	mv ID.lid ID.bad-lid
	1>&2 echo "Oops!  xtokid and lid disagree--look in ID.bad-{xti,lid}"
	errors=t
    fi

    xtokid_file=$$.xti
    fid_file=$$.fid

    for file in $files
    do
	if fid $file >$fid_file &&
	    xtokid -m "$lang_map" $file |sort -u >$xtokid_file &&
	    cmp -s $xtokid_file $fid_file;
	then
	    echo "Good.  xtokid and fid agree for $file"
	else
	    1>&2 echo "Oops!  xtokid and fid disagree for $file--look in $file.bad-{xti,fid}"
	    mv $xtokid_file $file.bad-xti
	    mv $fid_file $file.bad-fid
	    errors=t
	fi
    done
    rm -f $xtokid_file $fid_file
else
    1>&2 echo "Oops!  mkid failed."
    errors=t
fi

if mkid -m "$lang_map"; then
    echo "Good.  No crash when dealing with single file"
else
    1>&2 echo "Oops!  mkid failed."
    errors=t
fi

case x$errors in
xt) echo "Some checks failed."
    exit 1;;
*)  echo "All checks successful."
    exit 0;;
esac
