#!/usr/bin/perl
use strict;
use warnings;

# If the file has a matching folder in the format [file]_files, then move
# the file and the folder to the target dir.

#The Bushy Knoll.html
my $file=shift;
die "no such file: $file" unless -f "$file";

my $dir=shift;
die "target dir required" unless defined $dir;
die "no such dir: $dir" unless -d "$dir";

#The Bushy Knoll_files
my $filesDir=$file;
$filesDir=~s/.html/_files/;
warn "no such dir: $filesDir" unless -d "$filesDir";

if(-f $filesDir.'/'.$file ) {
  warn "$filesDir/$file exists but who cares";
}

if(-d $filesDir.'/'.$filesDir) {
  warn "$filesDir/$filesDir exists but who cares";
}

my $result=rename $file, $dir.'/'.$file;
if ($result==1) {
  printf "Moved  %s to %s.\n", $file, $dir, $result;
}
else {
  printf STDERR "Move from %s to %s failed: %s\n", $file, $dir.'/'.$file, $result;
}

$result=rename $filesDir, $dir.'/'.$filesDir;
if ($result==1) {
  printf "Moved %s to %s.\n", $filesDir, $dir, $result;
}
else {
  printf STDERR "Move from %s to %s failed: %s\n", $filesDir, $dir, $result;
}
