#!/usr/bin/perl

#
# Copyright (C) 2018 Nethesis S.r.l.
# http://www.nethesis.it - nethserver@nethesis.it
#
# This script is part of NethServer.
#
# NethServer 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 any later version.
#
# NethServer 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 NethServer.  If not, see COPYING.
#

#
# Read users and groups from the account provider
# and dump all accounts in a file with tsv format:
#
# - /var/lib/nethserver/backup/users.tsv
# - /var/lib/nethserver/backup/groups.tsv
#

use strict;
use warnings;

use JSON;
use NethServer::Password;
use esmith::ConfigDB;

our $users = decode_json(`/usr/libexec/nethserver/list-users`) or die("Can't read user list");
our $generator = NethServer::Password->new('', {length => 10});
our $default_password = $ARGV[0];
my $db = esmith::ConfigDB->open_ro() || die("Can't open config db");
our $org = $db->get_prop("OrganizationContact","Company") || 'DefaultTeam';
our $team = lc($org);
$team =~ s/\s(.*)$//;

sub create_user {
   my $user = shift;
   my $stripped = $user;
   $stripped =~ s/(@.*)//; # strip domain
   my $password = $default_password || $generator->generate()->getAscii();
  
   my $cmd = "cd /opt/mattermost; ./bin/mmctl --local user create --email $user --password '$password' --username $stripped";

   my @output = `cd /opt/mattermost; ./bin/mmctl --local user search $stripped 2>&1`;
   foreach (@output) {
       if ($_ =~ /username: $stripped/) {
           print "Skipping existing user: '$stripped'\n";
           return;
       }
   }

   print "Creating user: '$stripped' with password '$password' ... ";
   system($cmd."&>/dev/null");
   if ($? == 0) {
       print "OK\n";
       system("cd /opt/mattermost; bin/mmctl --local team add '$team' '$stripped' &>/dev/null");
   } else {
       print "FAIL (Command was: $cmd)\n";
   }

}

print "Creating default team: $team ($org) ... ";
system("cd /opt/mattermost; bin/mmctl --local team create --name '$team' --display_name '$org' &>/dev/null");
# hide error if team already exists
print "OK\n";

foreach (keys(%$users)) {
    my $key = $_;
    if ($users->{$_}->{'locked'}) {
        print "Skipping locked user: '$key'\n";
    } else {
        create_user($_);
    }
}
