#!/usr/bin/perl -w

use strict;
use warnings;
use File::Lock::ParentLock 0.06;
use File::Path qw(make_path remove_tree);
use Test::Repocop::Workdir;
use Test::Repocop::CLI::Base;
our @ISA=qw/Test::Repocop::CLI/;

#------------------------------------------
my ($is_locked_by_others,$is_locked_by_us);
my $lock=0;
our @LONGOPT=(
    "lock"  => sub {$lock=1},
    "unlock"  => sub {$lock=2},
    "is-locked-by-others"  => \$is_locked_by_others,
    "is-locked-by-us"  => \$is_locked_by_us,
    );
our @LONGOPT_POD2USAGE=qw/-verbose 1 -exitval 1/;
__PACKAGE__->get_and_process_cli_options();

my $parent_pid=$ARGV[0];
unless ($parent_pid) {
    warn "first arg should be a valid PID\n";
    __PACKAGE__->usage();
    exit 1;
}

#my $locker=File::Lock::ParentLock->new();

if ($lock==1) {
    make_path($repocop_workdir);
    exit 1 if !&File::Lock::ParentLock::parentlock_lock($Test::Repocop::Workdir::lockfile,$parent_pid);
    exit 0;
} elsif ($lock==2) {
    exit 1 if !&File::Lock::ParentLock::parentlock_unlock($Test::Repocop::Workdir::lockfile,$parent_pid);
    exit 0;
} elsif ($is_locked_by_others) {
    exit 0 if &File::Lock::ParentLock::parentlock_is_locked_by_others($Test::Repocop::Workdir::lockfile,$parent_pid);
    exit 1;
} elsif ($is_locked_by_us) {
    exit 0 if &File::Lock::ParentLock::parentlock_is_locked_by_us($Test::Repocop::Workdir::lockfile,$parent_pid);
    exit 1;
} else {
    my $status=&File::Lock::ParentLock::parentlock_status_string($Test::Repocop::Workdir::lockfile,$parent_pid);
    print "$status: ",$status,"\n";
    exit 0;
}
#--------------------------------------------
__END__



=head1	NAME

repocop-cachedir-lock - locks repocop workdir directory.

=head1	SYNOPSIS

B<repocop-cachedir-lock>
[B<-l|--lock> ]
[B<-u|--unlock> ]
[B<--is-locked-by-us>|B<--is-locked-by-others>]
[B<-h|--help>]
[B<-v|--verbose>]
[B<-q|--quiet>]
[B<--workdir> I<workdir>]
I<PID>

=head1	DESCRIPTION

B<repocop-cachedir-lock> locks repocop workdir directory.
It should be run inside of a repocop wrapper script with pid I<PID>.

=head1	OPTIONS

=over

=item	B<-l,--lock>

Lock

=item	B<-u,--unlock>

Unlock

=item	B<--is-locked-by-us>

Query about existing lock. Returns true if it is locked by I<PID> or its parent.
Return false ottherwise.

=item	B<--is-locked-by-others>

Query about existing lock. Returns true if is locked by a live process not related to I<PID>.
Return false ottherwise.

=item	B<--workdir> I<dir>

Provides alternative location for the Repocop workdir. Repocop workdir
is a place where test results and packages metadata information are stored.
Default is I<~/.repocop>.

=item	B<-h, --help>

Display this help and exit.

=item	B<-v, --verbose>, B<-q, --quiet>

Verbosity level. Multiple -v increase the verbosity level, -q sets it to 0.


=back

=head1	AUTHOR

Written by Igor Vlasenko <viy@altlinux.org>.

=head1	ACKNOWLEGEMENTS

To Alexey Torbin <at@altlinux.org>, whose qa-robot package
had a strong influence on repocop.

=head1	COPYING

Copyright (c) 2008-2022 Igor Vlasenko, ALT Linux Team.

This 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.

=cut
