#!/usr/bin/perl -w
use strict;
use File::Copy;

#Copyright 2006-2007 John C. Vernaleo
#Unfortunately, I am not comfortable putting unobscured email addresses
#on the web, but these shouldn't be too hard to figure out.
#
#		(my_first_name)@netpurgatory.com
#
#    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 for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA


########## I still don't handle @extras in a good way since arxiv will
#probably only work correctly for apj type submissions.
# 08/20/2007
# jcv
# arxiv version now removes \clearpage that apj seems to like so
# emulateapj actually looks almost correct.
# 08/03/2007
# jcv
# Fixed bug for multipanel figures.
# 05/30/2007
# jcv
# Trying to handle different types of submissions a bit better
# And and f1a.eps, f2b.eps, etc. for multiple figures in one graphics
# command.
# 09/22/2006
# jcv
# Made things a whole lot more modular
# And put some stuff to use for conference proceedings.
# 03/07/2006
# jcv
# Make some allowances for B&W version of figures as well as color.
# Also a preliminary version of astroph submission script
# 02/02/2006
# jcv
# Now it makes a tarball to submit and checks for response file.
# 01/24/2006
# jcv
# Some additions. Now files to submit are put in their own directory.
# And it checks to see if things are in preprint form.
# 11/08/2005
# jcv
# script to help apj submissions
# takes a tex file, gets all of the apj ready (plotone)
# figures and make a properly named copy of the figure
# and makes a new tex file that references the new f1.eps and so on.

my $type=$ARGV[0];
my $basename;
my $bbl;
my $submitbase;
my $submitname;
my $special;
my $graphicscmd;
my @data;
my @extras;
my $figbase;

unless(defined($type)){
    print "Please choose submission type: apj, arxiv, or conf: ";
    chomp($type=<STDIN>);
}

sub apj_like{
    $basename="paper";
    $bbl=$basename.".bbl";
    $submitbase="ms";
    $submitname=$submitbase.".tex";
    $figbase="f";
    $graphicscmd="plotone";
}
sub conf_like{
    $basename="vernaleo";
    $bbl=$basename.".bbl";
    $submitbase="vernaleo";
    $submitname=$submitbase.".tex";
    $figbase=$submitname."F";
    $graphicscmd="includegraphics";
}

&apj_like;

if($type eq "apj"){
    &apj;
    print "Paper prepared for APJ submission\n";
}

if($type eq "arxiv"){
    &arxiv;
    print "Paper prepared for arXiv submission\n";
}

if($type eq "conf"){
    &conf;
    print "Paper prepared for conference proceedings\n";
}

if(($type ne "apj")&&($type ne "arxiv")&&($type ne "conf")){
    print "Invalid submission type!\n";
}

#################################Subroutines##########################
sub apj{
    unless (-e "submit")
    {
	mkdir "submit" or die "Cannot make directory: $!";
    }
    my $texname=$basename.".tex";
    open DATA, "< $texname" or die("Cannot open $texname: $!");
    @data=<DATA>;
    close DATA;
    chomp(@data);
    $special="BW";
    @extras=("README",$bbl,"response");
    
    &get_figures;
    &extra_files;

    foreach(@data){
	if(($_=~"documentclass")&&($_!~"\%"))
	{
	    unless($_=~"preprint"){
		print "documentclass is not in preprint form apj wants.\n$_\n";
	    }
	}
    }

    &tarball;

}

sub arxiv{
    unless (-e "submit")
    {
	mkdir "submit" or die "Cannot make directory: $!";
    }
    my $i;
    my $texname=$basename.".tex";
    open DATA, "< $texname" or die("Cannot open $texname: $!");    
    @data=<DATA>;
    close DATA;
    chomp(@data);
    $special="small";
    @extras=($bbl, "apj.bst", "emulateapj.cls");

    for($i=0;$i<=$#data;$i++){
	if(($data[$i]=~"documentclass")&&($data[$i]!~"\%"))
	{
	    if($data[$i]=~"preprint"){
		print "documentclass is in preprint form, change to emulate apj or something nicer.\n$data[$i]\n";
	    }
	}
	if($data[$i]=~"clearpage")
	{
	    $data[$i]="%\clearpage\n";
	}
    }

    &get_figures;
    &extra_files;

    &tarball;
    my $size=-s $submitbase.".tar.gz";
    $size=$size/1024;
    print "tar file is $size K \n";
    #I am sort of guessing at this number
    if ($size >= 650){
	print "File is probably too big\n";
    }
}

sub conf{
    unless (-e "submit")
    {
	mkdir "submit" or die "Cannot make directory: $!";
    }
    my $texname=$basename.".tex";
    open DATA, "< $texname" or die("Cannot open $texname: $!");    
    @data=<DATA>;
    close DATA;
    chomp(@data);
    $submitname=$texname;
    @extras=($bbl,"svind.ist","svmult.cls","svmultphys.clo");

    &get_figures;
    &extra_files;
    &tarball;
}


###################These are to be used by the specific routines#########
sub get_figures{
    my $i=1;
    my $j=0;
    my @junk1;
    my @junk2;
    my @extension;
    my $infig=0;
    my $subfig=0;
    my $sublabel="";
    foreach(@data){
        if(($_=~"begin\{figure")&&($_!~"\%")){
	    $infig=1;
	}
        if(($_=~"end\{figure")&&($_!~"\%")){
	    $infig=0;
	    $subfig=0;
	    $sublabel="";
	    $i++;
	}
	if(($_=~$graphicscmd)&&($_!~"\%")){
	    unless($sublabel){
		if(($data[$j+1]=~$graphicscmd)&&($data[$j+1]!~"\%")){
		    $sublabel="a";
		}
	    }
	    @junk1=split('\{',$_);
	    @junk2=split('\}',$junk1[1]);
	    @extension=split('\.',$junk2[0]);
	    print "$extension[0]\n";
	    if(($special)&&(-e "$extension[0]-".$special.".$extension[1]")){
		copy("$extension[0]-".$special.".$extension[1]","submit/$figbase$i$sublabel.$extension[1]");
		if($special eq "BW"){
		    copy("$extension[0].$extension[1]","submit/$figbase$i$sublabel\_color.$extension[1]");
		}
	    }else{
		copy("$extension[0].$extension[1]","submit/$figbase$i$sublabel.$extension[1]");
	    }
	    $data[$j]=$junk1[0]."{$figbase$i$sublabel.$extension[1]}";
	    if($infig){
	      $subfig++;
	      if($subfig==1){
		$sublabel="b";
	      }
	      if($subfig==2){
		$sublabel="c";
	      }
	      if($subfig==3){
		$sublabel="d";
	      }
	    }
	}
	$j++;
    }
    open OUTDATA, "> submit/$submitname" or die("Cannot open submit/$submitname:$! ");
    select OUTDATA;
    foreach(@data){
	print "$_\n";
    }
    close OUTDATA;
    select STDOUT;
}

sub extra_files{
    foreach(@extras)
    {
	if(-e $_)
	{
	    if($_=~"bbl")
	    {
		copy($_,"submit/".$submitbase.".bbl");
	    }else{
		copy($_, "submit/$_");
	    }
	}else{
	    print "Warning: No $_ found.\n";
	}
    }
}

sub tarball{
    #make a tarball (with no internal directories) of whole thing
    chdir "submit" or die "Cannot change directory $!";
    if (-e $submitbase.".tar.gz")
    {
	print "$submitbase.tar.gz already exists. CANNOT make up-to-date tar file.\n";
    }else{
	system "tar -cvzf ".$submitbase.".tar.gz *";
    }
}
