#!%PERL%
# Copyright (c) vhffs project and its contributors
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions 
# are met:
#
# 1. Redistributions of source code must retain the above copyright 
#   notice, this list of conditions and the following disclaimer.
#2. Redistributions in binary form must reproduce the above copyright
#   notice, this list of conditions and the following disclaimer in 
#   the documentation and/or other materials provided with the 
#   distribution.
#3. Neither the name of vhffs nor the names of its contributors 
#   may be used to endorse or promote products derived from this 
#   software without specific prior written permission.
#
#THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
#"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
#LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 
#FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 
#COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
#INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 
#BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
#LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
#CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
# POSSIBILITY OF SUCH DAMAGE.

use strict;
use utf8;

use lib '%VHFFS_LIB_DIR%';
use Vhffs::Main;
use Vhffs::User;
use Vhffs::Group;
use Vhffs::Robots::Group;
use Vhffs::Robots;



my $user;
my $group;
my $name;
my $vhffs = init Vhffs::Main;

Vhffs::Robots::lock( $vhffs , "usergroup" );

my $joins = Vhffs::Robots::Group::getall_user_group_to_create( $vhffs );

if(!ref($joins)) {
    Vhffs::Robots::vhffs_log( 'Database error while trying to add new users to groups' );
} else {
    foreach(@{$joins}) {
        $group = Vhffs::Group::get_by_gid( $vhffs , $_->[0] );
        if( defined $group ) {
            $user = Vhffs::User::get_by_uid($vhffs, $_->[1] );
    
            if( defined $user ) {
                if( Vhffs::Robots::Group::add_user( $user , $group , $vhffs ) < 0 ) {
                    Vhffs::Robots::vhffs_log( sprintf( "Error while adding user %s to group %s" , $user->get_username , $group->get_groupname ), $vhffs);
                } else {
                    Vhffs::Robots::vhffs_log( sprintf( "Added user %s to group %s" , $user->get_username , $group->get_groupname ), $vhffs);
                }			
            } else {
                Vhffs::Robots::vhffs_log( sprintf( "Cannot get user %s" , $_->[1] ), $vhffs);
            }
        } else {
            Vhffs::Robots::vhffs_log( sprintf( "Cannot get group %s" , $_->[0] ), $vhffs);
        }
    }
}



my $quits = Vhffs::Robots::Group::getall_user_group_to_delete( $vhffs );

if(!ref($quits)) {
    Vhffs::Robots::vhffs_log( 'Database error while trying to delete users from groups' );
} else {
    foreach(@{$quits}) {
        $group = Vhffs::Group::get_by_gid( $vhffs , $_->[0] );
        if( defined $group ) {
            $user = Vhffs::User::get_by_uid( $vhffs , $_->[1] );
            if( defined $user ) {
                if( Vhffs::Robots::Group::del_user( $user , $group , $vhffs ) > 0 ) {
                    Vhffs::Robots::vhffs_log( sprintf( 'Removed user %s from group %s', $user->get_username, $group->get_groupname ), $vhffs);
                } else {
                    Vhffs::Robots::vhffs_log( sprintf( 'Error while removing user %s from group %s', $user->get_username, $group->get_groupname ), $vhffs);
                }
            } else {
                Vhffs::Robots::vhffs_log( sprintf( 'Cannot get user %d', $_->[1] ), $vhffs);
            }
        } else {
            Vhffs::Robots::vhffs_log( sprintf( 'Cannot get group %d' , $->[0] ), $vhffs);
        }
    }
}

Vhffs::Robots::unlock( $vhffs , "usergroup" );

exit 0;
