#!/usr/bin/perl -w

#----------------------------------------------------------------------
# copyright (C) 2011-2012 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
#
#----------------------------------------------------------------------

package esmith;

use strict;
use Errno;
use esmith::ConfigDB;
use esmith::AccountsDB;
use User::pwent;
use File::Copy qw(mv);
use File::Path qw(mkpath);

my $configdb = esmith::ConfigDB->open_ro or
    die "Could not open configuration db\n";
my $accountsdb = esmith::AccountsDB->open_ro or
    die "Could not open accounts db\n";

my $domain = $configdb->get('DomainName')->value();
my $l = $configdb->get('ldap') or die 'Error reading ldap configuration';

my $defCity = $l->prop('defaultCity') || '';
my $defComp = $l->prop('defaultCompany') || '';
my $defDep = $l->prop('defaultDepartment') || '';
my $defTel = $l->prop('defaultPhoneNumber') || '';
my $defStreet = $l->prop('defaultStreet') || '';

my $event = shift;
my $userName = shift;
my @users;

if (defined $userName){
    my $rec = $accountsdb->get($userName);
    die 
        "Account $userName is not a user account; signature generation failed.\n"
        unless $userName eq 'admin' || ($rec && $rec->prop('type') eq "user");
    @users = ($rec);
}
else{
    @users = ( $accountsdb->users, $accountsdb->get('admin') );
}

if (-d "/home/e-smith/files/shares/tools/files/templates_signatures"){
    mv ("/home/e-smith/files/shares/tools/files/templates_signatures/",
          "/home/e-smith/files/shares/tools/files/signatures/templates/");
    rmdir "/home/e-smith/files/shares/tools/files/templates_signatures";
}

foreach my $user (@users){
    $userName = $user->key;

    setpwent();
    my $home = getpwnam($userName)->dir;
    my $dir = '/home/e-smith/files/shares/tools/files/signatures/' . $userName;

    # Migrate frmo previous path
    if ( -d "$home/home/signature" && !-d $dir ){
        mv ("$home/home/signature", $dir);
    }
    if (!-d "$dir"){
        mkpath "$dir";
    }

    my $first = $user->prop('FirstName') || '';
    my $last = $user->prop('LastName') || '';
    my $mail = $user->prop('PreferredEmail') || "$userName\@$domain";
    my $tel = $user->prop('Phone') || '';
    my $mob = $user->prop('Mobile') || '';
    my $fax = $user->prop('Fax') || '';
    my $func = $user->prop('Function1') || '';
    my $func2 = $user->prop('Function2') || '';
    my $func3 = $user->prop('Function3') || '';
    my $func4 = $user->prop('Function4') || '';
    my $comp = $user->prop('Company') || '';
    my $dep = $user->prop('Dept') || '';
    my $postalcode = $user->prop('PostalCode') || '';
    my $street = $user->prop('Street') || '';
    my $city = $user->prop('City') || '';
    my $url = $user->prop('Url') || '';
    $url =~ s/^https?:\/\///;
    $url =~ s/\/$//;
    my $template = $user->prop('SignatureTemplate') || "email";

    my $addr = "$street $postalcode $city";

    foreach my $ext (qw/txt html/){
        open(R, '<', "/home/e-smith/files/shares/tools/files/signatures/templates/$template.$ext") ||
            next;
        open(W, '>', "$dir/email.$ext") || die "Error opening output file $dir/email.$ext\n";
        my $src = '';
        $src .= $_ foreach (<R>);

        # Delete if value is empty
        $src =~ s/__START_NOM__.*__END_NOM__//smg if ($last eq '');
        $src =~ s/__START_PRENOM__.*__END_PRENOM__//smg if ($first eq '');
        $src =~ s/__START_EMAIL__.*__END_EMAIL__//smg if ($mail eq '');
        $src =~ s/__START_TEL__.*__END_TEL__//smg if ($tel eq '');
        $src =~ s/__START_MOBILE__.*__END_MOBILE__//smg if ($mob eq '');
        $src =~ s/__START_FAX__.*__END_FAX__//smg if ($fax eq '');
        $src =~ s/__START_FONCTION__.*__END_FONCTION__//smg if ($func eq '');
        $src =~ s/__START_FONCTION2__.*__END_FONCTION2__//smg if ($func2 eq '');
        $src =~ s/__START_FONCTION3__.*__END_FONCTION3__//smg if ($func3 eq '');
        $src =~ s/__START_FONCTION4__.*__END_FONCTION4__//smg if ($func4 eq '');
        $src =~ s/__START_ENTREPRISE__.*__END_ENTREPRISE__//smg if ($comp eq '');
        $src =~ s/__START_CODE_POSTAL__.*__END_CODE_POSTAL__//smg if ($postalcode eq '');
        $src =~ s/__START_RUE__.*__END_RUE__//smg if ($street eq '');
        $src =~ s/__START_VILLE__.*__END_VILLE__//smg if ($city eq '');
        $src =~ s/__START_ADRESSE__.*__END_ADRESSE__//smg if ($addr eq '');
        $src =~ s/__START_URL__.*__END_URL__//smg if ($url eq '');

        $src =~ s/__NOM__/$last/g;
        $src =~ s/__PRENOM__/$first/g;
        $src =~ s/__EMAIL__/$mail/g;
        $src =~ s/__TEL__/$tel/g;
        $src =~ s/__MOBILE__/$mob/g;
        $src =~ s/__FAX__/$fax/g;
        $src =~ s/__FONCTION__/$func/g;
        $src =~ s/__FONCTION2__/$func2/g;
        $src =~ s/__FONCTION3__/$func3/g;
        $src =~ s/__FONCTION4__/$func4/g;
        $src =~ s/__SERVICE__/$dep/g;
        $src =~ s/__ENTREPRISE__/$comp/g;
        $src =~ s/__ADRESSE__/$addr/g;
        $src =~ s/__CODE_POSTAL__/$postalcode/g;
        $src =~ s/__RUE__/$street/g;
        $src =~ s/__VILLE__/$city/g;
        $src =~ s/__URL__/$url/g;

        # Now remove any remaining __START_ and __END_ tags
        $src =~ s/__(START|END)_\w+__//g;
        print W $src;

        close R;
        close W;
    }
}

die "Failed to reset permissions on tools share"
    unless ( system("/sbin/e-smith/signal-event", "share-modify-files", "tools") == 0 );

exit (0);

