zwave_configconvert.pl 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #!/usr/bin/perl
  2. # Details in Forum #35416
  3. if(@ARGV == 0 || ($ARGV[0] =~ m/^-/ && $ARGV[0] ne "-d")) {
  4. print "Usage:\n".
  5. " cd open-zwave\n".
  6. " perl .../contrib/zwave_configconvert.pl config |\n".
  7. " gzip > .../FHEM/lib/openzwave_deviceconfig.xml.gz\n".
  8. "or\n".
  9. " cd open-zwave\n".
  10. " gzip -d < .../FHEM/lib/openzwave_deviceconfig.xml.gz |\n".
  11. " perl .../contrib/zwave_configconvert.pl -d\n";
  12. exit 1;
  13. }
  14. if($ARGV[0] eq "-d") {
  15. while(my $l = <STDIN>) {
  16. next if($l !~ m/<Product sourceFile="([^"]*)"/);
  17. my $f = $1;
  18. next if(-f "config/$f");
  19. print("Creating: config/$f\n");
  20. open(FH, ">config/$f") || die("open config/$f: $!\n");
  21. print FH $l;
  22. while($l = <STDIN>) {
  23. print FH $l;
  24. last if($l =~ m,</Product>,);
  25. }
  26. close(FH);
  27. }
  28. exit(0);
  29. }
  30. print '<?xml version="1.0" encoding="utf-8"?>', "\n";
  31. print "<ProductList>\n";
  32. foreach my $file (`find $ARGV[0] -name \*.xml`) {
  33. chomp($file);
  34. my $name = $file;
  35. $name =~ s+.*config/++;
  36. next if($name !~ m+/+); # Only files from subdirs
  37. open(FH, $file) || die("$file:$!\n");
  38. my $buffer="";
  39. while(my $l = <FH>) {
  40. next if($l =~ m/^<\?xml/);
  41. chomp($l);
  42. $l =~ s/^<Product.*xmlns.*/<Product sourceFile="$name">/;
  43. $l =~ s/\r//g;
  44. $l =~ s/\t/ /g;
  45. #$l =~ s/^ *//g;
  46. $l =~ s/ *$//g;
  47. next if($l eq "");
  48. if($l !~ m/>$/ || $l =~ m/^\s*<Help>\s*$/) { $buffer .= " ".$l; next; }
  49. if($buffer && $l =~ m/>$/) { $l = "$buffer $l"; $buffer=""; }
  50. $l =~ s/<!--.*-->//g;
  51. $l =~ s/ *$//g;
  52. print $l,"\n" if($l);
  53. }
  54. close(FH);
  55. print $buffer if($buffer);
  56. print "\n"; # One empty line between products
  57. }
  58. print "</ProductList>\n";