#!/usr/bin/perl 

# linuxmuster-pk
# Wrapper script for pykota tasks
# Simplifiying tasks for pykota integration
# to Linux Musterloesung
# 
# License: GPLv2
# Author: Frank Schiebel <schiebel@rheinstrasse.net>
#

use strict;
use vars qw(%balance $ROCONFIG);
#
# Globals
#

my $opt_h;
my $opt_autopykota;

#
# Command line options processing
#
sub init() 
{
	use Getopt::Long;
	GetOptions( "h"     =>  \$opt_h,
		    "autopykota=s" => \$opt_autopykota
		    );
	
	usage() if $opt_h;
	conf_read();
}

#
# About message
#

sub usage()
{
	print "Usage...\n";
	exit;
}

init();

#
# conf_read
# + reads configuration file
# 
sub conf_read {
  do "/etc/linuxmuster/pykota.conf";
}
if ($opt_autopykota) {
  autopykota($opt_autopykota);
}


# 
# gets initial balance for a specified user
# on pykota account creation via autopykota
#
sub autopykota {
  my $username = @_[0];
  my $max = -1;
  my $groups = `groups $username`;
  $groups =~ s/.*:(.*)/$1/g;
  my @groups = split " ", $groups;
  foreach my $group (@groups) {
    chomp($group);
    if( ($balance{"$group"} > $max) && ($balance{"$group"} > 0) ){  
      $max = $balance{"$group"};
    }
  }
  if ($max == -1) {
    $max=$balance{"students"};
  }
  print "$max\n";
}



