From 931ff07e713d7869a16a23dcf770f5fd30c18df8 Mon Sep 17 00:00:00 2001
From: Andres Freund <andres@anarazel.de>
Date: Mon, 8 Mar 2021 14:59:22 -0800
Subject: [PATCH v5 05/16] meson: prereq: move snowball_create.sql creation
 into perl file.

FIXME: deduplicate with Install.pm
---
 src/backend/snowball/Makefile           |  27 +-----
 src/backend/snowball/snowball_create.pl | 110 ++++++++++++++++++++++++
 2 files changed, 113 insertions(+), 24 deletions(-)
 create mode 100644 src/backend/snowball/snowball_create.pl

diff --git a/src/backend/snowball/Makefile b/src/backend/snowball/Makefile
index 50b9199910c..259104f8eb3 100644
--- a/src/backend/snowball/Makefile
+++ b/src/backend/snowball/Makefile
@@ -119,29 +119,8 @@ all: all-shared-lib $(SQLSCRIPT)
 
 include $(top_srcdir)/src/Makefile.shlib
 
-$(SQLSCRIPT): Makefile snowball_func.sql.in snowball.sql.in
-	echo '-- Language-specific snowball dictionaries' > $@
-	cat $(srcdir)/snowball_func.sql.in >> $@
-	@set -e; \
-	set $(LANGUAGES) ; \
-	while [ "$$#" -gt 0 ] ; \
-	do \
-		lang=$$1; shift; \
-		nonascdictname=$$lang; \
-		ascdictname=$$1; shift; \
-		if [ -s $(srcdir)/stopwords/$${lang}.stop ] ; then \
-			stop=", StopWords=$${lang}" ; \
-		else \
-			stop=""; \
-		fi; \
-		cat $(srcdir)/snowball.sql.in | \
-			sed -e "s#_LANGNAME_#$$lang#g" | \
-			sed -e "s#_DICTNAME_#$${lang}_stem#g" | \
-			sed -e "s#_CFGNAME_#$$lang#g" | \
-			sed -e "s#_ASCDICTNAME_#$${ascdictname}_stem#g" | \
-			sed -e "s#_NONASCDICTNAME_#$${nonascdictname}_stem#g" | \
-			sed -e "s#_STOPWORDS_#$$stop#g" ; \
-	done >> $@
+$(SQLSCRIPT): snowball_create.pl Makefile snowball_func.sql.in snowball.sql.in
+	$(PERL) $< --input ${srcdir} --output .
 
 install: all installdirs install-lib
 	$(INSTALL_DATA) $(SQLSCRIPT) '$(DESTDIR)$(datadir)'
@@ -171,4 +150,4 @@ uninstall: uninstall-lib
 	done
 
 clean distclean maintainer-clean: clean-lib
-	rm -f $(OBJS) $(SQLSCRIPT)
+	rm -f $(OBJS) $(SQLSCRIPT) snowball_create.dep
diff --git a/src/backend/snowball/snowball_create.pl b/src/backend/snowball/snowball_create.pl
new file mode 100644
index 00000000000..d9d79f3668f
--- /dev/null
+++ b/src/backend/snowball/snowball_create.pl
@@ -0,0 +1,110 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+use Getopt::Long;
+
+my $output_path = '';
+my $makefile_path = '';
+my $input_path = '';
+
+GetOptions(
+	'output:s'   => \$output_path,
+	'input:s'    => \$input_path) || usage();
+
+# Make sure input_path ends in a slash if needed.
+if ($input_path ne '' && substr($input_path, -1) ne '/')
+{
+	$output_path .= '/';
+}
+
+# Make sure output_path ends in a slash if needed.
+if ($output_path ne '' && substr($output_path, -1) ne '/')
+{
+	$output_path .= '/';
+}
+
+GenerateTsearchFiles();
+
+sub usage
+{
+	die <<EOM;
+Usage: snowball_create.pl --input/-i <path> --input <path>
+    --output        Output directory (default '.')
+    --input         Input directory
+
+snowball_create.pl creates snowball.sql from snowball.sql.in
+EOM
+}
+
+sub GenerateTsearchFiles
+{
+	my $target = shift;
+	my $output_file = "$output_path/snowball_create.sql";
+
+	print "Generating tsearch script...";
+	my $F;
+	my $D;
+	my $tmpl = read_file("$input_path/snowball.sql.in");
+	my $mf   = read_file("$input_path/Makefile");
+
+	open($D, '>', "$output_path/snowball_create.dep")
+	  || die "Could not write snowball_create.dep";
+
+	print $D "$output_file: $input_path/Makefile\n";
+	print $D "$output_file: $input_path/snowball.sql.in\n";
+	print $D "$output_file: $input_path/snowball_func.sql.in\n";
+
+	$mf =~ s{\\\r?\n}{}g;
+	$mf =~ /^LANGUAGES\s*=\s*(.*)$/m
+	  || die "Could not find LANGUAGES line in snowball Makefile\n";
+	my @pieces = split /\s+/, $1;
+	open($F, '>', $output_file)
+	  || die "Could not write snowball_create.sql";
+
+	print $F "-- Language-specific snowball dictionaries\n";
+
+	print $F read_file("$input_path/snowball_func.sql.in");
+
+	while ($#pieces > 0)
+	{
+		my $lang    = shift @pieces || last;
+		my $asclang = shift @pieces || last;
+		my $txt     = $tmpl;
+		my $stop    = '';
+		my $stopword_path = "$input_path/stopwords/$lang.stop";
+
+		if (-s "$stopword_path")
+		{
+			$stop = ", StopWords=$lang";
+
+			print $D "$output_file: $stopword_path\n";
+		}
+
+		$txt =~ s#_LANGNAME_#${lang}#gs;
+		$txt =~ s#_DICTNAME_#${lang}_stem#gs;
+		$txt =~ s#_CFGNAME_#${lang}#gs;
+		$txt =~ s#_ASCDICTNAME_#${asclang}_stem#gs;
+		$txt =~ s#_NONASCDICTNAME_#${lang}_stem#gs;
+		$txt =~ s#_STOPWORDS_#$stop#gs;
+		print $F $txt;
+		print ".";
+	}
+	close($F);
+	close($D);
+	print "\n";
+	return;
+}
+
+
+sub read_file
+{
+	my $filename = shift;
+	my $F;
+	local $/ = undef;
+	open($F, '<', $filename) || die "Could not open file $filename\n";
+	my $txt = <$F>;
+	close($F);
+
+	return $txt;
+}
-- 
2.23.0.385.gbc12974a89

