#!/usr/bin/perl
use strict;

#=========================================================
# config, you'll have to edit this
#=========================================================

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

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

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

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

my %satconfig = (
"1", 2,
"3", 5,
"4", 6
);
#=========================================================
# end of config
#=========================================================

`$stopcmd`;

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

print "Copying multiplexes\n";

opendir(DIR, "$configpath/dvbmuxes/$adapter_src/") or die "can't opendir $configpath/dvbmuxes/$adapter_dest/: $!";
while (defined(my $file = readdir(DIR)))
{
  next if $file eq '.';
  next if $file eq '..';
  print "$configpath/dvbmuxes/$adapter_src/$file\n";
  open SOURCE, "<", "$configpath/dvbmuxes/$adapter_src/$file" or die $!;
  my $destfile = $file;
  while ((my $key, my $value) = each(%satconfig))
  {
    $destfile =~ s/$adapter_src(\d+_\w_satconf_)$key/$adapter_dest$1$value/;
  }
  # if no switchport is defined
  $destfile =~ s/$adapter_src(.*)/$adapter_dest$1/;
  open DEST, ">", "$configpath/dvbmuxes/$adapter_dest/$destfile" or die $!;
  while (<SOURCE>) 
  {
    #$line = $_;
    while ((my $key, my $value) = each(%satconfig))
    {
      s/(\s*satconf": ")$key"/$1$value"/;
    }
    print DEST $_; 
  }
  close(DEST);
  `chown hts:video $configpath/dvbmuxes/$adapter_dest/$destfile`;
}
closedir(DIR);

print "\nCopying transports\n";

opendir(DIR, "$configpath/dvbtransports/") or die "can't opendir $configpath/dvbtransports/: $!";
while (defined(my $file = readdir(DIR)))
{
  next if $file eq '.';
  next if $file eq '..';
  print "$configpath/dvbtransports/$file\n";
  my $destfile = $file;
  while ((my $key, my $value) = each(%satconfig))
  {
    $destfile =~ s/$adapter_src(\d+_\w_satconf_)$key/$adapter_dest$1$value/;
  }
  # if no switchport is defined
  $destfile =~ s/$adapter_src(.*)/$adapter_dest$1/;
  `cp -r $configpath/dvbtransports/$file $configpath/dvbtransports/$destfile`;
  `chown hts:video $configpath/dvbtransports/$destfile`;
  # rename files in newly created dir
  opendir(DIR2, "$configpath/dvbtransports/$destfile/") or die "can't opendir $configpath/dvbtransports/$destfile/: $!";
  while (defined(my $file2 = readdir(DIR2)))
  {
    next if $file2 eq '.';
    next if $file2 eq '..';
    print "    $configpath/dvbtransports/$destfile/$file2\n";
    my $destfile2 = $file2;
    while ((my $key, my $value) = each(%satconfig))
    {
      $destfile2 =~ s/$adapter_src(\d+_\w_satconf_)$key(_.*)/$adapter_dest$1$value$2/;
    }
    #if no switchport is defined
    $destfile2 =~ s/$adapter_src(.*)/$adapter_dest$1/;
    `mv $configpath/dvbtransports/$destfile/$file2 $configpath/dvbtransports/$destfile/$destfile2`;
    `chown hts:video $configpath/dvbtransports/$destfile/$destfile2`;
  }
  closedir(DIR2);

}
closedir(DIR);
`$startcmd`;
