#!/usr/bin/python2
# OpenChange provision script
#
# Copyright (C) Jelmer Vernooij <jelmer@openchange.org> 2008
#
# 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/>.
#

import optparse
import sys

# To allow running from the source directory
sys.path.append("python")

import samba.getopt as options
import openchange.provision as provision

parser = optparse.OptionParser("openchange_newuser [options] <username>")

sambaopts = options.SambaOptions(parser)
parser.add_option_group(sambaopts)

credopts = options.CredentialsOptions(parser)
parser.add_option_group(credopts)
parser.add_option("--firstorg", type="string", metavar="FIRSTORG",
                  help="select the OpenChange Organization Name")
parser.add_option("--firstou", type="string", metavar="FIRSTOU",
                  help="select the OpenChange Administration Group")
parser.add_option("--enable", action="store_true", metavar="ENABLE",
                  help="Enable access to OpenChange server")
parser.add_option("--disable", action="store_true", metavar="DISABLE",
                  help="Disable access to OpenChange server")
parser.add_option("--create", action="store_true", metavar="CREATE",
                  help="Create the OpenChange user account")
parser.add_option("--mailbox", action="store_true", metavar="MAILBOX",
                  help="Create the OpenChange user mailbox")
parser.add_option("--mail", type="string", metavar="MAIL",
                  help="User email address. If not specified, the email "
                       "will be samaccountname@realm")
opts, args = parser.parse_args()

if len(args) != 1:
    parser.print_usage()
    sys.exit(1)
elif not opts.create and not opts.enable and not opts.disable:
    parser.error("missing action option: --create, --enable or --disable")
elif opts.enable and opts.disable:
    parser.error("--enable and --disable options are incompatible")


username = args[0]
lp = sambaopts.get_loadparm()
creds = credopts.get_credentials(lp)
provisionnames = provision.guess_names_from_smbconf(
    lp, creds, opts.firstorg, opts.firstou)

if opts.create:
    provision.newuser(provisionnames, lp, creds, username=username, mail=opts.mail)

if opts.mailbox:
    print "Mailbox provisioning is now performed automatically at user logon"

if opts.enable:
    provision.accountcontrol(provisionnames, lp, creds, username=username, value=0)
elif opts.disable:
    provision.accountcontrol(provisionnames, lp, creds, username=username, value=2)
