#!/usr/bin/perl -w
eval 'LANG=C exec perl -w -S $0 ${1+"$@"}'
    if $running_under_some_shell;
$running_under_some_shell = 0;

######################################################################
#
# Copyright  Freescale Semiconductor, Inc. 2004-2007. All rights reserved.
#
# Stuart Hughes, stuarth@freescale.com, 14th June 2005
#   
# This file is part of LTIB.
#
# LTIB is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# LTIB is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LTIB; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
#
######################################################################
use FindBin;
use Cwd;
$isodir = $FindBin::Bin;
$echo   = '';

die <<TXT if $> == 0;

You should not be root when installing LTIB

TXT

warn <<TXT unless -f "$isodir/EULA";

WARNING: cannot find $isodir/EULA

 If this is a release from Freesale, please report this:

   * go to http://www.freescale.com
   * click on "Support / Technical support"
   * click on "Submit a Service Request"
   * register to get a user name and password.
   * login in with your user name and password
   * on the "New Service Request" page:
      * category = Technical Request
      * topic = Linux BSP
      * Click on "Continue"
   * fill out the information for the service request
   * click on the "Submit" button at the bottom of the page.

TXT

umask(0022);

print <<TXT;

You are about to install the LTIB (GNU/Linux Target Image Builder)

Before installing LTIB, you must read and accept the EULA 
(End User License Agreement) which will be presented next.

Do you want to continue ? Y|n
TXT
$_ = <STDIN>;
die "Installation aborted, goodbye\n" if /^n/i;

print <<TXT;

Hit enter to continue:
TXT
system("cat $isodir/EULA | more");

do {
print <<TXT;

I have read and accept the EULA (yes|no):
TXT
$_ = <STDIN>;
} while (m,^\s*$,);

die "Installation aborted, goodbye\n" unless /^\s*yes/i;

$tag  = get_release_tag($isodir);
$tdir = cwd();

print <<TXT;

The LTIB files are extracted from a tar file which includes the
prefix $tag.  After installation you will find LTIB in:
$tdir/$tag
TXT

while(1){
    print "\nWhere do you want to install LTIB ? ($tdir)\n";
    chomp($line = <STDIN>);
    $tdir = $line if $line;
    warn("Cannot install into $isodir\n"), next if $tdir eq $isodir;
    warn("Directory $tdir/$tag already exists\n"), next if -d "$tdir/$tag";
    print "Making target directory $tdir/$tag\n";
    system("$echo mkdir -p $tdir/$tag") == 0 or next;
    last;
}

print "Installing LTIB to $tdir/$tag\n";
system("$echo tar -C $tdir -zxvf $isodir/ltib.tar.gz") == 0 or die;

$tpkgsdir = "$tdir/$tag/pkgs";
if(! -d $tpkgsdir) {
    mkdir($tpkgsdir) or die("mkdir $tpkgsdir: $!");
    chmod(0755, $tpkgsdir);
    print "\nCopying packages to $tpkgsdir\n";
    foreach $path (glob("$isodir/pkgs/*")) {
        ($fn) = $path =~ m,/([^/]+)$,;
        next if -f "/opt/freescale/pkgs/$fn";
        next if -f "/opt/ltib/pkgs/$fn";
        system("$echo cp -d $path $tpkgsdir/$fn") == 0 or die;
    }
}

print <<TXT;

Installation complete, your ltib installation has been placed in 
$tdir/$tag, to complete the installation:

cd $tdir/$tag
./ltib

TXT
exit 0;


sub get_release_tag
{
   my ($dir) = @_;
   open(TAR, "tar -ztf $dir/ltib.tar.gz |")
                         or die("cannot run tar -ztf $dir/ltib.tar.gz: $!\n");
   $_ = <TAR>;
   close(TAR);
   my ($rtag) = m,([^./]+),;
   die("Cannot extract release tag") unless $rtag;
   return $rtag;
}
