#!/usr/bin/perl

use strict;
use warnings;

use RPM::Source::Editor;
use RPM::Source::SpecConst;

my $verbose;
if ($ARGV[0] and $ARGV[0] eq '-v') {
    $verbose=1;
    shift @ARGV;
}
my $specfile=$ARGV[0];

die "first arg is not a file" unless defined $specfile;
die "$ARGV[0]: not a file" unless -f $specfile;

my $spec=RPM::Source::Editor->new(SPECFILE=>$specfile);
my $exit_code=3;
foreach my $sec ($spec->get_sections()) {
    last unless $exit_code;
    my $sectype=$sec->get_type;
    #warn "section: ", $sectype, "\n" if $verbose;
    next if ! $RPM::Source::SpecConst::SCRIPT_SECTION{$sectype};
    $sec->visit_body(
	sub{
	    #warn "visiting: ", $_ if $verbose;
	    return if /^\s*(?:[#]|cp|grep|sed|pushd)/;
	    #(?<!\%)
	    if (m!(?:^|\s|;|/)useradd\s.*-n!) {
		$exit_code=0;
		#warn "MATCHED: ", $_ if $verbose;
	    }
	});
}

exit $exit_code;
