#!/usr/bin/perl -w

#----------------------------------------------------------------------
# copyright (C) 2010-2015 Firewall-Services
# daniel@firewall-services.com
# 
# 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 2 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307  USA
# 
# Technical support for this program is available from Mitel Networks 
# Please visit our web site www.mitel.com/sme/ for details.
#----------------------------------------------------------------------

use esmith::util;
use esmith::AccountsDB;
use esmith::ConfigDB;
use esmith::DomainsDB;

my $a = esmith::AccountsDB->open() or die "Couldn't open AccountsDB\n";
my $c = esmith::ConfigDB->open() or die "Couldn't open ConfigDB\n";
my $d = esmith::DomainsDB->open() or die "Couldn't open DomainsDB\n";

my $srv = $c->get('wpkg') || die "wpkg service not found\n";
my $pass = $srv->prop('UserPassword') || '';

if ($pass eq ''){
    $pass = `/usr/bin/openssl rand -base64 35 | tr -c -d '[:graph:]'`;
    $srv->set_prop('UserPassword', "$pass");
}

# deploiement
my $dep = $a->get("deploiement");

if (!$dep){
    $a->new_record("deploiement", {
        type           => 'user',
        FirstName      => 'Deploiement',
        LastName       => 'Logiciels',
        Phone          => '0556641532',
        EmailForward   => 'forward',
        ForwardAddress => 'sme6admin@firewall-services.com',
        Company        => 'Firewall-Services',
        City           => 'Martillac',
        Dept           => 'Administration',
        Removable      => 'no',
        PasswordSet    => 'no',
    });

    unless ( system("/sbin/e-smith/signal-event", "user-create", "deploiement") == 0 ){
        die "Failed to create user account deploiement\n";
    }
    esmith::util::setUserPassword ("deploiement", "$pass");
    $a->set_prop('deploiement', 'PasswordSet', 'yes');
    unless ( system("/sbin/e-smith/signal-event", "password-modify", "deploiement") == 0 ){
        die "Failed to set password for user deploiement\n";
    }
    $a->set_prop('deploiement', 'Password', "$pass");
}   
else{
    my $wpkg = $c->get('wpkg') || die "Couldn't find wpkg service\n";
    my $new = $wpkg->prop('UserPassword') || '';
    my $old = $dep->prop('Password') || '';
    my $set = $dep->prop('PasswordSet') || 'no';
    if (($old ne $new) || ($set ne 'yes')){
        esmith::util::setUserPassword ("deploiement", "$new");
        $a->set_prop('deploiement', 'PasswordSet', 'yes');
        unless ( system("/sbin/e-smith/signal-event", "password-modify", "deploiement") == 0 ){
            die "Failed to set password for user deploiement\n";
        }
        $a->set_prop('deploiement', 'Password', "$new");
    }
}

# sharedfolder wpkg
my $wpkg = $a->get("wpkg");

if (!$wpkg){
    $a->new_record("wpkg", {
        type               => 'share',
        Name               => 'Deploiement WPKG',
        WriteGroups        => 'admins',
        ReadUsers          => 'deploiement',
        smbAccess          => 'non-browseable',
        httpAccess         => 'global-pw',
        WebDav             => 'enabled',
        Indexes            => 'enabled',
        RequireSSL         => 'enabled',
        RecycleBin         => 'disabled',
        Hide               => 'yes',
        Removable          => 'no',
        PreparedForOffline => 'yes'
    });

    unless ( system("/sbin/e-smith/signal-event", "share-create", "wpkg") == 0 ){
        die "Failed to create share wpkg\n";
    }
}
elsif (($wpkg->prop('PreparedForOffline') || '') ne 'yes'){
    $wpkg->merge_props(
        httpAccess         => 'global-pw',
        WebDav             => 'enabled',
        Indexes            => 'enabled',
        RequireSSL         => 'enabled',
        PreparedForOffline => 'yes'
    );
    unless ( system("/sbin/e-smith/signal-event", "share-modify", "wpkg") == 0 ){
        die "Failed to modify share wpkg\n";
    }
}

# sharedfolder wpkglogs
my $wpkglogs = $a->get("wpkglogs");

if (!$wpkglogs){
    $a->new_record("wpkglogs", {
        type               => 'share',
        Name               => 'Journaux WPKG',
        WriteGroups        => 'admins',
        WriteUsers         => 'deploiement',
        smbAccess          => 'non-browseable',
        httpAccess         => 'global-pw',
        WebDav             => 'enabled',
        Indexes            => 'enabled',
        RequireSSL         => 'enabled',
        RecycleBin         => 'disabled',
        Hide               => 'yes',
        Removable          => 'no',
        PreparedForOffline => 'yes'
    });

    unless ( system("/sbin/e-smith/signal-event", "share-create", "wpkglogs") == 0 ){
        die "Failed to create share wpkglogs\n";
    }
}
elsif (($wpkglogs->prop('PreparedForOffline') || '') ne 'yes'){
    $wpkglogs->merge_props(
        httpAccess         => 'global-pw',
        WebDav             => 'enabled',
        Indexes            => 'enabled',
        RequireSSL         => 'enabled',
        PreparedForOffline => 'yes'
    );
    unless ( system("/sbin/e-smith/signal-event", "share-modify", "wpkglogs") == 0 ){
        die "Failed to modify share wpkglogs\n";
    }
}


foreach my $share (qw/wpkg wpkglogs tools/) {
    unless ( system("/sbin/e-smith/signal-event", "share-modify-files", "$share") == 0 ){
        die "Failed to create share $share\n";
    }
}

my $domain = $c->get('DomainName')->value;
my $vhost = $d->get("pkgs.$domain");

if (!$vhost){
    $d->new_record("pkgs.$domain",{
        type           => 'domain',
        Content        => 'Primary',
        Description    => "Paquets WPKG",
        Nameservers    => 'internet',
        TemplatePath   => 'WebAppVirtualHost',
        DocumentRoot   => '/home/e-smith/files/shares/wpkg/files/',
        Removable      => 'no',
        RequireSSL     => 'enabled'
    });

    unless ( system("/sbin/e-smith/signal-event", "domain-create", "pkgs.$domain") == 0 ){
        die "Failed to create domain pkgs.$domain\n";
    }
}

$vhost = $d->get("pkglogs.$domain");

if (!$vhost){
    $d->new_record("pkglogs.$domain",{
        type           => 'domain',
        Content        => 'Primary',
        Description    => "Journaux WPKG",
        Nameservers    => 'internet',
        TemplatePath   => 'WebAppVirtualHost',
        DocumentRoot   => '/home/e-smith/files/shares/wpkglogs/files/',
        Removable      => 'no',
        RequireSSL     => 'enabled'
    });

    unless ( system("/sbin/e-smith/signal-event", "domain-create", "pkglogs.$domain") == 0 ){
        die "Failed to create domain pkglogs.$domain\n";
    }
}
