#!/usr/bin/perl

#### add option args for movixrc opts

use strict;
use vars qw(
	    %RC
	    $title
	    $isoOut
	    $bootLabel
	    @extra_mkisofs_options
	    $extra_mkisofs_options
	    @extra_mplayer_options
	    $extra_mplayer_options
	    @unwanted_mplayer_options
	    $unwanted_mplayer_options
	    $make_empty_iso
	    $css
	    $loop
	    $random
	    $shut
	    $reboot
	    $eject
	    $fontSet
	    $language
	    $verbose
	    $version
	    $movixDir
	    $fontDir
	    $isoDir
	    $mkisofs_bin
	    $noSubs
	    $bootOptions
	    $stdOptions
	    $extraOptions
	    $hasQT
	    $hasASF
	    $hasWMV
	    $hasRP
	    $hasXANIM
	    $autoDelete
	    $help
	    $nodma
	    $dma
	    $keyboard
	    );

my $pwd = $ENV{"PWD"};
my $bin_dir  = $0;
$bin_dir     =~ s/\/?[^\/]*$//g;
$mkisofs_bin = "mkisofs";

##################################
###  Retrieving options from .mkmovixisorc
###  These are the default options for this script
###########################
my $mkmovixisorc = $ENV{"HOME"}."/.mkmovixisorc";
if( -e $mkmovixisorc ){
  open(RC,$mkmovixisorc);
  while(<RC>){
    next if /^\#/;
    next unless /^([^\=]+)\=(.*)$/;
    $RC{$1}=$2;
  }
  close(RC);
}
$isoOut    = $RC{'output-file'}   if exists $RC{'output-file'};
$bootLabel = $RC{'boot-label'}    if exists $RC{'boot-label'};
$fontSet   = $RC{'subtitleFonts'} if exists $RC{'subtitleFonts'};
$language  = $RC{'language'}      if exists $RC{'language'};
$mkisofs_bin = $RC{'mkisofs_bin'} if exists $RC{'mkisofs_bin'};
$keyboard  = $RC{'keyboard'}      if exists $RC{'keyboard'};
$extra_mkisofs_options    = $RC{'extra-mkiso-options'}      if exists $RC{'extra-mkiso-options'};
$extra_mplayer_options    = $RC{'extra-mplayer-options'}    if exists $RC{'extra-mplayer-options'};
$unwanted_mplayer_options = $RC{'unwanted-mplayer-options'} if exists $RC{'unwanted-mplayer-options'};
$css    = "y" if $RC{'css'} eq "y";
$loop   = $RC{'loop'} if exists $RC{'loop'} and $RC{'loop'} =~ /^\d+$/;
$random = "y" if $RC{'random'} eq "y";
$shut =   "y" if $RC{'shut'}   eq "y";
$reboot = "y" if $RC{'reboot'} eq "y";
$eject  = "y" if $RC{'eject'}  eq "y";
$nodma  = "y" if $RC{'dma'}    eq "n";
$autoDelete  = 1 if $RC{'auto-delete'}  eq "y";

#######################
### Retrieve options from command line
#################################
use Getopt::Long;
use Pod::Usage;

GetOptions (
	    "title=s"             => \$title,
	    "output-file=s"       => \$isoOut,
	    "boot-label=s"        => \$bootLabel,
	    "extra-mkiso-options=s"       => \@extra_mkisofs_options,
	    "extra-mplayer-options=s"     => \@extra_mplayer_options,
	    "unwanted-mplayer-options=s"  => \@unwanted_mplayer_options,
	    "make-empty-iso"      => \$make_empty_iso,
	    "css"                 => \$css,
	    "loop=i"              => \$loop,
	    "random"              => \$random,
	    "reboot"              => \$reboot,
	    "shut"                => \$shut, 
	    "eject"               => \$eject,
	    "auto-delete"         => \$autoDelete,
	    "movixDir=s"          => \$movixDir,
	    "mkisofs=s"           => \$mkisofs_bin,
	    "subtitleFonts=s"     => \$fontSet,
	    "language=s"          => \$language,
	    "keyboard=s"          => \$keyboard,
	    "hasQT"               => \$hasQT,
	    "hasWMV"              => \$hasWMV,
	    "hasASF"              => \$hasASF,
	    "hasRP"               => \$hasRP,
	    "hasXANIM"            => \$hasXANIM,
	    "verbose"             => \$verbose,
	    "nodma"               => \$nodma,
	    "movix-version"       => \$version,
	    'help|?'              => \$help
	    );

$extra_mkisofs_options = join(' ',split(/,/,join(',',@extra_mkisofs_options))) if @extra_mkisofs_options > 0;
$extra_mplayer_options = join(' ',split(/,/,join(',',@extra_mplayer_options))) if @extra_mplayer_options > 0;
$unwanted_mplayer_options = join(' ',split(/,/,join(',',@unwanted_mplayer_options))) if @unwanted_mplayer_options > 0;

$fontSet  = "FreeSerifBoldItalic" unless defined $fontSet;

my $prefix = `$bin_dir/movix-conf`;
chop $prefix;

### collect all known languages avaialble for the boot messages
my $knownLanguages = `ls \"$prefix/boot-messages/\"`;
$knownLanguages =~ s/\n/ /g;
chop $knownLanguages;
$knownLanguages = " $knownLanguages ";

### collect all known kbd layouts avaialble 
my $knownKeymaps = `ls \"$prefix/keyboard-i18n/\"`;
$knownKeymaps =~ s/\n/ /g;
chop $knownKeymaps;
$knownKeymaps = " $knownKeymaps ";

# Check if the given lang is available
$language = "en" unless defined $language;
unless( $knownLanguages =~ / $language / ){
    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!\n\n";
    help();
    exit;
}

if( defined $keyboard ){
  unless( $knownKeymaps =~ / $keyboard / ){
    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";
    help();
    exit;
  }
} else {
  $keyboard = ( $knownKeymaps =~ / $language / ) ? $language : "us";
  $keyboard =~ s/^en$/us/;
}

if( ! defined $movixDir ){
    $movixDir = `$bin_dir/movix-conf`;
    chop $movixDir;
}

if( defined $help ){
  help();
  exit;
}

if( defined $version ){
  system "movix-version";
  exit;
}

if( defined $isoOut ){
  $isoOut = $pwd."/".$isoOut if $isoOut !~ /^\//;
} else {
  print "\nYou _must_ provide an output file!\n";
  help();
  exit;
}

if( @ARGV == 0 and ! defined $make_empty_iso ){
  print "\nYou _must_ provide at least one input file!\n";
  help();
  exit;
}

if( defined $title ) {
  my @chars = split '', $title;
  if( @chars > 32 ){
    print "\nTitle length cannot be longer than 32 characters!\n";
    exit;
  }
} else {
  my $version = `$bin_dir/movix-version`;
  chop $version;
  $title    = "eMoviX $version CD";
}

################################
### Building the temporary dir and linking there
### all audio/video/auxiliary files
################################
my $date = `date +%s`;
chop $date;
my $tmpDir = "/tmp/movix-$date";
mkdir  "$tmpDir";
chdir  "$tmpDir";
mkdir  "movix";
foreach ( split "\n", `$bin_dir/movix-files` ){
  `ln -s "$movixDir/movix/$_" movix`;
}
#system "ln -s \"$movixDir\"/movix/* movix";

### dealing with codecs
#`rm -f movix/win32`;
my @codecs = split "\n", `ls "$movixDir"/codecs/ 2> /dev/null`;
if( @codecs > 0 ){
  `mkdir -p mplayer/codecs/`;
  `ln -s "$movixDir"/codecs/* mplayer/codecs`;
  `rm -f mplayer/codecs/qt*`    unless defined $hasQT;
  `rm -f mplayer/codecs/win32*` unless defined $hasASF or defined $hasWMV;
  `rm -f mplayer/codecs/rp*`    unless defined $hasRP;
  `rm -f mplayer/codecs/xanim*` unless defined $hasXANIM;
}

### dealing with libdvdcss - you'll need this if you want to play encrypted DVDs
system "rm -f movix/libdvdcss*"   unless defined $css;

mkdir  "isolinux";
system "ln -s \"$movixDir\"/isolinux/* isolinux";

# Link the isolinux boot-keymap to the selected keyboard layout
system "ln -s \"$movixDir\"/keyboard-i18n/$keyboard/bootkey.map isolinux/bootkey.map";

system "rm -f isolinux/isolinux.bin";
system "cp \"$movixDir\"/isolinux/isolinux.bin isolinux/isolinux.bin";
if( defined $bootLabel ){
  die "boot label $bootLabel is unknown :-(\n"
    unless $bootLabel =~ /^vesa|vesaFB(\d*)?|FB|aa(\d*)?|sdl$/;
  my $isolinuxcfg = "default $bootLabel\n";
  open CFG, "isolinux/isolinux.cfg";
  <CFG>;
  while(<CFG>){
    $isolinuxcfg .= $_;
  }
  close CFG;
  system "rm -f isolinux/isolinux.cfg";
  open CFG, "> isolinux/isolinux.cfg";
  print CFG $isolinuxcfg;
  close CFG;
}

### next instruction will disappear once the old txt messages will be
### removed from the src/isolinux dir
system "rm -f isolinux/*txt";
system "ln -s \"$movixDir\"/boot-messages/$language/*txt isolinux";

foreach my $file (@ARGV){
    $file = $pwd."/".$file if $file !~ /^\//;
    if( -d $file ){
	system "ln -s $file/* .";
    } elsif( -e $file ) {
	system "ln -s \"$file\" .";
    } else {
	die "The file $file does not exist! Please check and retry.\n";
    }
}

$fontDir = "mplayer/font";
if( -e "$movixDir/mplayer-fonts/$fontSet/font.desc" ){
  system "mkdir -p $fontDir";
  system "ln -s \"$movixDir\"/mplayer-fonts/$fontSet/* $fontDir";
} else {
#  print "\"$movixDir\"/mplayer-fonts/$fontSet/font.desc"."\n";
#  print "pippo\n" if -e "/usr/local/share/emovix";
#  print "pippo->\n" if -e "$movixDir";
#  $movixDir = "/usr/a b";
#  print "<-pippo\n" if -e "$movixDir";
#  print "pippo\n" if -e "$movixDir/mplayer-fonts/$fontSet/font.desc";
#  die "che ci fo qui?";
  system "mkdir -p mplayer";
  system "ln -s \"$movixDir\"/mplayer-fonts/$fontSet\.ttf mplayer/subfont.ttf";
}

# Link the selected shell keymap 
system "ln -s \"$movixDir\"/keyboard-i18n/$keyboard/shellkey.map movix/shellkey.map";

open MOVIXRC, "> movix/movixrc";
print MOVIXRC "extra-mplayer-options=$extra_mplayer_options\n" if defined $extra_mplayer_options;
print MOVIXRC "unwanted-mplayer-options=$unwanted_mplayer_options\n" if defined $unwanted_mplayer_options;
print MOVIXRC "loop=$loop\n" if defined $loop;
print MOVIXRC "shut=y\n"     if defined $shut;
print MOVIXRC "reboot=y\n"   if defined $reboot;
print MOVIXRC "random=y\n"   if defined $random;
print MOVIXRC "eject=y\n"    if defined $eject;
print MOVIXRC "dma=n\n"      if defined $nodma;
close MOVIXRC;

print "\n";
if( $verbose == 1 ){
    print "Your movix directory seems to be in: \"$movixDir\"\n";
    print "MoviX temporary directory is:        $tmpDir\n";
}

##############
### Standard booting options, from SysLinux docs
### and from slackware installation CD README
###############
$bootOptions = "-no-emul-boot -boot-load-size 4 -boot-info-table -b isolinux/isolinux.bin -c isolinux/isolinux.boot -sort isolinux/iso.sort";

##############
### Standard mkisofs options: -v=>verbose, -r=>rock-ridge,
####                          -J=>Joliet,  -f=>follow symbolic links
##############
$stdOptions = "-v -r -J -f";

#######
### User's extra options (if any)
#########
if( defined @extra_mkisofs_options ) {
    $extraOptions .= $extra_mkisofs_options;
}

my $mkiso  = "$mkisofs_bin -o \"$isoOut\" $stdOptions -V \"$title\" $extraOptions $bootOptions .";
if( $verbose == 1 ){
    print "Starting mkisofs...\n$mkiso\n";
} else {
    $mkiso .= "> /dev/null 2> /dev/null";
}
system "$mkiso";

print "Your iso image is now in \"$isoOut\":\n";
print `ls -l \"$isoOut\"`;
if ( $autoDelete == 1 ) {
    system "rm -rf '$tmpDir'";
    print "The temporary directory '$tmpDir' was deleted.\n\n";
} else {
    print "You can safely delete the temporary directory $tmpDir\n\n";
}

##########################################################
####  Eventually this should be replaced by pod docs
######################

sub help {
    print
"Usage: mkmovixiso [options] file1 ... fileN dir1 ... dirN
Note: All options may be be abbreviated to uniqueness and
      a single dash is sufficient
Options:

 Generic:

   --title=<title>         Title of the iso image
   --output-file=<file>    Name [with full path!] of the output iso image
   --boot-label=<label>    Make MoviX boot by default with your favorite label
   --subtitleFonts=<lang>  Specify which font set will be included in the iso image.

Available font sets:
 - MPlayer standard font sets
   ";

    my $prefix = `$bin_dir/movix-conf`;
    chop $prefix;
    my @fonts = split "\n", `ls $prefix/mplayer-fonts/`;
    my @stFonts;
    my @ttFonts;
    foreach (@fonts){
      push @stFonts, $_ if -d "$prefix/mplayer-fonts/$_";
      push @ttFonts, $_ if /.ttf$/i;
    }
    
    for( my $i=0; $i < @stFonts; $i++ ){
      print "$stFonts[$i]";
      print " (default)" if $fontSet eq $stFonts[$i];
      print ", " unless $i == @stFonts - 1;
    }
    
    print "

 - True type font sets:
   ";

    for( my $i=0; $i < @ttFonts; $i++ ){
      $ttFonts[$i] =~ s/.ttf$//i;
      print "$ttFonts[$i]";
      print " (default)" if $fontSet eq $ttFonts[$i];
      print ", " unless $i == @ttFonts - 1;
    }

print "

   --language=<lang>       Specify the language of the CD boot messages

Available Languages:
en - English (default),  it - Italian,     es - Spanish,      nl - Dutch,
hu - Hungarian,          de - German,      fr - French,       pt - Portuguese,
pl - Polish,             jp - Japanese,    sar - Sardinian,   esp - Esperanto

   --keyboard=<lang>       Specify the keyboard layout to use

Available keyboard layouts:
us - US English (default), de - German     es - Spanish,      fr - French,
hu - Hungarian,            it - Italian,   nl - Dutch,        pt - Portuguese

   --movixDir=<dir>        Where the movix data dir is (no need to specify it if
                           you didn't move it from its default install location)
   --mkisofs=<mkisofs exe> Full path of mkisofs, in case it's not in the \$PATH
   --css                   Include the dvdcss library (see README for more info)
   --loop=<k>              Make MPlayer play k times your files before stopping [0=infinity]
   --random                Make MPlayer play your files in random order
   --eject                 Make MoviX eject the CD once it finished playing
   --reboot                Make MoviX reboot the PC once it finished playing
   --shut                  Make MoviX shut the PC off once it finished playing
   --hasQT                 Include DLL files required to play QuickTime files
   --hasASF                Include DLL files required to play .asf files
   --hasWMV                Include DLL files required to play .wmv files
   --hasRP                 Include DLL files required to play RealPlayer files
   --hasXANIM              Include DLL files extracted from the xanim project
   --nodma                 Do not enable fast mode for CD-ROM devices by default
   --auto-delete           Automatically remove the temporary directory
   --extra-mkiso-options=\"opt1 opt2 ...\"
                           Extra options for mkisofs
   --extra-mplayer-options=\"opt1 opt2 ...\"
                           Extra options for mplayer
   --unwanted-mplayer-options=\"opt1 opt2 ...\"
                           MPlayer options you want to be sure MPlayer will not use
   --movix-version         Print the version number and exit
   --verbose               Be verbose
   --help                  This help

";
}
