#!/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

umask(0);

print <<TXT;

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

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

$tag  = "ltib";
$tdir = cwd();

while(1){
    print "Where do you want to install LTIB ? ($tdir/$tag)\n";
    chomp($line  = <STDIN>);
    ($tdir,$tag) = $line =~ m,(/[^\s]+)/([^/\s]+), if $line;
    warn("You need to supply an absolute path\n\n"), next unless $tdir && $tag;
    warn("Directory $tdir/$tag already exists\n\n"), next if -d "$tdir/$tag";
    if(! -d $tdir) {
        print "Making target directory $tdir\n";
        system("$echo mkdir -p $tdir/$tag") == 0 or next;
    }
    chdir $tdir or die "chdir $tdir";
    last;
}

print "Installing LTIB to $tdir/$tag\n";
system("set -x; $echo cvs -z3 -d:pserver:anonymous\@cvs.savannah.nongnu.org:/sources/ltib co -d $tag -P ltib") == 0 or die;

print <<TXT;

LTIB download complete, your ltib installation has been placed in 
$tdir/$tag, to complete the installation, run the following commands:

\$ cd $tdir/$tag
\$ ./ltib

TXT
exit 0;

