#!/bin/env perl
use FindBin;
use lib $FindBin::Bin;
use strict;
use warnings FATAL => 'all';
use File::Basename;
use File::Temp;
use Fcntl ':flock';
use _kxLab;
my $distro = _kxLab::distro_name();
# Global variables
my $header_printed = 0;
my $rootfs_dest_dir;
my ($toolchain, $hardware, $flavour);
my $target_build_dir;
my $cleanup = $ENV{DO_CREATE_DIST_FILES} ? 0 : 1;
my ($tempfd, $tempname);
sub usage
{
print <<EOF;
Usage: update_pkgs [options] package[ package]
Options:
--destination=ROOTFS - where ROOTFS is a destination directory.
--toolchain=TOOLCHAIN - where TOOLCHAIN ia a toolchain name;
--hardware=HARDWARE - where HARDWARE ia a HARDWARE name;
--flavour=FLAVOUR - where FLAVOUR ia a FLAVOUR name.
EOF
exit;
}
# cleanpath( path )
sub cleanpath
{
my $path = shift;
$path =~ s!/{2,}!/!g;
$path =~ s!^.*dist/!!;
return $path;
}
# rootfs( rootfs_dest_dir, pkg )
sub rootfs
{
my $dest_dir = cleanpath( shift );
my $target = shift;
my $group = dirname( $target ); # extract GROUP if present:
$group =~ s!$target_build_dir/!!;
my $file = basename( $target, ".txz" );
print $tempfd "$dest_dir/var/log/$distro/packages/$group/$file\n";
}
# update( updatepkg, rootfs_dest_dir, verbose, targets )
sub update
{
my $updatepkg = shift;
my $rootfs_dest_dir = shift;
my $verbose = shift;
my $targets = shift;
foreach my $target ( @{$targets} )
{
my $pkg = basename( $target );
my $cdir = dirname( $target );
if( !$header_printed )
{
print "\n======= Update packages =======\n";
$header_printed = 1;
}
print "Update $target ...\n" if ( $verbose );
_kxLab::system( "mkdir -p $rootfs_dest_dir" );
_kxLab::system( "cd $cdir; $updatepkg --ignore-chrefs-errors --root=$rootfs_dest_dir $pkg" );
rootfs( $rootfs_dest_dir, $target );
}
}
my @targets;
my $verbose = $ENV{VERBOSE};
my $curdir = $ENV{CWD};
my $updatepkg = $ENV{UPDATE_PACKAGE};
my $fname = "";
my $dest_fname = "";
foreach ( @ARGV )
{
if( /--destination=(\S*)/ )
{
$rootfs_dest_dir = $1;
}
elsif( /--toolchain=(\S*)/ )
{
$toolchain = $1;
}
elsif( /--hardware=(\S*)/ )
{
$hardware = $1;
}
elsif( /--flavour=(\S*)/ )
{
$flavour = $1;
}
elsif( /--help/ )
{
usage;
}
else
{
push @targets, $_;
}
}
if( ! defined $rootfs_dest_dir or $rootfs_dest_dir eq "" ) { usage; }
if( ! defined $toolchain or $toolchain eq "" ) { usage; }
if( ! defined $hardware or $hardware eq "" ) { usage; }
if( ! defined $flavour or $flavour eq "" )
{
$flavour = "";
$target_build_dir = "." . $toolchain . "/" . $hardware;
}
else
{
$target_build_dir = "." . $toolchain . "/" . $hardware . "/" . $flavour;
}
if ( $curdir )
{
$fname = "$curdir/" . $target_build_dir . "/.rootfs.XXXXXX";
$dest_fname = "$curdir/" . $target_build_dir . "/.rootfs";
}
else
{
$fname = $target_build_dir . "/.rootfs.XXXXXX";
$dest_fname = $target_build_dir . "/.rootfs";
}
($tempfd, $tempname) = File::Temp::tempfile( $fname, UNLINK => $cleanup );
#####################
# LOCK procedure:
#
#my $lock_fname = _kxLab::build_system_tmpdir() . "/." . $hardware . ".pkgtool-lock";
#open( my $lock_fh, '+>', $lock_fname ) or
# _kxLab::error( "$0: Could not open $lock_fname file: $!" );
#flock( $lock_fh, LOCK_EX ) or
# _kxLab::error( "$0: Cannot lock $lock_fname file: $!" );
update( $updatepkg, $rootfs_dest_dir, $verbose, \@targets );
#flock( $lock_fh, LOCK_UN ) or
# _kxLab::error( "$0: Cannot unlock $lock_fname file: $!" );
#close( $lock_fh );
#
# UN LOCK procedure.
#####################
# reverse file line by line:
_kxLab::system( "tac $tempname >> $dest_fname" );
_kxLab::system( "rm -f $tempname" );