#!/usr/bin/perl
# dh_sysuser --- debhelper to create system users

# Copyright (C) 2016 Dmitry Bogatov <kaction@sagulo>

# Author: Dmitry Bogatov <kaction@sagulo>

# This program 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 3
# of the License, or (at your option) any later version.

# This program 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 this program. If not, see <http://www.gnu.org/licenses/>.

use 5.014;
use strict;
use Debian::Debhelper::Dh_Lib;
use File::Find;
use File::stat;
use feature 'signatures';
use feature 'switch';
no warnings 'experimental::signatures';
no warnings 'experimental::smartmatch';

init();

sub parse_options($conf, $options, $user) {
    foreach my $opt (split(/,/, $options)) {
        given ($opt) {
            when (/^home=(.*)$/)  { $conf->{home} = $1; }
            when (/^home$/)       {
                my $normal = $user;
                $normal =~ s/^_+//;         # strip leading
                $normal =~ s/_+$//;         # and trailing underscore
                $normal =~ s/^[Dd]ebian-//; # and discouraged debian- prefix
                $conf->{home} = "/var/lib/$normal";
            }
            when (/^defaults$/)   { "do nothing"; }
            default               { error("unknown option `$opt'"); }
        }
    }
}

foreach my $pkg (@{$dh{DOPACKAGES}}) {
    my @entries = ();
    if (@ARGV) {
        while (@ARGV) {
            (my $user, my $opt) = splice(@ARGV, 0, 2);
            push @entries, [$user, $opt];
        }
    } elsif (my $cfg = pkgfile($pkg, 'sysuser')) {
        @entries = filedoublearray($cfg);
    };
    foreach my $entry (@entries) {
        (my $user, my $opts) = @$entry;
        $opts ||= 'defaults';
        my %conf = (home => '/nonexistent');
        parse_options(\%conf, $opts, $user);
        foreach my $script (qw/prerm postinst/) {
            autoscript($pkg, $script, "$script-sysuser",
                       sub { s/%HOME%/$conf{home}/;
                             s/%PACKAGE%/$pkg/;
                             s/%USERNAME%/$user/;});
        }
    }
    addsubstvar($pkg, 'misc:Depends', 'adduser');
    # every time maintainer script changes, minor version must be bumped.
    addsubstvar($pkg, 'misc:Depends', 'sysuser-helper', '<< 1.4');
}

# PROMISE: DH NOOP WITHOUT sysuser
=head1 NAME

dh_sysuser - manage system users, required for package operation

=head1 SYNOPSIS

B<dh_sysuser> [S<I<debhelper options>>] [I<username> I<options>] ...

=head1 DESCRIPTION

B<dh_sysuser> is debhelper addon, that provide simple and uniform way
of creating and removing system users, required for package operation
(for example, to run with dropped privileges).

B<dh_sysuser> read it's arguments from command line and file
F<debian/I<package>.F<sysuser>> in pairs, first one being an username
and second one is options. Options are comma-separated words, like
options to mount. Following options are supported (unsupported option
is error):

=over

=item I<home>=F</path/to/home/directory>

This option requests creation of home directory. By default, no home
directory is created.

=item I<home>

This option request creation of home directory somewhere under
F</var/lib>. Probably, you should use this form over explicit one,
described above.

=item I<defaults>

If you do not need any other options, put this one.

=back

B<dh_sysuser> ensures, that

=over 2

=item *
required system users are present after package installation

=item *
they are locked on package removal.

=back

=cut
