#! /usr/bin/perl
#------------------------------------------------------------------------------
#$Author: saulius $
#$Date: 2017-10-22 17:57:19 +0300 (Sun, 22 Oct 2017) $ 
#$Revision: 1143 $
#$URL: svn+ssh://saulius-grazulis.lt/home/saulius/svn-repositories/seminarai/2017-Kaunas-OpenCon/bin/select-language $
#------------------------------------------------------------------------------
#*
# Select text with a specified language from a multilingual text file.
#
# Different languages mys be marked up in the input text by the lines:
#
# %%BEGIN LANGUAGE xx
# Text in language 'xx', or markup for language 'xx'.
# %%END LANGUAGE
#
# Examples:
# $0 en input1.mlt inputs*.mlt
# $0 ru < input1.mltex
#
# Here the 'xx' designator in the '%%BEGIN LANGUAGE' comment must
# corespond to the the first argument of the script. So, the first
# command in the above exmaple ('$0 en input1.mlt') selects lines
# between the '%%BEGIN LANGUAGE en' and '%%END LANGUAGE' designators;
# the second command ('$0 ru < input1.mltex') selects the lines the
# '%%BEGIN LANGUAGE eru' and '%%END LANGUAGE' designators (presumably
# in Russian).
#
# Text that is not enclosed between the '%%BEGIN LANGUAGE ...' and
# '%%END LANGUAGE' lines is always copied to the output.
#
# All output text is written to STDOUT.
#*

if( @ARGV < 1 ) {
    die( "Please specify the language designator " .
         "('en', 'ru', 'lt', or similar) as the first argument" );
}

my $selected_language = shift(@ARGV);

while(<>) {
    if( /^\s*%%LANGUAGE\s+([^\s]+) (.*)/ ) {
        print $2,"\n" if $1 eq $selected_language
    } else {
        print if 
            !( /^\s*%%BEGIN LANGUAGE/../^\s*%%END LANGUAGE/ ) ||
            ( /^\s*%%BEGIN LANGUAGE $selected_language/../^\s*%%END LANGUAGE/ )
    }
}
