Skip to content

Commit a8f672a

Browse files
committed
better parsing of mysql.txt
1 parent 01a012f commit a8f672a

File tree

1 file changed

+38
-29
lines changed

1 file changed

+38
-29
lines changed

trunk/mythtv/db.pm

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,37 +32,46 @@ package mythtv::db;
3232

3333
# Read the mysql.txt file in use by MythTV.
3434
# could be in a couple places, so try the usual suspects
35-
open(CONF, "$ENV{HOME}/.mythtv/mysql.txt")
36-
or open(CONF, "/usr/share/mythtv/mysql.txt")
37-
or open(CONF, "/usr/local/share/mythtv/mysql.txt")
38-
or open(CONF, "/etc/mythtv/mysql.txt")
39-
or die ("Unable to locate mysql.txt: $!\n\n");
40-
while (my $line = <CONF>) {
41-
# Cleanup
42-
next if ($line =~ /^\s*#/);
43-
$line =~ s/^str //;
44-
chomp($line);
45-
# Split off the var=val pairs
46-
my ($var, $val) = split(/\=/, $line, 2);
47-
next unless ($var && $var =~ /\w/);
48-
if ($var eq 'DBHostName') {
49-
$db_host = $val;
50-
}
51-
elsif ($var eq 'DBUserName') {
52-
$db_user = $val;
53-
}
54-
elsif ($var eq 'DBName') {
55-
$db_name = $val;
56-
}
57-
elsif ($var eq 'DBPassword') {
58-
$db_pass = $val;
59-
}
60-
# Hostname override
61-
elsif ($var eq 'LocalHostName') {
62-
$hostname = $val;
35+
my $found = 0;
36+
my @mysql = ("/usr/local/share/mythtv/mysql.txt",
37+
"/usr/share/mythtv/mysql.txt",
38+
"/etc/mythtv/mysql.txt",
39+
"/usr/local/etc/mythtv/mysql.txt",
40+
"$ENV{HOME}/.mythtv/mysql.txt",
41+
"mysql.txt"
42+
);
43+
foreach my $file (@mysql) {
44+
next unless (-e $file);
45+
$found = 1;
46+
open(CONF, $file) or die "Unable to open $file: $!\n\n";
47+
while (my $line = <CONF>) {
48+
# Cleanup
49+
next if ($line =~ /^\s*#/);
50+
$line =~ s/^str //;
51+
chomp($line);
52+
# Split off the var=val pairs
53+
my ($var, $val) = split(/\=/, $line, 2);
54+
next unless ($var && $var =~ /\w/);
55+
if ($var eq 'DBHostName') {
56+
$db_host = $val;
57+
}
58+
elsif ($var eq 'DBUserName') {
59+
$db_user = $val;
60+
}
61+
elsif ($var eq 'DBName') {
62+
$db_name = $val;
63+
}
64+
elsif ($var eq 'DBPassword') {
65+
$db_pass = $val;
66+
}
67+
# Hostname override
68+
elsif ($var eq 'LocalHostName') {
69+
$hostname = $val;
70+
}
6371
}
72+
close CONF;
6473
}
65-
close CONF;
74+
die "Unable to locate mysql.txt: $!\n\n" unless ($found && $db_host);
6675

6776
# Connect to the database
6877
$dbh = DBI->connect("dbi:mysql:database=$db_name:host=$db_host", $db_user, $db_pass)

0 commit comments

Comments
 (0)