#!/usr/bin/perl -w
# $Id$
# This script (sophomorix-groupadd) is maintained by Rüdiger Beck
# It is Free Software (License GPLv3)
# If you find errors, contact the author
# jeffbeck@web.de  or  jeffbeck@gmx.de


# Bibliotheken
use strict;
use Getopt::Long;
Getopt::Long::Configure ("bundling");
use Sophomorix::SophomorixConfig;
use Sophomorix::SophomorixBase;
use Sophomorix::SophomorixAPI;
use DBI;
use Net::LDAP;

use Sophomorix::SophomorixPgLdap qw(show_modulename
                                    check_connections
                                    create_class_db_entry
                                    pg_get_group_type
                                   );


my @arguments = @ARGV;


# ===========================================================================
# Variablen
# ==========================================================================

my $help=0;
my $info=0;

# ===========================================================================
# Optionen verarbeiten
# ==========================================================================

my $unix_group="";
my $nt_group="";
my $gid_number=-1;

my $domain_group=0;
my $local_group=0;
my $room_group=0;

my $explanation="";
my $skiplock=0;

my $type=-1; # 3: domain group

# Parsen der Optionen
my $testopt=GetOptions(
           "help|h" => \$help,
           "info|i" => \$info,
           "domain" => \$domain_group,
           "local" => \$local_group,
           "room" => \$room_group,
           "unix-group=s" => \$unix_group,
           "nt-group=s" => \$nt_group,
           "gidnumber=i" => \$gid_number,
           "skiplock" => \$skiplock,
          );

# Prüfen, ob Optionen erkannt wurden, sonst Abbruch
&check_options($testopt);
&check_connections();


# --help
if ($help==1) {
   # Scriptname ermitteln
   my @list = split(/\//,$0);
   my $scriptname = pop @list;
   # Befehlsbeschreibung
   print('
sophomorix-groupadd adds a group to the sophomorix database (and ldap)

Options
  -h  / --help
  -v  / --verbose
  -vv / --verbose --verbose
  -i  / --info (unused)
  --skiplock
  --unix-group groupname
  --nt-group name
  --domain (manually added domain group))
  --local  (manually added local group)
  --room   (a group for some workstations))

Please see the sophomorix-groupadd(8) man pages for full documentation
');
   print "\n";
   exit;
}


# --info
if ($info==1){
    print "No Option --info\n";
    exit;
}



# exit if options are missing
if ($unix_group eq "" or $nt_group eq "" 
                      or ($domain_group==0 
                          and $room_group==0 
                          and $local_group==0) ){
    print "\nThe options:\n";
    print "   --unix-group name \n";
    print "   --nt-group name \n";
    print "and one of:\n";
    print "   --domain\n";
    print "   --local\n";
    print "   --room\n";
    print "are mandatory.\n\n";
    exit;
}




# set the group type
# and related stuff
if ($domain_group==1){
    $type=3;
    $explanation="Domain Group";
}
if ($local_group==1){
    $type=6;
    $explanation="Local Group";
}
if ($room_group==1){
    $type=5;
    $explanation="Room Group";
}

&log_script_start(@arguments);



# Do it !

print "Creating unix group $unix_group (gidnumber:$gid_number)\n";
print "    nt_groupname: $nt_group\n";
print "    group type  : $type ($explanation)\n";

# create postgres entry
&create_class_db_entry($unix_group,$type,$gid_number,$nt_group);


&log_script_end(@arguments);
