#!/usr/bin/perl

######################################################################
#
# This script uses wpd2sxw to convert batches of WPD files to 
# OpenOffice.org format so that WordPerfect files may be opened in 
# oowriter. For more info type: wpd2sxwbatch.pl -h
#
# Copyright 2003 Michael Clark <miark@gardnerbusiness.com>
# Written with the support of Brent Hasty.
#
# This program is free software, redistributable and/or modifiable
# under the terms of the GNU General Public License as published 
# by the Free Software Foundation. You can read the GPL at
# http://www.gnu.org/copyleft/gpl.html
#
# 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.
#
######################################################################

use Getopt::Std;

######################################################################
#
## Variables

getopts('thvurd:o:');
$ext_in  = "wpd";
$ext_in  = "wpd" if $opt_t == 1;


######################################################################
#
## Display help page 

if ($opt_h == 1) {
  print << "  EOF";

  Usage: wpd2sxwbatch.pl [-hruv] [-d dir] [-o dir]

  wpd2sxwbatch.pl uses wpd2sxw to convert batches of WordPerfect
  word processor files (WPD) to the OpenOffice.org word processor
  format (SXW) so that they may be opened in oowriter. The OOo
  files are generated anew and do not replace the original wpd files.

  Running wpd2sxwbatch.pl more than once on the same directory will 
  cause the existing SXW file to be replaced with a new one.

  -d <dir>
      begins the conversion process in directory \"dir\"
      instead of the current directory. Use this switch
      separately from the other switches, (-vr -d /home/robin)
      or at the end of a switch string (-rvd /home/robin).

  -h  displays this help text.

  -o <dir>
      causes all the generated SXW files to be deposited in
      this single specified output directory. When not used, the 
      generated SXW files are deposited in the same directory
      in which the original WPD file was found. Use this switch
      separately from the other switches, (-vr -o /home/robin)

  -r  converts files in either the current directory or the
      directory specified by the -d switch (see below) and
      all the wpd files in all the subdirectories.

  -u  replaces spaces with underscores in the target filename(s).

  -v  Displays statistics and the progress of the conversion.

  EOF

  exit;
}


######################################################################
#
## Let user know if they specified a bad directory

if ($opt_d && !-e $opt_d) {
  print STDERR "\nWarning! Input directory $opt_d does not exist!\n\n";
  exit;
}

if ($opt_d && !-d $opt_d) {
  print STDERR "\nWarning! Input directory $opt_d is not a directory!\n\n";
  exit;
}

if ($opt_d && !-w $opt_d) {
  print STDERR "\nWarning! Input directory $opt_d is not a writable!\n\n";
  exit;
}

if ($opt_o && !-e $opt_o) {
  print STDERR "\nWarning! Output directory $opt_o does not exist!\n\n";
  exit;
}

if ($opt_o && !-d $opt_o) {
  print STDERR "\nWarning! Output directory $opt_o is not a directory!\n\n";
  exit;
}

if ($opt_o && !-w $opt_o) {
  print STDERR "\nWarning! Output directory $opt_o is not a writable!\n\n";
  exit;
}


######################################################################
#
## The quick and dirty version when no switches are used

if ($opt_v != 1 && $opt_d eq "" && opt_r == 1 && $opt_o eq "") {
  my @files = `find ./ -type f -iname \"*.$ext_in\"`;

  foreach (@files) {
    chomp $_;
    my $in  = $_;
    my $out = $_;
    $out =~ s/\.$ext_in$/.sxw/i;
    $out =~ s/\s+/_/g;
    system("wpd2sxw '$in' '$out'") if $opt_t != 1; 
    print "\nwpd2sxw '$in' '$out'" if $opt_t == 1;
  }

  exit;
}


######################################################################
#
## Determine the working directory(s)

if ($opt_v == 1) {
  system('clear');
  print "== Converting WPD Files ==================================\n\n";
}

if ($opt_d) {
  @dirs = ("$opt_d");
  print "* Working in directory \"$opt_d\".\n" if $opt_v == 1;
}

else {
  @dirs = ("./");
  print "* Working in current directory ($dirs[0]).\n" if $opt_v == 1;
}

if ($opt_r == 1) {
  @dirs = `find $dirs[0] -type d`;

  foreach (@dirs) { 
    chomp $_; 
    $dir_count++;
  }

  $dir_count = $dir_count - 1;
  print "* Working in $dir_count subdirectories.\n" if $dir_count > 0 && $opt_v == 1;
  @dirs = ("./") if $dir_count < 1;
}


######################################################################
#
## Count existing WPD files

if ($opt_v == 1) {
  @wpd_files = `find $dirs[0] -type f -iname \"*.$ext_in\"`;
  $wpd_count = @wpd_files;
  print "* Working on $wpd_count WPD files.\n\n";
}


######################################################################
#
## Perform the conversion

foreach $dir (@dirs) {
  print "- Working in $dir... " if $opt_v == 1;
  my($found, $found_ct);

  opendir DIR, "$dir";
  @files = readdir DIR;
  close DIR;

  foreach $file (@files) {
    $path = $dir . "/" . $file;
    $path =~ s/\/\//\//;

    if ($file =~ m/\.$ext_in$/i) {
      $found = 1;
      $found_ct++;

      $in  = $dir . "/" . $file;
      $in =~ s/\/+/\//g;

      $file =~ s/\s/_/g if $opt_u == 1;
      $out = $dir . "/" . $file;
      $out = $opt_o . "/" . $file if $opt_o;
      $out =~ s/\/+/\//g;
      $out =~ s/\.$ext_in$/.sxw/i;

      system("wpd2sxw '$in' '$out'") if $opt_t != 1;
      print "\nwpd2sxw '$in' '$out'" if $opt_t == 1;
    }
  }
  print "no WPD files.\n" 		 if $found != 1 && $opt_v == 1;
  print "converted $found_ct file(s).\n" if $found == 1 && $opt_v == 1;
  $total = $total + $found_ct;
}

print "\n* Successfully converted $total files.\n\n" if $opt_v == 1;
