#!/bin/env perl
use FindBin;
use lib $FindBin::Bin;
use strict;
use warnings FATAL => 'all';
use File::Basename;
use Fcntl ':flock';
use _kxLab;
my $distro = _kxLab::distro_name();
my @arguments;
my ( $base, $toolchain, $hardware, $flavour );
my $target_build_dir;
my $curdir = $ENV{CWD};
my $removepkg = $ENV{REMOVE_PACKAGE};
my $fname = "";
my $pkg = "";
my $pkg_name = "";
my $pkg_group = "";
my $pkg_dir = "";
my $rootfs_dir = "";
sub usage
{
print <<EOF;
Usage: rootfs_clean [options]
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;
}
foreach ( @ARGV )
{
if( /--destination=(\S*)/ )
{
$base = $1;
}
elsif( /--toolchain=(\S*)/ )
{
$toolchain = $1;
}
elsif( /--hardware=(\S*)/ )
{
$hardware = $1;
}
elsif( /--flavour=(\S*)/ )
{
$flavour = $1;
}
elsif( /--help/ )
{
usage;
}
}
if( ! defined $base or $base 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";
}
else
{
$fname = $target_build_dir . "/.rootfs";
}
open( F, '<', $fname ) or die "Could not open '$fname'";
while( <F> )
{
chomp;
$pkg = "$base/$_";
$pkg_group = dirname( $pkg ); # extract GROUP if present:
$pkg_group =~ s!/.+/var/log/$distro/packages/!!;
$pkg_name = basename( $pkg );
$pkg_dir = dirname( $pkg );
$rootfs_dir = $pkg_dir;
$rootfs_dir =~ s!/var/log/$distro/packages.*$!!;
#####################
# 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: $!" );
_kxLab::system( "$removepkg --ignore-chrefs-errors --root=$rootfs_dir $pkg" );
#flock( $lock_fh, LOCK_UN ) or
# _kxLab::error( "$0: Cannot unlock $lock_fname file: $!" );
#close( $lock_fh );
#
# UN LOCK procedure.
#####################
}