#! /bin/sh
#! -*-perl-*-
eval 'exec perl -x -wS $0 ${1+"$@"}'
    if 0;
# This file is part of GNU Rush.
# Copyright (C) 2009-2024 Sergey Poznyakoff
#
# GNU Rush 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, or (at your option)
# any later version.
#
# GNU Rush 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 GNU Rush.  If not, see <http://www.gnu.org/licenses/>.

use strict;
use warnings;
use File::Basename;
use Getopt::Long qw(:config gnu_getopt no_ignore_case require_order);

sub usage {
    my $code = shift;
    my $name = basename($0);
    select(STDERR) if $code;
    print "usage: $name [-o FILE] [--output=FILE] INPUT\n";
    print <<'EOF'
Extracts translatable strings from rush.rc to PO file

Options:

    -o, --output=FILE    write PO to FILE (default - standard output)

Report bugs to <bug-rush@gnu.org>	
EOF
    ;
    exit $code
}

sub initial_entry {
    print <<'EOF';
# SOME DESCRIPTIVE TITLE.
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: rush-config ADDITIONAL-DATA\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI +ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=CHARSET\n"
"Content-Transfer-Encoding: 8bit\n"
""

EOF
}

sub dequote {
    my $arg = shift;
    $arg =~ s/^\"(.*)\"$/$1/;
    $arg =~ s/\\%/\\\\%/g;
    return $arg
}

sub scan {
    my $filename = shift || '-';
    my $fd;
    if ($filename eq '-') {
	open($fd, '<&', \*STDIN) or
	    die "can't dup stdin: $!";
    } else {
	open($fd, '<', $filename) or
	    die "can't open file $filename: $!";
    }
    my $line = 0;
    my $start_line;
    my $cont_lines = 0;
    my $version;
    while (<$fd>) {
	chomp;

	if (/\\$/) {
	    $cont_lines++;
	    chop;
	    $_ .= <$fd>;
	    redo;
	}

	$start_line = $line + 1;
	$line = $start_line + $cont_lines;
	$cont_lines = 0;

	s/\s+$//;
	s/^\s+//;
	if (/^(#.*)?$/) {
	    next;
	} elsif (!defined($version)) {
	    if (/^rush\s+(\d+\.\d+)$/) {
		$version = $1;
		die "unsupported version: $version" unless $version eq '2.0';
		$version = 2;
		next;
	    } else {
		$version = 1;
	    }
	}

	if (/^include\s+(.+)$/) {
	    my $s = $1;
	    if ($version == 2) {
		$s = dequote($s);
	    }
	    scan($1);
	    next;
	}
	if (s/^exit\s+(\d+\s+)?//) {
	    if ($version == 1) {
		if (s/^\@\@/\@/) {
		    # ok
		} elsif (/^@/) {
		    next
	        }
	    } elsif (!/^\".*\"$/) {
		next
	    }
	} elsif (!s/^(usage|nologin|config)-error\s+//) {
	    next;
	}

	if ($version == 1) {
	    s{([\\"])}{\\$1}g
	} else {
	    $_ = dequote($_)
	}

	printf("#: %s:%d\n", $filename, $start_line);
	printf("msgid \"%s\"\n", $_);
	printf("msgstr \"\"\n\n");
    }
    close $fd;
}

my $outfile;
GetOptions('output|o=s' => \$outfile,
	   'help|usage|h' => sub { usage(0) }
    ) or usage(1);

my $input = shift @ARGV or usage(1);
usage(1) if @ARGV;

if ($input && $input ne '-') {
    die "input file $input does not exist"
	unless -f $input;
    die "input file $input is unreadable"
	unless -r $input;
} else {
    $input = '-';
}

if ($outfile) {
    open(STDOUT, '>', $outfile) or die "can't open output file $outfile: $!";
}

initial_entry();
scan($input);
