#!/bin/bash
set -e
. tests/lib

t-dependencies bsdgames
t-dependencies man-db git-man
t-tstunt-parsechangelog

t-archive-none example
bv=1.0
t-worktree $bv

: ----- prepare badnesses -----

mkdir af

badattr1 () {
	local filename=$1
	local attrspec=$2
	echo >>af/$filename "Test file with $attrspec"
	printf >>af/$filename 'crlf: \r\n'
	echo >>af/$filename 'id $Id: $'
	echo >>af/$filename 'id $Id: SPLARK $'
	echo >>gitattrs "af/$filename" "$attrspec"
}

badattr () {
	attrname=$1; shift
	badattr1 $attrname-set $attrname
	badattr1 $attrname-unset -$attrname
	badattr1 $attrname-unspec \!$attrname
	local val
	for val in "$@"; do
		badattr1 $attrname=$val $attrname=$val
	done
}

# xxx want to make each of these files into a quilt patch

t-git-config core.eol crlf

badattr	text auto
badattr eol lf crlf
badattr ident

t-git-config filter.dgit-test-crazy-f.smudge '/usr/games/rot13 2'
t-git-config filter.dgit-test-crazy-f.clean  '/usr/games/rot13 24'
t-git-config filter.dgit-test-crazy-f.requrired true

badattr filter dgit-test-crazy-f

badattr diff
badattr merge text binary union
badattr whitespace
badattr export-ignore
badattr export-subst
badattr delta
badattr encoding no-such-encoding

man gitattributes \
| perl -ne 'print $1,"\n" if m/^ *(\w[-a-z]*)$/' \
> grepped-attrs

exec <grepped-attrs
while read attr; do
	badattr $attr
done

sha256sum af/* >sums

# ----- common to source formats -----

sfmt_setup () {
	v=$1
	sfmt=$2

	pdb=$p.$sfmt

	local addpatch=${sfmt}_addpatch
	local convert=${sfmt}_convert

	cp -a $p $pdb-edit
	cd $pdb-edit

	$convert

	dch -v $v -m convert

	rm -rf .git

	cp ../gitattrs .gitattributes
	$addpatch gitattrs

	cp -a ../af .
	$addpatch files

	cp ../sums .
	$addpatch sums

	dpkg-source -b .

	cd ..
}

sfmt_import () {
	inst=$1
	dgitargs=$2
	refname="import.$sfmt-$inst"
	brkerr=stderr.$sfmt-$inst.broken

	mkdir $pdb-import-$inst
	cd $pdb-import-$inst
	git init

	LC_MESSAGES=C \
	t-dgit $dgitargs import-dsc ../${p}_${v}.dsc +$refname.broken \
		2>&1 |tee ../$brkerr
	fgrep 'warning: source tree contains .gitattributes' ../$brkerr
	git checkout $refname.broken
	sha256sum af/* >../$sums.broken

	for s in ../sums ../$sums.broken; do
		sed 's/[0-9a-f]* //' $s >$s.nosums
	done
	diff -U0 ../sums.nosums ../$sums.broken.nosums
	set +e
	diff -U0 ../sums ../$sums.broken
	rc=$?
	set -e
	test $rc = 1

	t-dgit setup-new-tree

	t-dgit $dgitargs import-dsc ../${p}_${v}.dsc +$refname
	git checkout $refname

	sums=sums.$sfmt-$inst

	for f in af/*; do
		git cat-file blob "refs/heads/$refname:$f" \
			| sha256sum \
			| sed -e 's#-$#'$f'#' \
		>>../$sums
	done

	cd ..
	diff -U0 sums $sums
}

: ----- generate the orig -----

tar --exclude=debian --exclude=.git -zcf ${p}_${bv}.orig.tar.gz $p

: ----- test 1.0 native -----

native_addpatch () { :; }
native_convert () { :; }

sfmt_setup 1.1 native
sfmt_import norm

: ----- test 1.0 diff -----

diff_addpatch () { :; }
diff_convert () { :; }

sfmt_setup 1.1 diff
sfmt_import norm

: ----- test "3.0 (quilt)"

quilt_addpatch () {
	pname=$1
	cat >../editor.pl <<END
		next if m/^$/..0;
		s{^(description:).*}{\$1 dgit test patch $pname}i;
		\$_='' if m/^ /;
END
	EDITOR="perl -pi $tmp/editor.pl" dpkg-source -iX --commit . $pname
	test -f debian/patches/$pname
}

quilt_convert () {
	mkdir -p debian/source
	echo '3.0 (quilt)' >debian/source/format
}

sfmt_setup 1.0-1 quilt
sfmt_import norm
sfmt_import absurd --force-import-gitapply-absurd

#t-ok
