"mig" tool
[migration-tools.git] / mig-bin / mig-skip-clean
diff --git a/mig-bin/mig-skip-clean b/mig-bin/mig-skip-clean
new file mode 100755 (executable)
index 0000000..013c075
--- /dev/null
@@ -0,0 +1,98 @@
+#!/usr/bin/perl -w
+###############################################################################
+=pod
+
+=head1 NAME
+
+mig-skip-clean 
+
+Allows you to either use an existing file named <file>.utf8.clean or a
+named [cleaned file] as if it were the one created by mig-clean
+
+Note that the clean file, however specified, should contain headers. The
+remaining tools in the chain will expect this.
+
+=head1 SYNOPSIS
+
+B<mig-skip-clean> <file> [cleaned file]
+
+=cut
+
+###############################################################################
+
+use strict;
+use Switch;
+use Env qw(
+    HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
+    MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
+);
+use Pod::Usage;
+use DBI;
+use Cwd 'abs_path';
+use FindBin;
+my $mig_bin = "$FindBin::Bin/";
+use lib "$FindBin::Bin/";
+use Mig;
+
+pod2usage(-verbose => 2) if ! ($ARGV[0]||$ARGV[1]) || $ARGV[0] eq '--help';
+
+Mig::die_if_no_env_migschema();
+Mig::die_if_mig_tracking_table_does_not_exist();
+
+my $file = abs_path($ARGV[0]);
+my $clean_file;
+if ($ARGV[1]) {
+    $clean_file = abs_path($ARGV[1]);
+}
+if ($clean_file && ! $clean_file =~ /^$MIGBASEWORKDIR/) {
+    die "File falls outside of MIGWORKDIR ($MIGWORKDIR): $clean_file\n";
+}
+
+if ($file =~ /^$MIGBASEWORKDIR/) {
+    skip_clean($file,$clean_file);
+} else {
+    die "File falls outside of MIGWORKDIR ($MIGWORKDIR): $file\n";
+}
+
+exit 0;
+
+###############################################################################
+
+sub skip_clean {
+    my $file = shift;
+    my $clean_file = shift;
+
+    my $tracked_file_id = Mig::check_for_tracked_file($file);
+    if ($tracked_file_id) {
+        my $data = Mig::status_this_file($file);
+
+        if (! $data->{'utf8_filename'}) {
+            die "mig-iconv or mig-skip-iconv needed for UTF8 version of file: $file\n";
+        }
+
+        my $utf8_file = $data->{'utf8_filename'};
+        if (! -e $utf8_file) {
+            die "missing file: $utf8_file\n";
+        }
+
+        print "skipping cleaning of tracked file: $file\n";
+
+        my $dbh = Mig::db_connect();
+        if (! $clean_file) {
+            $clean_file = $utf8_file . '.clean';
+        }
+        if (! -e $clean_file) {
+            die "clean file does not exist: $clean_file\n";
+        }
+
+        my $rv = $dbh->do("
+            UPDATE $MIGSCHEMA.tracked_file
+            SET clean_filename = " . $dbh->quote($clean_file) . "
+            WHERE base_filename = " . $dbh->quote($file) . "
+            ;
+        ") || die "Error inserting into table $MIGSCHEMA.tracked_file: $!\n";
+        Mig::db_disconnect($dbh);
+    } else {
+        die "File not currently tracked: $file\n";
+    }
+}