#!/usr/bin/perl -T

# adduser: a utility to add users to the system

# Copyright (C) 2006-2008 Davor Ocelic <docelic@spinlocksolutions.com>
#                         SPINLOCK, http://www.spinlocksolutions.com/

use warnings;
use strict;
use lib '.'; # XXX

use Getopt::Long;

use Debian::Adduser::Init   qw/:gettext/;
use Debian::Adduser::Common qw/:reporting/;
use Debian::Adduser::Config qw/:cfg :getopt/;
use Debian::Adduser::Backend;

# Prepare for getopt call and invoke other crucial init-like tasks.
pregetopt();

# Parse program-specific options, perform sanity checks.
# NOTE: If the getopt option name is different
# than the variable you want to set, then there are three ways to go about it:
# 0) Scratch that, and make the names match, OR
# 1) Provide multiple names for the option, with the first name being the same
#    as the variable name, and second name being the new name you want to use
#    and, if needed, the third name being used for compatibility with
#    traditional adduser
# 2) Add the appropriate entry to %Debian::Adduser::Config::optmap,
#    so that the W() function knows about the mapping from option name to
#    getopt name and can set the correct variable.
# Approach 0 is mostly not possible for compatibility reasons, approach 1
# is cleaner but would require the exact same getopt definition for all
# tools within adduser (so it would involve copy-pasting). Approach 2 is best.
unless ( GetOptions (
    %Debian::Adduser::Config::getopt, # Global options
    'system'            => \&W,
    'ingroup|in-group=s'=> \&W,
    'home=s'            => \&W,
    'gecos=s'           => \&W,
    'shell=s'           => \&W,
    'literal-password|literal-passwd!' => \&W,
    'disabled-password|disabled-passwd' => sub {
        W 'ask_passwd', 0
        },
    'disabled-login'    => sub {
        W 'disabled_login', 1;
        W 'ask_passwd', 0;
        },
    'uid=i'             => \&W,
    'gid=i'             => \&W,
    'no-create-home'    => \&W,
    'add-extra-groups|add_extra_groups' => \&W,
    'extra_groups|extra-groups=s'       => sub {
        W 'extra_groups', $_[1];
        W 'add_extra_groups', 1;
        },
    'first_uid|first-uid|firstuid=i' => \&W,
    'last_uid|last-uid|lastuid=i'    => \&W,
    'first_gid|first-gid|firstgid=i' => \&W,
    'last_gid|last-gid|lastgid=i'    => \&W,
    'force-badname'     => \&W,
    'policy=s'          => \&W,
) ) {
  usage();
  exit 1;
}

# Perform option adjustments and verification, and other generally
# accepted (and even very recommended) post-getopt tasks.
postgetopt();

my $be = new Debian::Adduser::Backend;

# Run pre-checks (requirements needed before running adduser)
$be->check;

# Run the whole chain. In case we bump into a problem, then automatically
# $be->rollback will be called to undo all actions, and we'll exit.
$be->execute;

# We here, we champ!
exit 0;

# Local Variables:
# mode:cperl
# cperl-indent-level:4
# End:
# vim:set ai et sts=4 sw=4 tw=0:

__DATA__
adduser [options] <name> [to-group-name]

Options:
  --system                  - create system account
  --ingroup|in-group GROUP  - set primary existing GROUP
  --home DIR                - set home directory to DIR
  --gecos GECOS             - set gecos information to GECOS
  --shell SHELL             - set new user's shell to SHEL
  --disabled-password       - disable new user's password
  --literal-password        - use specified password string literally
  --disabled-login          - disable new user's login
  --uid UID                 - manually specify UID for new user
  --gid GID                 - manually specify GID for new user
  --first-uid UID           - first UID in autoselection range
  --last-uid UID            - last UID in autoselection range
  --first-gid GID           - first GID in autoselection range
  --last-gid GID            - last GID in autoselection range
  --no-create-home          - don't create home directory
  --add-extra-groups|add_extra_groups - do add new user to extra groups
  --extra_groups|extra-groups EXTRA   - list of EXTRA groups
  --force-badname           - allow weird characters in input
