# Testsuite makefile for GNU sed

SHELL = /bin/sh

# These are only fallback values. They are usually overridden by runtest.
srcdir = .
SED = ../sed/sed

TIME=
#TIME=time

SEDENV_2 = LC_ALL=C
SEDENV = $(SEDENV_2) $(TIME)
TR = $(SEDENV_2) tr

CMP=cmp
RM=rm -f

SKIP = :>$@.skip; exit 77

enable sep inclib 8bit 8to7 newjis xabcx dollar noeol bkslashes \
numsub head madding mac-mf empty xbxcx xbxcx3 recall recall2 xemacs \
appquit fasts uniq manis linecnt khadafy allsub flipcase space modulo \
y-bracket y-newline insert::
	$(SEDENV) $(SED) -f $(srcdir)/$@.sed \
		< $(srcdir)/$@.inp | $(TR) -d \\r > $@.out 
	$(CMP) $(srcdir)/$@.good $@.out 
	@$(RM) $@.out 

0range::
	$(SEDENV) $(SED) -s -f $(srcdir)/$@.sed \
		< $(srcdir)/$@.inp | $(TR) -d \\r > $@.out 
	$(CMP) $(srcdir)/$@.good $@.out 
	@$(RM) $@.out 

badenc::
	LANG=ru_RU.UTF-8 $(TIME) $(SED) -nf $(srcdir)/$@.sed \
		< $(srcdir)/$@.inp | $(TR) -d \\r > $@.out 
	$(CMP) $(srcdir)/$@.good $@.out 
	LANG=it_IT.UTF-8 $(TIME) $(SED) -nf $(srcdir)/$@.sed \
		< $(srcdir)/$@.inp | $(TR) -d \\r > $@.out 
	$(CMP) $(srcdir)/$@.good $@.out 
	LANG=en_US.UTF-8 $(TIME) $(SED) -nf $(srcdir)/$@.sed \
		< $(srcdir)/$@.inp | $(TR) -d \\r > $@.out 
	$(CMP) $(srcdir)/$@.good $@.out 
	LANG=en_GB.UTF-8 $(TIME) $(SED) -nf $(srcdir)/$@.sed \
		< $(srcdir)/$@.inp | $(TR) -d \\r > $@.out 
	$(CMP) $(srcdir)/$@.good $@.out 
	@$(RM) $@.out 

# Try with ru_RU.UTF-8.  If it is presumably not installed, see if the current
# locale is UTF-8 and run it in the current locale.
utf8-1 utf8-2 utf8-3 utf8-4::
	echo "LANG=ru_RU.UTF-8" \
	  "$(TIME) $(SED) -f $(srcdir)/$@.sed" \
	  "< $(srcdir)/$@.inp | $(TR) -d \\r > $@.out"; \
	LANG=ru_RU.UTF-8 \
	  $(TIME) $(SED) -f $(srcdir)/$@.sed \
		< $(srcdir)/$@.inp | $(TR) -d \\r > $@.out; \
	$(CMP) $(srcdir)/$@.good $@.out && exit 0; \
	$(CMP) $(srcdir)/$@.inp $@.out || exit 1; \
	locale > $@.info 2>/dev/null || { rm $@.info; $(SKIP); }; \
	. $@.info; rm $@.info; \
	case "$$LC_CTYPE" in \
	  *UTF-8 | *UTF8 | *utf8 | *utf-8) \
	    echo "$(TIME) $(SED) -f $(srcdir)/$@.sed" \
	      " < $(srcdir)/$@.inp | $(TR) -d \\r > $@.out"; \
	    $(TIME) $(SED) -f $(srcdir)/$@.sed \
		< $(srcdir)/$@.inp | $(TR) -d \\r > $@.out; \
	    $(CMP) $(srcdir)/$@.good $@.out && exit 0; \
	    $(CMP) $(srcdir)/$@.inp $@.out || exit 1 ;; \
	  *) ;; \
	esac; \
	$(SKIP)

# This checks for a bug in 3.02 and 3.02.80
stdin::
	($(SEDENV) $(SED) d; $(SEDENV) $(SED) G) < $(srcdir)/numsub.inp \
	  | $(TR) -d \\r > $@.1out 
	$(SEDENV) cat $(srcdir)/numsub.inp \
	  | ($(SEDENV) $(SED) d; $(SEDENV) $(SED) G) | $(TR) -d \\r > $@.2out 
	$(CMP) $@.1out $@.2out 
	@$(RM) $@.1out $@.2out 

inplace-hold::
	echo x > $@.1out
	echo y > $@.2out
	echo > $@.out
	$(SEDENV) $(SED) -i x $@.1out $@.2out 
	$(CMP) $@.1out $@.out 
	$(CMP) $@.2out $@.out 
	@$(RM) $@.1out $@.2out $@.out

cv-vars classes middle dc distrib factor numsub2 numsub3 numsub4 numsub5 \
insens::
	$(SEDENV) $(SED) -n -f $(srcdir)/$@.sed \
	  < $(srcdir)/$@.inp | $(TR) -d \\r > $@.out 
	$(CMP) $(srcdir)/$@.good $@.out 
	@$(RM) $@.out 

noeolw::
	$(SEDENV) $(SED) -n -f $(srcdir)/$@.sed \
	  $(srcdir)/noeol.inp $(srcdir)/noeol.inp | $(TR) -d \\r > $@.out 
	$(CMP) $(srcdir)/$@.good $@.out 
	$(TR) -d \\r <  $@.1out | $(CMP) $(srcdir)/$@.1good -
	$(TR) -d \\r <  $@.2out | $(CMP) $(srcdir)/$@.2good -
	@$(RM) $@.1out $@.2out $@.out

subwrite::
	$(SEDENV) $(SED) -f $(srcdir)/$@.sed \
	  < $(srcdir)/$@.inp | $(TR) -d \\r > $@.1out 
	$(CMP) $(srcdir)/subwrt1.good $@.1out
	$(TR) -d \\r < $@.wout | $(CMP) $(srcdir)/subwrt2.good -
	@$(RM) $@.1out $@.wout

bsd::
	$(SEDENV_2) sh $(srcdir)/$@.sh '$(TIME) $(SED)' $@.out
	$(TR) -d \\r < $@.out | $(CMP) $(srcdir)/$@.good -
	@$(RM) $@.out 

writeout::
	$(SEDENV) $(SED) -f $(srcdir)/$@.sed \
	  < $(srcdir)/$@.inp | $(TR) -d \\r > $@.1out 
	$(CMP) $(srcdir)/wrtout1.good $@.1out 
	$(TR) -d \\r < $@.wout | $(CMP) $(srcdir)/wrtout2.good -
	@$(RM) $@.1out $@.wout

readin::
	echo MOO | $(SEDENV) $(SED) b > $@.in2
	$(SEDENV) $(SED) -f $(srcdir)/$@.sed \
	  < $(srcdir)/$@.inp | $(TR) -d \\r > $@.out 
	$(CMP) $(srcdir)/$@.good $@.out 
	@$(RM) $@.out $@.in2

eval.in2: $(srcdir)/eval.inp
	cat $(srcdir)/eval.inp > $@

eval:: eval.in2
	$(SEDENV) $(SED) -f $(srcdir)/$@.sed \
	  < $(srcdir)/$@.inp | $(TR) -d \\r > $@.out 
	$(CMP) $(srcdir)/$@.good $@.out 
	@$(RM) $@.out 

binary binary2 binary3::
	$(SEDENV) $(SED) -n -f $(srcdir)/$@.sed \
	  < $(srcdir)/binary.inp | $(TR) -d \\r > $@.out 
	$(CMP) $(srcdir)/binary.good $@.out 
	@$(RM) $@.out 

#
# cmdlines targets
#

help::
	$(SEDENV) $(SED) --help | grep E-mail > /dev/null 2>&1
	$(SEDENV) $(SED) --help | $(SEDENV) $(SED) '1s/ [^ ]* / sed /; /^E-mail/,$$d' > $@.1out 
	$(SEDENV) $(SED) 2>&1 | $(SEDENV) $(SED) '1s/ [^ ]* / sed /' > $@.2out || :
	$(CMP) $@.1out $@.2out 
	@$(RM) $@.1out $@.2out

version::
	$(SEDENV) $(SED) --version 2>&1 | $(TR) -d \\r > $@.out
	$(CMP) $@.good $@.out 
	@$(RM) $@.out

file::
	$(SEDENV) $(SED) --file=$(srcdir)/newjis.sed \
		< $(srcdir)/newjis.inp | $(TR) -d \\r > $@.out 
	$(CMP) $(srcdir)/newjis.good $@.out 
	@$(RM) $@.out 

quiet::
	$(SEDENV) $(SED) --quiet -f $(srcdir)/cv-vars.sed \
		< $(srcdir)/cv-vars.inp | $(TR) -d \\r > $@.out 
	$(CMP) $(srcdir)/cv-vars.good $@.out 
	@$(RM) $@.out 

# The following target is not used in super sed builds (only GNU sed)

bug-regex7$(EXEEXT) bug-regex8$(EXEEXT) bug-regex9$(EXEEXT) \
bug-regex10$(EXEEXT) bug-regex11$(EXEEXT) bug-regex12$(EXEEXT) \
bug-regex13$(EXEEXT) bug-regex14$(EXEEXT) bug-regex15$(EXEEXT) bug-regex16$(EXEEXT) \
bug-regex21$(EXEEXT) runtests$(EXEEXT) runptests$(EXEEXT)::
	echo "$(SEDENV) ./$@ > `echo $@ | $(SED) s/$(EXEEXT)$$/.log/`"
	@$(SEDENV) ./$@ > `echo $@ | $(SED) s/$(EXEEXT)$$/.log/`

tst-pcre$(EXEEXT)::
	$(SEDENV) ./tst-pcre $(srcdir)/PCRE.tests > tst-pcre.log

tst-boost$(EXEEXT)::
	$(SEDENV) ./tst-boost $(srcdir)/BOOST.tests > tst-boost.log

tst-rxspencer$(EXEEXT)::
	$(SEDENV) ./tst-rxspencer $(srcdir)/SPENCER.tests > tst-spencer.log

tst-regex2$(EXEEXT)::
	$(SEDENV) ./tst-regex2 $(srcdir)/tst-regex2.c > tst-regex2.log

# The following target is not used in GNU sed builds (only super-sed)

pcretest$(EXEEXT)::
	$(SEDENV) ./pcretest $(srcdir)/pcre1.inp pcre1.out
	$(CMP) $(srcdir)/pcre1.good pcre1.out
	#$(SEDENV) ./pcretest -p $(srcdir)/pcre1.inp pcre1p.out
	#$(CMP) $(srcdir)/pcre1p.good pcre1p.out
	$(SEDENV) ./pcretest -P $(srcdir)/pcre2.inp pcre2.out
	$(CMP) $(srcdir)/pcre2.good pcre2.out
	$(SEDENV) ./pcretest -P -p $(srcdir)/pcre2.inp pcre2p.out
	$(CMP) $(srcdir)/pcre2p.good pcre2p.out
	$(SEDENV) ./pcretest $(srcdir)/pcre3.inp pcre3.out
	$(CMP) $(srcdir)/pcre3.good pcre3.out
	$(SEDENV) ./pcretest -p $(srcdir)/pcre3.inp pcre3p.out
	$(CMP) $(srcdir)/pcre3p.good pcre3p.out
	@$(RM) pcre*.out

.PHONY: \
bug-regex7$(EXEEXT) bug-regex8$(EXEEXT) bug-regex9$(EXEEXT) \
bug-regex10$(EXEEXT) bug-regex11$(EXEEXT) bug-regex12$(EXEEXT) \
bug-regex13$(EXEEXT) bug-regex14$(EXEEXT) bug-regex15$(EXEEXT) bug-regex16$(EXEEXT) \
bug-regex21$(EXEEXT) runtests$(EXEEXT) runptests$(EXEEXT) tst-regex2$(EXEEXT) \
tst-pcre$(EXEEXT) tst-boost$(EXEEXT) tst-rxspencer$(EXEEXT) pcretest$(EXEEXT)
