Project

General

Profile

Feature #828 ยป copy_muxes.pl

Mark Hunting, 2012-01-12 11:31

 
1
#!/usr/bin/perl
2
use strict;
3

    
4
#=========================================================
5
# config, you'll have to edit this
6
#=========================================================
7

    
8
my $configpath = '/home/hts/.hts/tvheadend';
9

    
10
# Enter adapter names here, the config from adapter_src will be copied to adapter_dest
11
# See $configpath/dvbadapters/ for adapter names.
12
my $adapter_src = '_dev_dvb_adapter0_STV090x_Multistandard';
13
my $adapter_dest = '_dev_dvb_adapter1_STV090x_Multistandard';
14

    
15
my $startcmd = '/etc/init.d/tvheadend start';
16
my $stopcmd = '/etc/init.d/tvheadend stop';
17

    
18
# Define the array of port id's here. I have three ports on both adapters,
19
# on adapter_src my id's are 1, 3 and 4, on adapter_dest it's 2, 5 and 6
20
# See $configpath/dvbsatconf/ for id's
21
# The config of port id 1 will be copied to port id 2
22
# The config of port id 3 will be copied to port id 5
23
# The config of port id 4 will be copied to port id 6
24

    
25
my %satconfig = (
26
"1", 2,
27
"3", 5,
28
"4", 6
29
);
30
#=========================================================
31
# end of config
32
#=========================================================
33

    
34
`$stopcmd`;
35

    
36
`rm -r $configpath/dvbmuxes/$adapter_dest/*`;
37
`rm -r $configpath/dvbtransports/$adapter_dest*`;
38

    
39
print "Copying multiplexes\n";
40

    
41
opendir(DIR, "$configpath/dvbmuxes/$adapter_src/") or die "can't opendir $configpath/dvbmuxes/$adapter_dest/: $!";
42
while (defined(my $file = readdir(DIR)))
43
{
44
  next if $file eq '.';
45
  next if $file eq '..';
46
  print "$configpath/dvbmuxes/$adapter_src/$file\n";
47
  open SOURCE, "<", "$configpath/dvbmuxes/$adapter_src/$file" or die $!;
48
  my $destfile = $file;
49
  while ((my $key, my $value) = each(%satconfig))
50
  {
51
    $destfile =~ s/$adapter_src(\d+_\w_satconf_)$key/$adapter_dest$1$value/;
52
  }
53
  # if no switchport is defined
54
  $destfile =~ s/$adapter_src(.*)/$adapter_dest$1/;
55
  open DEST, ">", "$configpath/dvbmuxes/$adapter_dest/$destfile" or die $!;
56
  while (<SOURCE>) 
57
  {
58
    #$line = $_;
59
    while ((my $key, my $value) = each(%satconfig))
60
    {
61
      s/(\s*satconf": ")$key"/$1$value"/;
62
    }
63
    print DEST $_; 
64
  }
65
  close(DEST);
66
  `chown hts:video $configpath/dvbmuxes/$adapter_dest/$destfile`;
67
}
68
closedir(DIR);
69

    
70
print "\nCopying transports\n";
71

    
72
opendir(DIR, "$configpath/dvbtransports/") or die "can't opendir $configpath/dvbtransports/: $!";
73
while (defined(my $file = readdir(DIR)))
74
{
75
  next if $file eq '.';
76
  next if $file eq '..';
77
  print "$configpath/dvbtransports/$file\n";
78
  my $destfile = $file;
79
  while ((my $key, my $value) = each(%satconfig))
80
  {
81
    $destfile =~ s/$adapter_src(\d+_\w_satconf_)$key/$adapter_dest$1$value/;
82
  }
83
  # if no switchport is defined
84
  $destfile =~ s/$adapter_src(.*)/$adapter_dest$1/;
85
  `cp -r $configpath/dvbtransports/$file $configpath/dvbtransports/$destfile`;
86
  `chown hts:video $configpath/dvbtransports/$destfile`;
87
  # rename files in newly created dir
88
  opendir(DIR2, "$configpath/dvbtransports/$destfile/") or die "can't opendir $configpath/dvbtransports/$destfile/: $!";
89
  while (defined(my $file2 = readdir(DIR2)))
90
  {
91
    next if $file2 eq '.';
92
    next if $file2 eq '..';
93
    print "    $configpath/dvbtransports/$destfile/$file2\n";
94
    my $destfile2 = $file2;
95
    while ((my $key, my $value) = each(%satconfig))
96
    {
97
      $destfile2 =~ s/$adapter_src(\d+_\w_satconf_)$key(_.*)/$adapter_dest$1$value$2/;
98
    }
99
    #if no switchport is defined
100
    $destfile2 =~ s/$adapter_src(.*)/$adapter_dest$1/;
101
    `mv $configpath/dvbtransports/$destfile/$file2 $configpath/dvbtransports/$destfile/$destfile2`;
102
    `chown hts:video $configpath/dvbtransports/$destfile/$destfile2`;
103
  }
104
  closedir(DIR2);
105

    
106
}
107
closedir(DIR);
108
`$startcmd`;
    (1-1/1)