toward renaming mig to emig and tweaking the directory layout
[migration-tools.git] / emig.d / bin / mig-skip-iconv
1 #!/usr/bin/perl -w
2 ###############################################################################
3 =pod
4
5 =head1 NAME
6
7 mig-skip-iconv 
8
9 Allows you to either use an existing file named <file>.utf8 or a named
10 [utf8 file] as if it were the one created by mig-iconv
11
12 =head1 SYNOPSIS
13
14 B<mig-skip-iconv> <file> [utf8 file]
15
16 =cut
17
18 ###############################################################################
19
20 use strict;
21 use Switch;
22 use Env qw(
23     HOME PGHOST PGPORT PGUSER PGDATABASE MIGSCHEMA
24     MIGBASEWORKDIR MIGBASEGITDIR MIGGITDIR MIGWORKDIR
25 );
26 use Pod::Usage;
27 use DBI;
28 use Cwd 'abs_path';
29 use FindBin;
30 my $mig_bin = "$FindBin::Bin/";
31 use lib "$FindBin::Bin/";
32 use Mig;
33
34 pod2usage(-verbose => 2) if ! ($ARGV[0]||$ARGV[1]) || $ARGV[0] eq '--help';
35
36 Mig::die_if_no_env_migschema();
37 Mig::die_if_mig_tracking_table_does_not_exist();
38
39 my $file = abs_path($ARGV[0]);
40 my $utf8_file;
41 if ($ARGV[1]) {
42     $utf8_file = abs_path($ARGV[1]);
43 } else {
44     $utf8_file = $file;
45 }
46 if ($utf8_file && ! $utf8_file =~ /^$MIGBASEWORKDIR/) {
47     die "File falls outside of MIGWORKDIR ($MIGWORKDIR): $utf8_file\n";
48 }
49
50 if ($file =~ /^$MIGBASEWORKDIR/) {
51     skip_iconv($file,$utf8_file);
52 } else {
53     print "File falls outside of MIGWORKDIR ($MIGWORKDIR): $file\n";
54 }
55
56 exit 0;
57
58 ###############################################################################
59
60 sub skip_iconv {
61     my $file = shift;
62     my $utf8_file = shift;
63
64     my $tracked_file_id = Mig::check_for_tracked_file($file);
65     if ($tracked_file_id) {
66         my $data = Mig::status_this_file($file);
67         print "skipping the iconv'ing of tracked file: $file\n";
68
69         my $dbh = Mig::db_connect();
70         if (! $utf8_file) {
71             $utf8_file = $file . '.utf8';
72         }
73         if (! -e $utf8_file) {
74             die "utf8 file does not exist: $utf8_file\n";
75         }
76
77         my $rv = $dbh->do("
78             UPDATE $MIGSCHEMA.tracked_file
79             SET utf8_filename = " . $dbh->quote($utf8_file) . "
80             WHERE base_filename = " . $dbh->quote($file) . "
81             ;
82         ") || die "Error inserting into table $MIGSCHEMA.tracked_file: $!\n";
83         Mig::db_disconnect($dbh);
84     } else {
85         print "File not currently tracked: $file\n";
86     }
87 }