]> git.donarmstrong.com Git - lilypond.git/blob - bin/convert-mudela
release: 0.0.56
[lilypond.git] / bin / convert-mudela
1 #!/usr/bin/perl -w
2
3 #
4 # version of "supporting" engine, not mudela conversions.
5
6
7 $convert_mudela_version = "0.1";
8
9 use Getopt::Long;
10
11
12
13 sub version_string_conv
14 {
15     my ($from_version, $to_version) = @_;
16     s/\version \"0.0.$from_version\"/\version \"0.0.$to_version\"/g;
17 }
18
19 ################################################################
20
21 sub no_conv
22 {
23 }
24  
25 sub convert_0_0_52_to_0_0_53
26 {
27
28     s/include \"/$1\\include \"/g;
29 }
30
31   
32 sub convert_0_0_53_to_0_0_54
33 {
34     print STDERR "Not smart enough to convert \\transpose\n"    if (/\\transpose/) ;
35 }
36
37 ###############################################################
38
39 sub    last_conversion
40 {
41     my @v = &versions;
42     return pop @v;
43 }
44 sub identify
45 {
46     
47     print STDERR "This is convert-mudela " . $convert_mudela_version . 
48         " (up to mudela version 0.0.", last_conversion, ")\n";
49 }
50   
51   
52  sub usage
53   {
54      print STDERR "Usage: convert-mudela [options] [mudela-file]...\n"
55      . "Convert old mudela source from mudela-file or stdin\n\n"
56      . "Options:\n"
57      . "  -e, --edit             perform in-place conversion\n"
58      . "  -f, --from=PATHLEVEL   use source version 0.0.PATCHLEVEL\n"
59      . "  -h, --help             print this help\n"
60      . "  -o, --output=FILE      name output file\n"
61      . "  -s, --show-rules       print all known conversion rules\n"
62      . "  -t, --to=PATCHLEVEL    convert to version 0.0.PATCHLEVEL\n"
63   }
64       
65 # beware of the year 2000
66 my %minor_conversions = (50 => \&no_conv,
67                          52 => \&convert_0_0_50_to_0_0_52,
68                          53 => \&convert_0_0_52_to_0_0_53,
69                          54 => \&convert_0_0_53_to_0_0_54
70                          );
71  
72
73 sub versions 
74 {
75     return  (sort keys %minor_conversions);
76 }
77     
78
79 sub    show_rules
80 {
81     print "Rules: ", join(", ", sort keys %minor_conversions), "\n";
82     
83 }
84
85 sub do_conversion
86 {
87     my ($from,$to) = @_;
88
89     my @applicable_conversion;
90     my @mudela_levels;
91     
92     die "This is too old  to convert " if $from < 50;
93     my @v = versions;
94     foreach $a (@v) {
95         if ($a > $from && $a <= $to ){ 
96             push @applicable_conversion, $minor_conversions{$a};
97             push @mudela_levels, $a;
98         }
99     }
100     
101     print STDERR "Applying following rules: ", join(", ", @mudela_levels) , "\n";
102
103     while (<INLY>) {
104         foreach $subroutine (@applicable_conversion) {
105         
106             &$subroutine;
107             
108         }
109         version_string_conv $from, $to;
110         print OUTLY;
111     }
112 }
113
114 sub get_auto_from
115 {
116     my ($fn)=@_;
117     my ($ver);
118     open INLY, $fn || die "Can't open";
119
120     while (<INLY>) {
121         s/^.*\\version \"([^\"]*)\".*$//;
122         if (defined ($1)) {
123             print STDERR "Guessing version: ", $1, ".. ";
124             $ver = $1;
125             last;
126         }
127     }
128     if (!defined($ver)){
129         print STDERR "can't determine mudela version in $fn.\n";
130         my $u;
131         return $u;
132     }
133     $ver =~ s/0\.0\.// ;
134     close INLY;
135     return $ver;
136 }   
137
138 sub  set_files 
139 {
140     $infile = "-";
141     $outfile = "-";
142     $outfile = $opt_output if (defined($opt_output));
143
144     if ($ARGV [0])  {
145         $infile = $ARGV[0];
146     } 
147     if (( ! -f $infile) && (! $infile =~ /\\.ly$/s ) ){
148         $infile .= ".ly";
149         
150
151     }
152     if ($opt_edit && $infile ne "-") {
153         $opt_edit = 1;
154         $outfile = "$infile.NEW";
155         $infile = "$infile";
156     }
157     print STDERR "Input ", (($infile eq "-") ?"STDIN" : $infile), " .. ";
158
159 }
160
161 sub do_one_arg
162 {
163     set_files;
164
165     local ($from_version, $to_version);
166     $from_version = $opt_from;
167     $to_version = $opt_to;
168     
169     ($from_version = get_auto_from $infile) unless defined($opt_from);
170     return if (!defined($from_version));
171     
172     ($to_version =  last_conversion) unless (defined($opt_to));
173
174
175     die "can't open \`$infile\'" unless open INLY,$infile ;
176     die "can't open \`$outfile\'" unless open OUTLY, ">$outfile";
177     
178     do_conversion $from_version, $to_version;
179     close INLY;
180     close OUTLY;
181
182     if ($opt_edit) {
183         rename $infile, "$infile~";
184         rename $outfile, "$infile";
185     }
186 }
187
188 ## "main"
189
190 identify;
191
192 GetOptions ("help", "output=s", "from=i", "to=i", "minor=i", "edit", "show-rules");
193
194 if ($opt_help) {
195     usage();
196     $opt_help = 0;      # to extinguish typo check.
197     exit 0;
198 }
199
200 if ($opt_show_rules) { 
201     show_rules ;
202     $opt_show_rules = 0;        # to extinguish typo check.
203     exit 0;
204 }
205
206 local ( $infile,$outfile);
207 my $processed_one=0;
208
209 while (defined($ARGV[0])) {
210     do_one_arg;
211     shift @ARGV;
212     $processed_one = 1;
213 }
214 do_one_arg unless ($processed_one);
215
216