#!/usr/bin/env perl

use warnings;
use strict;
use 5.010;
use POSIX qw/strftime/;

use Pod::Usage;
use File::Copy;
use File::Spec;

use Pod::Usage;
use Getopt::Long;

my %opt = ();
GetOptions( \%opt, 'help', 'date_suffix|dsfx', 'date_prefix|dpfx' ) or pod2usage(2);

pod2usage( -exitval => 0, -verbose => 2 ) if ( $opt{help} );
pod2usage(2)
  unless ( @ARGV && @ARGV > 0 );

our $VERSION = '0.066'; # VERSION

# FIXME sections needed: NAME, SYNOPSIS, DESCRIPTION

my $dir    = shift @ARGV;
my $prefix = shift @ARGV;

pod2usage( -exitstatus => 1, -verbose => 0, -message => "$dir is not a directory" ) unless ( -d $dir );

my $date = strftime( "%Y-%m-%d", localtime() );
unless ( $prefix eq '.' ) {
  $prefix .= "_" . $date if ( $opt{'date_suffix'} );
  $prefix = $date . "_" . $prefix if ( $opt{'date_prefix'} );
}

say STDERR "prefix is > $prefix <";

for my $sd ( '', 'bin', 'conf' ) {
  clone_dir( File::Spec->catdir( $dir, $sd ), File::Spec->catdir( $prefix, $sd ) );
}

sub HELP_MESSAGE { pod2usage( -exitstatus => 0, -verbose => 2 ) }

sub clone_dir {
  my $dir    = shift;
  my $prefix = shift;

  return unless ( -d $dir );
  mkdir $prefix unless ( -d $prefix );

  my @files = glob("$dir/*.yml");
  push @files, glob("$dir/*.pl");
  push @files, glob("$dir/*.md");
  push @files, glob("$dir/*.rst");
  push @files, glob("$dir/*.json");
  push @files, glob("$dir/*.py");
  push @files, glob("$dir/*.R");
  push @files, glob("$dir/*.sh");
  push @files, "$dir/Makefile";

  for my $f (@files) {
    next unless ( -f $f );
    my ( undef, $rd, $rf ) = File::Spec->splitpath($f);
    say STDERR "$f -> $prefix/$rf";
    copy( $f, $prefix ) or die "Copy failed: $!";
  }

}

__END__

=head1 NAME

  jclone - clone job directory

=head1 SYNOPSIS

  jclone [options] <directory_to_clone> [<prefix>]


  Options:

   --help      detailed help message
   --version   show script version
   <prefix>    (see detailed help)
   --dpfx      add date in front of the prefix
   --dsfx      add date to the end of the prefix

=head1 DESCRIPTION

If <prefix> is omitted, the current directory is the destination. Otherwise, a new directory with the current date and the prefix is created. Directory format: C<YYYY-MM-DD_$prefix>.

=head1 OPTIONS

=over 4

=item B<< --help >>

Show this text.

=back

=head1 SEE ALSO

=head1 AUTHOR

jw bargsten, C<< <jw at bargsten dot org> >>

=cut