#!/usr/bin/perl -w
##############################################################################################
# Program: msplit (a utility for splitting STeX documents along module boundaries)
##############################################################################################
# $URL: svn://kwarc.faculty.iu-bremen.de/kohlhase/kwarc/projects/content/bin/msplit$ 
# $Rev: 3722 $; last modified by $Author: $ 
# $Date: 2005-07-27 03:22:19 +0200 (Wed, 27 Jul 2005) $
# Copyright (c) 2005 Ioan Sucan
##############################################################################################
# This program 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 2
# of the License, or (at your option) any later version.
# 
# This program 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 [http://www.gnu.org/copyleft/gpl.html]
# for more details.
##############################################################################################
use strict;
use Getopt::Long;
use File::Path;
use Pod::Usage;

my $input='-', my $output='-', my $path='', my $verbose=0, my $stop_at_end=0;
my $flag = "include";
my $header='', my @hdr_data;

sub texf {
    my $fn = shift;
    $fn.='.tex' if $fn !~ /\.tex$/;
    return $fn; }

sub write_file_data{
    my $filename = shift;
    open(FS,$filename) or die "Cannot write $filename\n";
    print FS @_;
    close(FS); }

# remove the last component in a path
sub justpath{
    my $arg = shift;
    return '' if ($arg !~ m!\/!);
    while ($arg !~ m!\/$!) {chop($arg);}
    return $arg; }
# end justpath


sub go {
    my ($input, $output) = @_; 
    my $path = justpath($output);

    my @module_file=();
    my @out_data=();

    print "Splitting $input...\n" if $verbose;
    `mv $output $output~` if (-e $output);
    $input="$input~" if ($input eq $output);
    
    open(FIN,$input) or die "Can't open $input\n";
        my $in_module=0; my $path_name;
    foreach (<FIN>){    
	chomp;
	/([^%]*)((%.*)?)/;
	($_,my $comment)=($1,$2);
	if (/\\begin\s*\{module\}/){
	    $_ =~ s/\s//g;
	    /id=([\w-]*)/;	
	    my $module_name = $1;
	    $path_name = "$path$module_name.tex";	    
	    print "$path_name\n" if $verbose;
	    `mv $path_name $path_name~` if (-e $path_name);
	    push(@module_file,@hdr_data) if $header;
	    push(@module_file,"\n$_$comment\n");
	    push(@out_data,"\\requiremodules[$flag]{$module_name}\n");
	    $in_module=1;
	    next; } 
	if (/\\end\s*\{module\}/){
	    $_ =~ s/\s//g;
	    push(@module_file,"$_$comment\n");
	    mkpath(justpath($path_name));
	    write_file_data(">$path_name",@module_file);
	    $in_module=0;	    
	    next; }
	if ($stop_at_end && /\\end\s*\{document\}/){
	    $_ =~ s/\s//g;
	    push(@out_data,"$_$comment\n");
	    last; }
	$_="$_$comment\n";
	if ($in_module) {push(@module_file,$_);}
	else {push(@out_data,$_); }}
    close(FIN);
    mkpath($path) if $path;
    write_file_data(">$output",@out_data); }

GetOptions("output=s" => \$output,
	   "path=s" => \$path,
	   "header=s" => \$header,
	   "exclude" => sub { $flag = "exclude"; },
	   "verbose" => sub { $verbose=1; },
	   "stop" => sub { $stop_at_end=1; },
	   "help" => sub { pod2usage(2)});

$input = $ARGV[0] if ($#ARGV>=0);
die "Input file does not exist\n" if (! defined $input || ! -e $input);
$output = $input if (! defined $output);

if ($header && -e $header) {
    open(HDR,$header);
    @hdr_data = <HDR>;
    close(HDR); }

$path.='/' if $path && $path !~ m!\/$!;
go($input,$path.$output);
print "Done.\n" if $verbose;

__END__


=head1 SYNOPSIS

msplit <inputfile> [options]

Options:
   
    --output      filename of new main .tex file; default: same as input
    --path        path where to generate the new documents; '.' by default
    --header      file to be copied at beginning of all new module files
    --exclude     modules exluded by default; if not mentioned, modules 
                  are included by default
    --verbose     verbose on
    --stop        stop when \end{document} is found; default is to go on
    --help        this screen

    

    This program splits a .tex file into more files, each containing a module.
    Example usage:
    ./msplit slides.tex -o sl.tex -p ./modules -s
