]> git.donarmstrong.com Git - lilypond.git/blob - bin/convert-mudela
release: 0.0.53
[lilypond.git] / bin / convert-mudela
1 #!/usr/bin/perl -w
2
3
4 use Getopt::Long;
5 sub 
6     version_string_conv
7 {
8     my ($from_version, $to_version) = @_;
9     s/\version \"0.0.$from_version\"/\version \"0.0.$to_version\"/g;
10 }
11
12 ################################################################
13
14 sub
15     conv_pl0_0_50_pl0_0_52
16 {
17  
18 }
19 sub conv_pl52_pl53
20 {
21
22     s/include \"/$1\\include \"/g;
23 }
24
25
26 ###############################################################
27
28
29
30
31
32 sub
33     usage
34 {
35     print STDERR "Usage: convert-mudela\n";
36     print STDERR "other options: --edit --output=FILE --show-rules\n";
37     exit 2;
38 }
39     
40 my %minor_conversions = (50 => \&no_conv,
41                          52 => \&conv_pl0_0_50_pl0_0_52,
42                          53 => \&conv_pl52_pl53
43                          );
44 sub versions 
45 {
46     return  (sort keys %minor_conversions);
47 }
48     
49
50 sub
51     show_rules
52 {
53     print "Rules: ", join(", ", sort keys %minor_conversions), "\n";
54     
55 }
56
57 sub 
58     do_conversion
59 {
60     my ($from,$to) = @_;
61
62     my @applicable_conversion;
63     my @mudela_levels;
64     
65     die "This is too old  to convert " if $from < 50;
66     my @v = versions;
67     foreach $a (@v) {
68         if ($a > $from && $a <= $to ){ 
69             push @applicable_conversion, $minor_conversions{$a};
70             push @mudela_levels, $a;
71         }
72     }
73     
74     print STDERR "Applying following rules: ", join(", ", @mudela_levels) , "\n";
75
76     while (<INLY>) {
77         foreach $subroutine (@applicable_conversion) {
78         
79             &$subroutine;
80             
81         }
82         version_string_conv $from, $to;
83         print OUTLY;
84     }
85 }
86 sub set_auto_from
87 {
88     my ($fn)=@_;
89     my ($ver);
90     open INLY, $fn || die "Can't open";
91
92     while (<INLY>) {
93         s/^.*\\version \"([^\"]*)\".*$//;
94         if (defined ($1)) {
95             print STDERR "Guessing version: ", $1, ".. ";
96             $ver = $1;
97             last;
98         }
99     }
100     die "can't determine mudela version." unless (defined($ver));
101     $ver =~ s/0\.0\.// ;
102     close INLY;
103     return $ver;
104 }   
105
106 sub  set_files 
107 {
108     $infile = "-";
109     $outfile = "-";
110     $outfile = $opt_output if (defined($opt_output));
111
112     if ($ARGV [0])  {
113         $infile = $ARGV[0];
114     } 
115     if (( ! -f $infile) && (! $infile =~ /\\.ly$/s ) ){
116         $infile .= ".ly";
117         print STDERR "trying $infile";
118
119     }
120     if ($opt_edit && $infile ne "-") {
121         $opt_edit = 1;
122         $outfile = "$infile.NEW";
123         $infile = "$infile";
124     }
125 }
126
127 GetOptions ("output=s", "from=i", "to=i", "minor=i", "edit", "show-rules");
128
129 if ($opt_show_rules) { 
130     show_rules ;
131     $opt_show_rules = 0;        # to extinguish typo check.
132     exit 0;
133 }
134
135 local ( $infile,$outfile);
136 set_files;
137
138
139 ($opt_from = set_auto_from $infile) unless defined($opt_from);
140
141 my @v = versions;
142 ($opt_to =  pop @v) unless (defined($opt_to));
143
144
145 die "can't open \`$infile\'" unless open INLY,$infile ;
146 die "can't open \`$outfile\'" unless open OUTLY, ">$outfile";
147
148 do_conversion $opt_from, $opt_to;
149 close INLY;
150 close OUTLY;
151
152 if ($opt_edit) {
153     rename $infile, "$infile~";
154     rename $outfile, "$infile";
155 }
156