1#!/usr/local/bin/perl
2
3#### add option args for movixrc opts
4
5use strict;
6use Cwd 'abs_path';
7use vars qw(
8	    %RC
9	    $title
10	    $isoOut
11	    $bootLabel
12	    @extra_mkisofs_options
13	    $extra_mkisofs_options
14	    @extra_mplayer_options
15	    $extra_mplayer_options
16	    @unwanted_mplayer_options
17	    $unwanted_mplayer_options
18	    $make_empty_iso
19	    $css
20	    $loop
21	    $random
22	    $shut
23	    $reboot
24	    $eject
25	    $fontSet
26	    $bgVideo
27	    $language
28	    $verbose
29	    $version
30	    $movixDir
31	    $fontDir
32	    $isoDir
33	    $distro
34	    $mkisofs_bin
35	    $noSubs
36	    $bootOptions
37	    $stdOptions
38	    $extraOptions
39	    $hasQT
40	    $hasASF
41	    $hasWMV
42	    $hasRP
43	    $hasXANIM
44	    $autoDelete
45	    $help
46	    $nodma
47	    $noSubhelp
48	    $dma
49	    $keyboard
50	    );
51
52my $pwd = $ENV{"PWD"};
53my $bin_dir  = $0 =~ /^\// ? $0 : abs_path($0);
54$bin_dir     =~ s/\/?[^\/]*$//g;
55$mkisofs_bin = "mkisofs";
56
57##################################
58###  Retrieving options from .mkmovixisorc
59###  These are the default options for this script
60###########################
61my $mkmovixisorc = $ENV{"HOME"}."/.mkmovixisorc";
62if( -e $mkmovixisorc ){
63  open(RC,$mkmovixisorc);
64  while(<RC>){
65    next if /^\#/;
66    next unless /^([^\=]+)\=(.*)$/;
67    $RC{$1}=$2;
68  }
69  close(RC);
70}
71$isoOut    = $RC{'output-file'}   if exists $RC{'output-file'};
72$bootLabel = $RC{'boot-label'}    if exists $RC{'boot-label'};
73$fontSet   = $RC{'subtitleFonts'} if exists $RC{'subtitleFonts'};
74$bgVideo   = $RC{'bgVideo'}       if exists $RC{'bgVideo'};
75$language  = $RC{'language'}      if exists $RC{'language'};
76$distro    = $RC{'distro'}        if exists $RC{'distro'};
77$mkisofs_bin = $RC{'mkisofs_bin'} if exists $RC{'mkisofs_bin'};
78$keyboard  = $RC{'keyboard'}      if exists $RC{'keyboard'};
79$extra_mkisofs_options    = $RC{'extra-mkisofs-options'}    if exists $RC{'extra-mkisofs-options'};
80$extra_mplayer_options    = $RC{'extra-mplayer-options'}    if exists $RC{'extra-mplayer-options'};
81$unwanted_mplayer_options = $RC{'unwanted-mplayer-options'} if exists $RC{'unwanted-mplayer-options'};
82$css    = "y" if $RC{'css'} eq "y";
83$loop   = $RC{'loop'} if exists $RC{'loop'} and $RC{'loop'} =~ /^\d+$/;
84$random = "y" if $RC{'random'} eq "y";
85$shut =   "y" if $RC{'shut'}   eq "y";
86$reboot = "y" if $RC{'reboot'} eq "y";
87$eject  = "y" if $RC{'eject'}  eq "y";
88$nodma  = "y" if $RC{'dma'}    eq "n";
89$noSubhelp = "y" if $RC{'subhelp'}    eq "n";
90$autoDelete  = 1 if $RC{'auto-delete'}  eq "y";
91
92#######################
93### Retrieve options from command line
94#################################
95use Getopt::Long;
96use Pod::Usage;
97
98GetOptions (
99	    "title=s"             => \$title,
100	    "output-file=s"       => \$isoOut,
101	    "boot-label=s"        => \$bootLabel,
102	    "extra-mkisofs-options=s"     => \@extra_mkisofs_options,
103	    "extra-mplayer-options=s"     => \@extra_mplayer_options,
104	    "unwanted-mplayer-options=s"  => \@unwanted_mplayer_options,
105	    "make-empty-iso"      => \$make_empty_iso,
106	    "css"                 => \$css,
107	    "loop=i"              => \$loop,
108	    "random"              => \$random,
109	    "reboot"              => \$reboot,
110	    "shut"                => \$shut,
111	    "eject"               => \$eject,
112	    "auto-delete"         => \$autoDelete,
113	    "movixDir=s"          => \$movixDir,
114	    "distro=s"            => \$distro,
115	    "mkisofs=s"           => \$mkisofs_bin,
116	    "subtitleFonts=s"     => \$fontSet,
117	    "bgVideo=s"           => \$bgVideo,
118	    "language=s"          => \$language,
119	    "keyboard=s"          => \$keyboard,
120	    "hasQT"               => \$hasQT,
121	    "hasWMV"              => \$hasWMV,
122	    "hasASF"              => \$hasASF,
123	    "hasRP"               => \$hasRP,
124	    "hasXANIM"            => \$hasXANIM,
125	    "verbose"             => \$verbose,
126	    "nodma"               => \$nodma,
127	    "nosubhelp"           => \$noSubhelp,
128	    "movix-version"       => \$version,
129	    'help|?'              => \$help
130	    );
131
132$extra_mkisofs_options = join(' ',split(/,/,join(',',@extra_mkisofs_options))) if @extra_mkisofs_options > 0;
133$extra_mplayer_options = join(' ',split(/,/,join(',',@extra_mplayer_options))) if @extra_mplayer_options > 0;
134$unwanted_mplayer_options = join(' ',split(/,/,join(',',@unwanted_mplayer_options))) if @unwanted_mplayer_options > 0;
135
136$fontSet  = "FreeSerifBoldItalic" unless defined $fontSet;
137
138if( ! defined $movixDir ){
139	$movixDir = -e "$bin_dir/movix-conf" ? `$bin_dir/movix-conf` : `movix-conf`;
140    chop $movixDir;
141}
142
143#$bgVideo  = "$movixDir/movix/movix.music.avi" unless defined $bgVideo;
144
145if( defined $help ){
146  help();
147  exit;
148}
149
150if( defined $version ){
151  system "movix-version";
152  exit;
153}
154
155if( defined $isoOut ){
156  $isoOut = $pwd."/".$isoOut if $isoOut !~ /^\//;
157} else {
158  print "\nYou _must_ provide an output file!\n";
159  help();
160  exit;
161}
162
163if( @ARGV == 0 and ! defined $make_empty_iso ){
164  print "\nYou _must_ provide at least one input file!\n";
165  help();
166  exit;
167}
168
169if( defined $title ) {
170  my @chars = split '', $title;
171  if( @chars > 32 ){
172    print "\nTitle length cannot be longer than 32 characters!\n";
173    exit;
174  }
175} else {
176  my $version = `$bin_dir/movix-version`;
177  chop $version;
178  $title    = "eMoviX $version CD";
179}
180
181### collect all known languages available for the boot messages
182my $knownLanguages = `ls \"$movixDir/translations/\"`;
183$knownLanguages =~ s/\n/ /g;
184chop $knownLanguages;
185$knownLanguages = " $knownLanguages ";
186
187### collect all known kbd layouts avaialble
188my $knownKeymaps = `ls \"$movixDir/keyboard-i18n/\"`;
189$knownKeymaps =~ s/\n/ /g;
190chop $knownKeymaps;
191$knownKeymaps = " $knownKeymaps ";
192
193# Check if the given lang is available
194$language = "en" unless defined $language;
195unless( $knownLanguages =~ / $language / ){
196    print "Label $language does not correspond to any language supported by eMoviX :-(\nIf you would like to provide translations for it post a message on the movix forums at http://movix.sf.net/ !\n\n";
197    help();
198    exit;
199}
200
201if( defined $keyboard ){
202  unless( $knownKeymaps =~ / $keyboard / ){
203    print "Keyboard layout $keyboard does not correspond to any layouts supported by eMoviX :-(\nIf you would like to provide a keyboard layout, read keyboard-i18n/README in the eMoviX distribution! :-)\n\n";
204    help();
205    exit;
206  }
207} else {
208  $keyboard = ( $knownKeymaps =~ / $language / ) ? $language : "us";
209  $keyboard =~ s/^en$/us/;
210}
211
212################################
213### Building the temporary dir and linking there
214### all audio/video/auxiliary files
215################################
216my $date = `date +%s`;
217chop $date;
218my $tmpDir = "/tmp/movix-$date";
219mkdir  "$tmpDir";
220chdir  "$tmpDir";
221mkdir  "eMoviX";
222mkdir  "eMoviX/boot";
223mkdir  "eMoviX/boot/kernel";
224mkdir  "eMoviX/movix";
225mkdir  "eMoviX/mplayer";
226mkdir  "eMoviX/help";
227foreach ( split "\n", `$bin_dir/movix-files` ){
228  `ln -s "$movixDir/movix/$_" eMoviX/movix`;
229}
230mkdir "eMoviX/remotes";
231`ln -s "$movixDir/remotes/$_" eMoviX/remotes`
232  foreach (split "\n", `ls "$movixDir/remotes"`);
233#system "ln -s \"$movixDir\"/movix/* movix";
234
235### dealing with codecs
236#`rm -f movix/win32`;
237my @codecs = split "\n", `ls "$movixDir"/codecs/ 2> /dev/null`;
238if( @codecs > 0 ){
239  `mkdir -p eMoviX/codecs/`;
240  `ln -s "$movixDir"/codecs/* eMoviX/codecs`;
241  `rm -f eMoviX/codecs/qt*`    unless defined $hasQT;
242  `rm -f eMoviX/codecs/win32*` unless defined $hasASF or defined $hasWMV;
243  `rm -f eMoviX/codecs/rp*`    unless defined $hasRP;
244  `rm -f eMoviX/codecs/xanim*` unless defined $hasXANIM;
245}
246
247### dealing with libdvdcss - you'll need this if you want to play encrypted DVDs
248`ln -s "$movixDir"/movix/libdvdcss* eMoviX/mplayer` if defined $css;
249#system "rm -f movix/libdvdcss*"   unless defined $css;
250
251#mkdir  "eMoviX";
252#mkdir  "eMoviX/kernel";
253system "ln -s \"$movixDir\"/isolinux/{iso.sort,movix.*} eMoviX/boot";
254
255# If the option --distro was given, another initrd file should be linked
256my $distfile = '';
257my $warned = 0;
258if (defined $distro) {
259  foreach $distfile ("initrd.gz", "isolinux.cfg", "kernel/vmlinuz") {
260
261    if ( -e "$movixDir/isolinux/$distfile.$distro" ) {
262    # the file of the selected distribution is there
263      system "ln -s \"$movixDir\"/isolinux/$distfile.$distro eMoviX/boot/$distfile";
264    } else {
265    # not here - use the one without the prefix
266      if ($warned == 0) {
267        print "Warning: the files necessary for --distro=$distro are missing.\nDefault ones will be used.\n";
268        $warned = 1;
269      }
270
271      system "ln -s \"$movixDir\"/isolinux/$distfile eMoviX/boot/$distfile";
272    }
273
274  }
275} else {
276# use the normal distro files
277  foreach $distfile ("initrd.gz", "isolinux.cfg", "kernel/vmlinuz") {
278    system "ln -s \"$movixDir\"/isolinux/$distfile eMoviX/boot/$distfile";
279  }
280}
281
282# Link the isolinux boot-keymap to the selected keyboard layout
283system "ln -s \"$movixDir\"/keyboard-i18n/$keyboard/bootkey.map eMoviX/boot/bootkey.map";
284
285#system "rm -f isolinux/isolinux.bin";
286system "cp \"$movixDir\"/isolinux/isolinux.bin eMoviX/boot/isolinux.bin";
287if( defined $bootLabel ){
288  die "boot label $bootLabel is unknown :-(\n"
289    unless $bootLabel =~ /^vesa|vesaFB(\d*)?|FB|aa(\d*)?|sdl$/;
290  my $isolinuxcfg = "default $bootLabel\n";
291  open CFG, "eMoviX/boot/isolinux.cfg";
292  <CFG>;
293  while(<CFG>){
294    $isolinuxcfg .= $_;
295  }
296  close CFG;
297  system "rm -f eMoviX/boot/isolinux.cfg";
298  open CFG, "> eMoviX/boot/isolinux.cfg";
299  print CFG $isolinuxcfg;
300  close CFG;
301}
302
303### next instruction will disappear once the old txt messages will be
304### removed from the src/isolinux dir
305#system "rm -f isolinux/*txt";
306system "ln -sf \"$movixDir\"/translations/$language/*txt eMoviX/boot";
307system "ln -sf \"$movixDir\"/translations/$language/*.sh eMoviX/help";
308system "ln -sf \"$movixDir\"/translations/$language/*.msg eMoviX/boot";
309
310system "ln -sf \"$movixDir\"/translations/$language/*.man eMoviX/help";
311system "ln -sf \"$movixDir\"/translations/$language/*.sub eMoviX/help";
312
313### localisation of mplayer menu
314system "ln -sf \"$movixDir\"/translations/$language/menu.conf eMoviX/mplayer/menu.conf";
315
316foreach my $file (@ARGV){
317    $file = $pwd."/".$file if $file !~ /^\//;
318    if( -d $file ){
319        system "ln -s $file/* .";
320    } elsif( -e $file ) {
321        system "ln -s \"$file\" .";
322    } else {
323        die "The file $file does not exist! Please check and retry.\n";
324    }
325}
326
327$fontDir = "eMoviX/font";
328if( -e "$movixDir/mplayer-fonts/$fontSet/font.desc" ){
329  system "mkdir -p $fontDir";
330  system "ln -s \"$movixDir\"/mplayer-fonts/$fontSet/* $fontDir";
331} else {
332#  print "\"$movixDir\"/mplayer-fonts/$fontSet/font.desc"."\n";
333#  print "pippo\n" if -e "/usr/local/share/emovix";
334#  print "pippo->\n" if -e "$movixDir";
335#  $movixDir = "/usr/a b";
336#  print "<-pippo\n" if -e "$movixDir";
337#  print "pippo\n" if -e "$movixDir/mplayer-fonts/$fontSet/font.desc";
338#  die "che ci fo qui?";
339  #system "mkdir -p eMoviX";
340#  system "ln -s \"$movixDir\"/mplayer-fonts/$fontSet\.ttf eMoviX/mplayer/subfont.ttf";
341  system "ln -s \"$movixDir\"/mplayer-fonts/$fontSet\.ttf eMoviX/mplayer/";
342}
343
344if ( $bgVideo =~ /^none$/ ) {
345  #print "Not including a background video.";
346  system "rm eMoviX/movix/movix.music.avi";
347} elsif ( $bgVideo eq '' ) {
348# No special background video given, use the one already determined by movix-files
349  # do nothing
350} else {
351# A filename was given
352
353  if ( -e $bgVideo ) {
354  # Absolute path to an existing file
355    system "rm eMoviX/movix/movix.music.avi; \
356            ln -s '$bgVideo' eMoviX/movix/movix.music.avi";
357
358  } elsif ( -e "$pwd/$bgVideo" ){
359  # The video is in the current directory, or a relative path was given
360    system "rm eMoviX/movix/movix.music.avi; \
361            ln -s '$pwd/$bgVideo' eMoviX/movix/movix.music.avi";
362
363  } elsif ( -e "$movixDir/backgrounds/$bgVideo" ){
364  # Perhaps in $movixDir/backgrounds/?
365    system "rm eMoviX/movix/movix.music.avi; \
366            ln -s '$movixDir/backgrounds/$bgVideo' eMoviX/movix/movix.music.avi";
367  } else {
368  # Sorry
369    die "Background video $bgVideo couldn't be found!\n";
370  }
371}
372
373# Link the selected shell keymap
374system "ln -s \"$movixDir\"/keyboard-i18n/$keyboard/shellkey.map eMoviX/boot/shellkey.map";
375
376open MOVIXRC, "> eMoviX/movix/movixrc";
377print MOVIXRC "extra-mplayer-options=$extra_mplayer_options\n" if defined $extra_mplayer_options;
378print MOVIXRC "unwanted-mplayer-options=$unwanted_mplayer_options\n" if defined $unwanted_mplayer_options;
379print MOVIXRC "loop=$loop\n" if defined $loop;
380print MOVIXRC "shut=y\n"     if defined $shut;
381print MOVIXRC "reboot=y\n"   if defined $reboot;
382print MOVIXRC "random=y\n"   if defined $random;
383print MOVIXRC "eject=y\n"    if defined $eject;
384print MOVIXRC "dma=n\n"      if defined $nodma;
385print MOVIXRC "noSubhelp=y\n" if defined $noSubhelp;
386close MOVIXRC;
387
388print "\n";
389if( $verbose == 1 ){
390    print "Your movix directory seems to be in: \"$movixDir\"\n";
391    print "MoviX temporary directory is:        $tmpDir\n";
392}
393
394##############
395### Standard booting options, from SysLinux docs
396### and from slackware installation CD README
397###############
398$bootOptions = "-no-emul-boot -boot-load-size 4 -boot-info-table -b eMoviX/boot/isolinux.bin -c eMoviX/boot/isolinux.boot -sort eMoviX/boot/iso.sort";
399
400##############
401### Standard mkisofs options: -v=>verbose, -r=>rock-ridge,
402####                          -J=>Joliet,  -f=>follow symbolic links
403##############
404$stdOptions  = "-v -r -J -f ";
405#
406### -joliet-long is supported only by mkisofs 2.x.x binaries!
407#
408$stdOptions .= "-joliet-long " if `mkisofs --version` =~ /^mkisofs 2/;
409
410#######
411### User's extra options (if any)
412#########
413if( defined $extra_mkisofs_options ) {
414    $extraOptions .= $extra_mkisofs_options;
415}
416
417my $mkiso  = "$mkisofs_bin -o \"$isoOut\" $stdOptions -V \"$title\" $extraOptions $bootOptions .";
418if( $verbose == 1 ){
419    print "Starting mkisofs...\n$mkiso\n";
420} else {
421    $mkiso .= "> /dev/null 2> /dev/null";
422}
423system "$mkiso";
424
425print "Your iso image is now in \"$isoOut\":\n";
426print `ls -l \"$isoOut\"`;
427if ( $autoDelete == 1 ) {
428    system "rm -rf '$tmpDir'";
429    print "The temporary directory '$tmpDir' was deleted.\n\n";
430} else {
431    print "You can safely delete the temporary directory $tmpDir\n\n";
432}
433
434##########################################################
435####  Eventually this should be replaced by pod docs
436######################
437
438sub help {
439    print
440"Usage: mkmovixiso [options] file1 ... fileN dir1 ... dirN
441Note: All options may be be abbreviated to uniqueness and
442      a single dash is sufficient
443Options:
444
445 Generic:
446
447   --title=<title>         Title of the iso image
448   --output-file=<file>    Name [with full path!] of the output iso image
449   --boot-label=<label>    Make MoviX boot by default with your favorite label
450   --subtitleFonts=<lang>  Specify which font set will be included in the iso image.
451
452Available font sets:
453 - MPlayer standard font sets
454   ";
455
456    my @fonts = split "\n", `ls $movixDir/mplayer-fonts/`;
457    my @stFonts;
458    my @ttFonts;
459    foreach (@fonts){
460      push @stFonts, $_ if -d "$movixDir/mplayer-fonts/$_";
461      push @ttFonts, $_ if /.ttf$/i;
462    }
463    for( my $i=0; $i < @stFonts; $i++ ){
464      print "$stFonts[$i]";
465      print " (default)" if $fontSet eq $stFonts[$i];
466      print ", " unless $i == @stFonts - 1;
467    }
468    print "
469
470 - True type font sets:
471   ";
472
473    for( my $i=0; $i < @ttFonts; $i++ ){
474      $ttFonts[$i] =~ s/.ttf$//i;
475      print "$ttFonts[$i]";
476      print " (default)" if $fontSet eq $ttFonts[$i];
477      print ", " unless $i == @ttFonts - 1;
478    }
479
480print "
481
482   --distro=<distribution> Specify the distribution to use (only if you are
483                             using the full eMoviX package)
484
485Available distributions in the full eMoviX package: mini, normal, full
486
487 mini:   just the drivers necessary for video and audio playback
488 normal: mini + LIRC remote support + character set conversion + aa, SDL output
489 full:   normal + DXR output
490
491   --language=<lang>       Specify the language of the CD boot messages
492
493Available Languages:
494en - English (default),  it - Italian,     es - Spanish,      nl - Dutch,
495hu - Hungarian,          de - German,      fr - French,       pt - Portuguese,
496pl - Polish,             jp - Japanese,    sar - Sardinian,   esp - Esperanto
497
498   --keyboard=<lang>       Specify the keyboard layout to use
499
500Available keyboard layouts:
501us - US English (default), de - German     es - Spanish,      fr - French,
502hu - Hungarian,            it - Italian,   nl - Dutch,        pt - Portuguese,
503be - Belgian
504
505   --movixDir=<dir>        Where the movix data dir is (no need to specify it if
506                           you didn't move it from its default install location)
507   --mkisofs=<mkisofs exe> Full path of mkisofs, in case it's not in the \$PATH
508   --bgVideo=file.avi      A video file to be used as background for audio file playback
509                           ('none' for not including the video file)
510 - Available backgrounds:
511";
512
513    my @bkg = split "\n", `ls $movixDir/backgrounds/`;
514    foreach(@bkg){
515      print "$_\n";
516    }
517
518print "
519   --css                   Include the dvdcss library (see README for more info)
520   --loop=<k>              Make MPlayer play k times your files before stopping [0=infinity]
521   --random                Make MPlayer play your files in random order
522   --eject                 Make MoviX eject the CD once it finished playing
523   --reboot                Make MoviX reboot the PC once it finished playing
524   --shut                  Make MoviX shut the PC off once it finished playing
525   --hasQT                 Include DLL files required to play QuickTime files
526   --hasASF                Include DLL files required to play .asf files
527   --hasWMV                Include DLL files required to play .wmv files
528   --hasRP                 Include DLL files required to play RealPlayer files
529   --hasXANIM              Include DLL files extracted from the xanim project
530   --nodma                 Do not enable fast mode for CD-ROM devices by default
531   --nosubhelp             Do not show the short subtitle help in movie files
532   --auto-delete           Automatically remove the temporary directory
533   --extra-mkisofs-options=\"opt1 opt2 ...\"
534                           Extra options for mkisofs
535   --extra-mplayer-options=\"opt1 opt2 ...\"
536                           Extra options for mplayer
537   --unwanted-mplayer-options=\"opt1 opt2 ...\"
538                           MPlayer options you want to be sure MPlayer will not use
539   --movix-version         Print the version number and exit
540   --verbose               Be verbose
541   --help                  This help
542
543";
544}
545