#!/usr/bin/perl 
#
#  Usage: jtour [-m -p] command
#
#  Loops over a document's directories and invokes local make commands.
#
#  -v     sets verbose mode that echos the commands 
# 
#  -d dir sets the root directory for the looping 
#
#  -m     loops over all directories returned by "makeDocList -m" 
#         loops over all directories with a makefile, essentially
#
#  -p     loops over all directories returned by "makeDocList -p"
#         loops over all directories with a paper.tex file, essentially
#
#  For example: 
# 
#      jtour -d dir -p gmake -n texclean 
# 
#   visits all paper directories that are subdirectories of dir 
#   and invokes everywhere "gmake texclean" 
#
#   Now here are some expected short cuts (hopefully self-explanatory): 
#
#   jtour hardcopy     every lowerletter dir with with make, tex, or java file 
#                      prints the make, tex, and java source files 
#   
#   jtour compile      every lowerletter dir with java file
#                      compiles java files
#
#   jtour test         every lowerletter dir with java file 
#                      invokes the test java routine
#
#   jtour burn, build, clean, view, texclean 
#                      every lowerletter dir with makefile
#
#   jtour resstat      every lowerletter dir with paper.tex, makefile 
#                      checks document figures
#
#   jtour rebuild      every lowerletter dir with makefile
#   jtour rebuildpaper every lowerletter dir with paper.tex
#                      burn, build cycle
#
#   jtour paper.ps     every lowerletter dir with paper.tex 
#                      build paper.ps
#
#      these are invoked in every directory with a makefile:
#            jtour makedoc 
#            jtour removedoc
#
#      these are not yet implemented:
#            jtour distclean
#            jtour ftp 
#            jtour cdrom 
#            jtour web 
#
# Sergey suggests to always have "jtour command" without any additional 
# shortcuts. Consistent! 
#
# I want to rewrite this: first part invokes makeDirList that makes a 
# a list of all directories fitting various criteria (AND OR logic?). 
# (all dirs with tex file, all dirs with makefiles, all dirs with java files)
# all entries in the list are unique (sorting!) 
# 
# Than jtour invokes a command in each directory in the list. 
# Instead of invoking makeDirList jtour may accept a list of dirs on 
# commandline. 
#
#
# for improvements, please edit the original in /homes/sep/matt/usr/srcAdm
# author: Matthias Schwab, 3 June 1997
#%

require "getopts.pl" ;

if ( $#ARGV < 0 ) {
  &usage ;
  }

&Getopts( 'vmpd:' ) ;

$verb = "no";
if ( $opt_v ) {
    $verb = "yes" ;
}

$dir = '.'        ; 
if ( $opt_d ) {
    $rootdir = $opt_d ;
}

if ( $opt_m ) {
    $make = "yes" ;
    $papr = "no"  ;
}

if ( $opt_p ) {
    $make = "no"  ;
    $papr = "yes" ;
}

# process the remaining nonflag arguments into a command
@localCmd = &localCmdProc(@ARGV);
#print "@localCmd\n"; 

# compute the needed directory list
# this could be more sophisticated and look for all dirs with 
# make and java files, or make and paper files. 
# $make=yes: loop over all dirs with makefiles
# $papr=yes: loop over all dirs with paper.tex
if    ( $make eq "yes") {
    @dirlist = `makeDocList -m -d $rootdir`;
}
elsif ( $papr eq "yes") {
    #print "we are in paper\n";
    @dirlist = `makeDocList -p -d $rootdir`;
}
else                    {
    print "jtour: Error no dirlist found ...";
}

# execute the constructed command in each directory
foreach $dir (@dirlist) {
    chop($dir);
    if ( $verb eq "yes" ) {
	@cmd = "cd $dir ; pwd; echo @localCmd; @localCmd;"; 
    }
    else {
        @cmd = "cd $dir ;                      @localCmd;"; 
    }
    @list = system(@cmd); 
}

sub usage {
  open( THIS, $0 ) ;

  while ( <THIS> ) {
       if ( $_ =~ /^\#/ ) {
         next if $_ =~ /^\#\!/ ;
         exit if $_ =~ /^\#\%/ ;

         print ;
         }
       }

  exit( 0 ) ;
  }

sub localCmdProc {
    local(@localCmd) = @_; 
    if (scalar(@localCmd) == 1) {
    if (!(@localCmd[0] eq gmake || @localCmd[0] eq make)) { 

    # the next cases loop over paper directories
	if (@localCmd[0] eq     hardcopy) { 
	    @localCmd = ("gmake hardcopy"); 
	    $papr     = "no"; 
	    $make     = "yes" ;
	}
	if (@localCmd[0] eq     burn) { 
	    @localCmd = ("gmake burn"); 
	    $papr     = "yes"; 
	    $make     = "no" ;
	}
	if (@localCmd[0] eq     build) { 
	    @localCmd = ("gmake build"); 
	    $papr     = "yes"; 
	    $make     = "no" ;
	}
	if (@localCmd[0] eq     clean) { 
	    @localCmd = ("gmake clean"); 
	    $papr     = "no" ; 
	    $make     = "yes";
	}
	if (@localCmd[0] eq     view) { 
	    @localCmd = ("gmake view"); 
	    $papr     = "yes"; 
	    $make     = "no" ;
	}
	if (@localCmd[0] eq     resstat) { 
	    @localCmd = ("Resstat -"); 
	    $papr     = "yes"; 
	    $make     = "no" ;
	}
	if (@localCmd[0] eq     rebuildpaper) { 
	    @localCmd = ("gmake rebuildpaper"); 
	    $papr     = "yes"; 
	    $make     = "no" ;
	}
	if (@localCmd[0] eq     paper.ps) { 
	    @localCmd = ("gmake paper.ps"); 
	    $papr     = "yes"; 
	    $make     = "no" ;
	}
	if (@localCmd[0] eq     texclean) { 
	    @localCmd = ("gmake texclean"); 
	    $papr     = "no" ; 
	    $make     = "yes";
	}
    # the next cases loop over makefile directories
	if ( @localCmd[0] eq    test) { 
	    @localCmd = ("gmake testjava"); 
	    $papr     = "no" ; 
	    $make     = "yes";
	}
	if ( @localCmd[0] eq    demo) { 
	    @localCmd = ("gmake demojava"); 
	    $papr     = "no" ; 
	    $make     = "yes";
	}
	if (@localCmd[0] eq     compile) { 
	    @localCmd = ("gmake compilejava"); 
	    $papr     = "no" ; 
	    $make     = "yes";
	}
	if (@localCmd[0] eq     rebuild) { 
	    @localCmd = ("gmake rebuild"); 
	    $papr     = "no" ; 
	    $make     = "yes";
	}
	if (@localCmd[0] eq     removedoc) { 
	    @localCmd = ("gmake removejavadoc"); 
	    $papr     = "no" ; 
	    $make     = "yes";
	}
	if (@localCmd[0] eq     makedoc) { 
	    @localCmd = ("gmake makejavadoc"); 
	    $papr     = "no" ; 
	    $make     = "yes";
	}
	#print "here: @localCmd\n";
    }}
    return @localCmd; 
}










