# cdr Installation Utility
#
# Prompts for necessary values, compiles the additional utilities and
# installs everything to its final location.
#
# Created by David Cantrell
# Last updated 9-Mar-2001 by Mike Hardy

###############################
### Check rootness
###############################
if($ENV{"LOGNAME"} ne "root") {
   print "You must be root to install cdr.\n";
   exit;
}

###############################
### Intro screen
###############################
system "clear";
print "\n  -------------------------------------------------------------\n";
print "                   cdr v2.0 Installation Utility\n";
print "  -------------------------------------------------------------\n";
print "\n";
print "  Please wait while install prepares the Setup Wizard which\n";
print "  will guide you through the rest of the setup process...\n\n";
print "  *************************************************************\n";
print "  *   YOU MUST BE ROOT! HIT CTRL+C NOW TO TERMINATE INSTALL!  *\n";
print "  *************************************************************\n\n";
print "  Press Enter to continue with the installation...\n\n";
<STDIN>;

###############################
### Get install prefix
###############################
system "clear";
print "\n  ========================\n";
print "  ==>  Install Prefix  <==\n";
print "  ========================\n\n";
print "  What prefix would you like to use for installing cdr? The\n";
print "  default is to use /usr/local, however, you may want to use\n";
print "  another prefix. Binaries will go in PREFIX/bin, support\n";
print "  programs in PREFIX/lib/cdr, documentation to PREFIX/doc\n\n";
print "  Installation Prefix? [/usr/local]: ";
$PREFIX = <STDIN>;
chomp $PREFIX;

if($PREFIX eq "") {
   $PREFIX = "/usr/local";
}

###############################
### Get Perl location
###############################
system "clear";
print "  ==========================\n";
print "  ==>  Location of Perl  <==\n";
print "  ==========================\n\n";
print "  Perl must be installed before you can use cdr. Where is the\n";
print "  Perl interpreter located? On Linux systems, this is typically\n";
print "  in /usr/bin. On commercial flavors of UNIX, it typically gets\n";
print "  dumped in /usr/local/bin or buried real deep in /opt.\n\n";
print "  Perl Location? [/usr/bin/perl]: ";
$PERL = <STDIN>;
chomp $PERL;

if($PERL eq "") {
   $PERL = "/usr/bin/perl";
}

###############################
### Compile support programs
###############################
system "clear";
print "Now compiling cdr support programs...\n\n";

# make final installation location
system "mkdir -p $PREFIX/lib/cdr";
system "mkdir -p $PREFIX/bin";

chdir "tools";

# cdparanoia
print "+------------+\n";
print "| cdparanoia |\n";
print "+------------+\n";
chdir "cdparanoia";
system "./build";
system "cp cdparanoia $PREFIX/lib/cdr";
system "chmod 0755 $PREFIX/lib/cdr/cdparanoia";
chdir "../";

# cdtool
print "+--------+\n";
print "| cdtool |\n";
print "+--------+\n";
chdir "cdtool";
system "./build";
system "cp cdown $PREFIX/lib/cdr";
system "chmod 0755 $PREFIX/lib/cdr/cdown";
chdir "../";

# dialog
print "+--------+\n";
print "| dialog |\n";
print "+--------+\n";
chdir "dialog";
system "./build";
system "cp cdialog $PREFIX/lib/cdr";
system "chmod 0755 $PREFIX/lib/cdr/cdialog";
chdir "../";

# lame
print "+------+\n";
print "| lame |\n";
print "+------+\n";
chdir "lame";
system "./build";
system "cp lame $PREFIX/lib/cdr/lame";
system "chmod 0755 $PREFIX/lib/cdr/lame";
chdir "../";

# id3ed
print "+--------+\n";
print "| id3ed |\n";
print "+--------+\n";
chdir "id3ed";
system "./build";
system "cp id3ed $PREFIX/lib/cdr/id3ed";
system "chmod 0755 $PREFIX/lib/cdr/id3ed";
chdir "../";

###############################
### Edit cdr.pl and install
###############################
system "clear";
chdir "../";
print "Now modifying cdr.pl and installing to $PREFIX/bin...\n";

open S, "cdr.pl";
open O, "> $PREFIX/bin/cdr";

$firstline = '1';
while(<S>) {
   if($firstline eq '1') {
      print O "#!$PERL\n";
      $firstline = '0';
   } else {
      if(/default binary prefix for cdr/) {
         print O "\$prefix = '$PREFIX/lib/cdr';\n";
      } else {
         chomp $_;
         print O "$_\n";
      }
   }
}

close S;
close O;

system "chmod 0755 $PREFIX/bin/cdr";

system "clear";
print "Installation of cdr 3.0 complete.\n";
print "You can run the program by typing $PREFIX/bin/cdr\n";

exit;
