Merge branch 'test'
[eq/.git] / bin / parse_ward_data
1 #!/usr/bin/perl
2
3 use DBI;
4 use Getopt::Std;
5
6 $mydir = `cd \$(dirname $0) 2>/dev/null; pwd`; chomp($mydir);
7 unshift @INC,("$mydir/../setup");
8 if( -f "$mydir/../setup/db_config.local") { require "db_config.local"; }
9 else { require "db_config"; }
10
11 %hometeaching_data = ();
12 %membership_data = ();
13 getopts('vsn:o:b');
14
15 $monthname2num{'Jan'} = '01';
16 $monthname2num{'Feb'} = '02';
17 $monthname2num{'Mar'} = '03';
18 $monthname2num{'Apr'} = '04';
19 $monthname2num{'May'} = '05';
20 $monthname2num{'Jun'} = '06';
21 $monthname2num{'Jul'} = '07';
22 $monthname2num{'Aug'} = '08';
23 $monthname2num{'Sep'} = '09';
24 $monthname2num{'Oct'} = '10';
25 $monthname2num{'Nov'} = '11';
26 $monthname2num{'Dec'} = '12';
27
28 ######################################################################
29 # SUBROUTINES
30 ######################################################################
31 sub csv_to_hash
32 {
33     my ($filename, $hashref) = @_;
34
35     open(FILE,$filename) || die "-E- Could not open $filename for reading\n";
36
37     my $found_header = 0; my $index = 0;
38     while(<FILE>)
39     {
40         $line = $_;
41         @data = split /\",/, $line;
42         if(!$found_header) { @header = @data; $found_header = 1; }
43         else {
44             foreach $i (0..$#data-1) {
45                 $data[$i] =~ s/\"//g;
46                 $header[$i] =~ s/\"//g;
47                 $hashref->{$index}{$header[$i]} = $data[$i];
48                 #print "$index: $i: $header[$i]: $data[$i]\n";
49             }
50             $index++;
51         }
52     }
53     
54     close(FILE);
55 }
56
57 ######################################################################
58 sub print_hash
59 {
60     my ($hashref) = @_;
61
62     foreach $key (sort {$a <=> $b} keys %$hashref) {
63         print "Index: $key\n";
64         foreach $field (keys %{$hashref->{$key}}) {
65             print "$field: $hashref->{$key}{$field}\n";
66         }
67         print "\n";
68     }
69 }
70
71 ######################################################################
72 sub print_birthdays
73 {
74     my ($hashref) = @_;
75
76     foreach $key (sort {$a <=> $b} keys %$hashref) {
77         $name = "";
78         $birthday = "";
79         foreach $field (keys %{$hashref->{$key}}) {
80             if($field =~ /Full Name/) { $name = $hashref->{$key}{$field}; }
81             if($field =~ /Birth/) { $birthday = $hashref->{$key}{$field}; }
82         }
83         if($name ne "" && $birthday ne "") { printf "%-30s %-10s\n",$name,$birthday; }
84     }
85 }
86
87 ######################################################################
88 # MAIN
89 ######################################################################
90
91 ###################################################
92 # Open a connection to the database
93 $dbh=DBI->connect("dbi:mysql:dbname=$dbname:host=$dbhost:port=$dbport",$dbuser,$dbpass,{
94     AutoCommit=>0,
95     PrintError=>0}) or print "Connect Failure:".$DBI::errstr."\n" and exit 2;
96
97 ###################################################
98 # Process command line options
99 if(defined $opt_n) { $datadir = $opt_n; }
100 else { $datadir = shift(@ARGV); }
101 print "-> Processing all ward data files in $datadir\n";
102
103 ###################################################
104 # Parse Ward Data Files
105 &csv_to_hash("$datadir/Membership.csv",\%membership_data);
106 &csv_to_hash("$datadir/HomeTeaching.csv",\%hometeaching_data);
107
108 if($opt_v) {
109     print "-> Membership Data Dump\n\n";
110     &print_hash(\%membership_data);
111     print "-> HomeTeaching Data Dump\n\n";
112     &print_hash(\%hometeaching_data);
113 }
114
115 if($opt_b) { &print_birthdays(\%membership_data); }
116
117 if($opt_s) { $dbh->disconnect(); exit; }
118
119 ###################################################
120 # Disconnect from the database
121 $dbh->disconnect();
122
123 ######################################################################
124
125
126
127
128
129
130
131
132
133
134
135