b9cb013dc64e8846a5b647e578417e1d0e4250f4
[migration-tools.git] / kmig.d / bin / kmig-clean
1 #!/usr/bin/perl -w
2 ###############################################################################
3 =pod
4
5 =head1 NAME
6
7 mig-clean 
8
9 Attempts to invoke B<clean_csv> on the specified tracked file, placing the
10 output in [file].clean
11
12 If given no other arguments, the invocation will lool like
13
14 =over 5
15
16 clean_csv --config scripts/clean.conf --fix --apply [--create-headers|--use-headers <hfile>] <file>
17
18 =back
19
20 otherwise, the arguments will be passed through like so
21
22 =over 5
23
24 clean_csv [other arguments...] <file>
25
26 =back
27
28 You'll need to invoke B<mig-iconv> or B<mig-skip-iconv> prior to using commands
29 like B<mig-clean>
30
31 =head1 SYNOPSIS
32
33 B<mig-clean> <file> [other arguments...]
34
35 =cut
36
37 ###############################################################################
38
39 use strict;
40 use Switch;
41 use Env qw(
42     HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
43     MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
44 );
45 use Pod::Usage;
46 use DBI;
47 use Cwd 'abs_path';
48 use FindBin;
49 my $mig_bin = "$FindBin::Bin/";
50 use lib "$FindBin::Bin/";
51 use Mig;
52
53 pod2usage(-verbose => 2) if ! $ARGV[0] || $ARGV[0] eq '--help';
54
55 Mig::die_if_no_env_migschema();
56 Mig::die_if_mig_tracking_table_does_not_exist();
57
58 my $file = abs_path($ARGV[0]);
59 if ($file =~ /^$MIGBASEWORKDIR/) {
60     call_clean_csv(@ARGV);
61 } else {
62     print "File falls outside of MIGWORKDIR ($MIGWORKDIR): $file\n";
63 }
64
65 exit 0;
66
67 ###############################################################################
68
69 sub call_clean_csv {
70     my $file = abs_path(shift);
71     my @args = @_;
72
73     my $tracked_file_id = Mig::check_for_tracked_file($file);
74     if ($tracked_file_id) {
75         my $data = Mig::status_this_file($file);
76
77         if (! $data->{'utf8_filename'}) {
78             die "mig-iconv or mig-skip-iconv needed for UTF8 version of file: $file\n";
79         }
80
81         my $utf8_file = $data->{'utf8_filename'};
82         if (! -e $utf8_file) {
83             die "missing file: $utf8_file\n";
84         }
85
86         print "cleaning tracked file: $file\n";
87
88         if (scalar(@args) == 0) {
89             @args = (
90                  '--config'
91                 ,'scripts/clean.conf'
92                 ,'--fix'
93                 ,'--apply'
94                 ,'--backslash'
95                 ,'--pad'
96             );
97             if (! $data->{'has_headers'}) {
98                 if ($data->{'headers_file'}) {
99                     push @args, '--use-headers';
100                     push @args, $data->{'headers_file'};
101                 } else {
102                     push @args, '--create-headers';
103                 }
104             }
105         }
106
107         print join(' ',@args) . "\n";
108         system('clean_csv', @args, $utf8_file);
109
110         my $dbh = Mig::db_connect();
111         my $clean_file = $dbh->quote($utf8_file . '.clean');
112         if (! -e $utf8_file . '.clean') {
113             print "clean file does not exist: $clean_file\n";
114             $clean_file = $dbh->quote('');
115         }
116
117         my $rv = $dbh->do("
118             UPDATE $MIGSCHEMA.tracked_file
119             SET clean_filename = $clean_file
120             WHERE base_filename = " . $dbh->quote($file) . "
121             ;
122         ") || die "Error inserting into table $MIGSCHEMA.tracked_file: $!\n";
123         Mig::db_disconnect($dbh);
124     } else {
125         print "File not currently tracked: $file\n";
126     }
127 }