copypage.pl 6.37 KB
Newer Older
1 2 3
#!/usr/bin/perl -w

# This script copies the file named on the command line to the translation
4
# named in language.conf, and adds the translation-check header to it.
5 6 7
# It also will create the destination directory if necessary, and create the
# Makefile.  It will do this by simply including the English version -- copied
# Makefiles are not supported anymore for they bear too much space for errors.
8

Peter Karlsson's avatar
Peter Karlsson committed
9
# Originally written 2000-02-26 by Peter Krefting <peterk@debian.org>
10 11 12 13
#
# Modified by Javier Fernandez-Sanguino <jfs@debian.org> to support CVS
# status of files in order to detect removed files or out-of-date CVS copies
#
14 15 16 17
# Modified by Steve McIntyre <93sam@debian.org> to switch to using our
# generic VCS support, as part of the move to git
#
# © Copyright 2000-2018 Software in the public interest, Inc.
18 19 20 21
# This program is released under the GNU General Public License, v2.

# $Id$

22 23 24 25
use FindBin;
FindBin::again();
use lib "$FindBin::Bin/Perl";

26
use File::Path;
27
use Local::VCS;
28
use File::Temp qw/tempfile/;
29
use Getopt::Std;
30

31
# Get configuration
32 33
# Read first two valid lines from language.conf
if (open CONF, "<language.conf")
34
{
35 36 37 38 39
	while (<CONF>)
	{
		next if /^#/;
		chomp;
		$language = $_, next unless defined $language;
40
		$maintainer = $_, next unless defined $maintainer;
41
	}
42
	close CONF;
43 44 45
}
else
{
46
	warn "Unable to open language.conf. Using environment variables...\n";
47 48
}

49 50 51 52 53 54 55 56 57 58
# Values are overwritten by environment variables
if (exists $ENV{DWWW_LANG})
{
	$language = $ENV{DWWW_LANG};
}
if (exists $ENV{DWWW_MAINT})
{
	$maintainer = $ENV{DWWW_MAINT};
}

59 60
my $VCS = Local::VCS->new();

61 62 63 64
# Options
our ($opt_n, $opt_t, $opt_l);
getopts('nm:l:'); 

65 66 67 68 69 70 71 72 73 74
# Values overwritten by commandline
if (defined $opt_m)
{
        $maintainer = $opt_m;
}
if (defined $opt_l)
{
        $language = $opt_l;
}

75 76 77
# Check usage.
if ($#ARGV == -1)
{
78
	print "Usage: $0 [-n] [-l language] [-m maintainer] page ...\n\n";
79
	print "Copies the page from the english/ directory to the $language/ directory\n";
80 81
	print "and adds the translation-check header with the current revision,\n";
	print "optionally adds also the maintainer name.\n";
82
	print "If the directory does not exist, it will be created, and the Makefile\n";
83 84 85 86 87
	print "copied or created, depending on the setting of your language.conf file.\n\n";
	print "The 'english/' part of the input path is optional.\n\n";
        print "If the file already exists in the $language/ repository either\n";
        print "because it was removed (and is in the Attic) or has been removed\n";
        print "locally the program will abort and warn the user (unless '-n' is used)\n";
88 89 90 91
        print "Environment variables:\n";
        print "\tDWWW_LANG\tSets the language for the translation\n";
        print "\t\t(overwrites language.conf definition\n";
        print "\tDWWW_MAINT\tSets maintainer for the translation\n";
92
        print "Options:\n";
93 94 95 96
        print "\t-m\tSets the maintainer for the translation (overwrites environment)\n";
        print "\t-l\tSets the language for the translation (overwrites environment)\n";
        print "\n";

97 98
}

99 100 101 102 103 104
die "Language not defined in DWWW_LANG or language.conf\n"
	if not defined $language;

#warn "Maintainer name not defined in DWWW_MAINT or language.conf\n"
#	if not defined $maintainer;

105
# Loop over command line
106
foreach $page (@ARGV)
107
{
108
	# Check if valid source
109
	if ($page =~ /wml$/ || $page =~ /src$/)
110
	{
111
		&copy($page);
112 113 114 115 116
	}
	else
	{
		print "$page does not seem to be a valid page.\n";
	}
117 118
}

119 120
# Subroutine to copy a page
sub copy
121
{
122 123
	my $page = shift;
	print "Processing $page...\n";
124

125 126 127 128 129
	# Remove english/ from path
	if ($page =~ m[^english/])
	{
		$page =~ s[^english/][];
	}
130

131 132 133
	# Create needed file and directory names
	my $srcfile = "english/$page";		# Source file path
	$dstfile = "$language/$page";		# Destination file path
134

135 136 137 138
	my $srcdir =  $srcfile;
	$srcdir =~ s[(.*/).*][$1];			# Source directory (trailing /)
	my $dstdir =  $dstfile;
	$dstdir =~ s[(.*/).*][$1];			# Desination directory (trailing /)
139

140 141
	my $filename = $srcfile;
	$filename =~ s[$srcdir][];			# Pathless filename
142

143 144
	my $srcmake = $srcdir . "Makefile";	# Name of source Makefile
	my $dstmake = $dstdir . "Makefile";	# Name of destination Makefile
145

146 147 148
	my $dsttitle = $dstfile;
	$dsttitle =~ s/\.wml$/.title/;		# Name of possible title translation

149 150 151
	# Sanity checks
	die "Directory $srcdir does not exist\n" unless -d $srcdir;
	die "File $srcfile does not exist\n"     unless -e $srcfile;
152 153 154 155 156
	if (-e $dstfile)
	{
		warn "File $dstfile already exists\n";
		return;
	}
157

158 159 160 161
	# Check if destination exists, if not - create it
	unless (-d $dstdir)
	{
		print "Destination directory $dstdir does not exist,\n";
162

163
		mkpath([$dstdir],0,0755)
164
			or die "Could not create $dstdir: $!\n";
165 166 167 168 169 170 171 172
	}
	if ( -e $srcmake && ! -e $dstmake )
	{
		print "creating it and making a $dstmake\n";
		open MK, ">", "$dstmake"
			or die "Could not create $dstmake: $!\n";
		print MK "include \$(subst webwml/$language,webwml/english,\$(CURDIR))/Makefile\n";
		close MK;
173
	}
174

175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
	# Check if title translation exists, if so - load it
	my $pagetitle;
	if (-e $dsttitle)
	{
		open TTL, $dsttitle
			or die "Could not read $dsttitle ($!)\n";

		# Scan for title;
		while (<TTL>)
		{
			$pagetitle = $_, last
				if /^<define-tag pagetitle>/;
		}

		close TTL;
	}
	else
	{
		undef $dsttitle;
	}

196
	# Retrieve VCS revision number
197
	my %vcsinfo = $VCS->file_info( $srcfile );
198

199
	if ( not %vcsinfo  or  not exists $vcsinfo{'cmt_rev'}  )
200
	{
201
		die "Could not get revision number for `$srcfile' - bug in script?\n";
202
	}
203

204 205 206 207 208 209 210
	# Open the files
	open SRC, $srcfile
		or die "Could not read $srcfile ($!)\n";

	open DST, ">$dstfile"
		or die "Could not create $dstfile ($!)\n";

211 212
	# Copy the file and insert the revision number
	my $insertedrevision = 0;
213

214
	while (<SRC>)
215
	{
Peter Karlsson's avatar
Peter Karlsson committed
216 217
		next if /\$Id/;

218
		unless ($insertedrevision || /^#/)
219
		{
220
			printf DST qq'#use wml::debian::translation-check translation="%s"', $vcsinfo{'cmt_rev'};
221 222 223
			print DST qq' maintainer="$maintainer"'
				if defined $maintainer;
			print DST qq'\n';
224 225
			$insertedrevision = 1;
		}
226 227 228 229 230 231 232 233
		if (defined $pagetitle && /^<define-tag pagetitle>/)
		{
			print DST $pagetitle;
		}
		else
		{
			print DST $_;
		}
234 235
	}

236 237
	unless ($insertedrevision)
	{
238
		printf DST qq'#use wml::debian::translation-check translation="%s"', $vcsinfo{'cmt_rev'};
239 240 241
		print DST qq' maintainer="$maintainer"'
			if defined $maintainer;
		print DST qq'\n';
242 243
	}

244 245 246 247 248
	close SRC;
	close DST;

	# We're done
	print "Copied $page, remember to edit $dstfile\n";
249 250
	print "and to remove $dsttitle when finished\n"
		if defined $dsttitle;
251
}