The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.
#!/usr/bin/perl
use warnings;
use strict;

our $VERSION = 'v1.4.5';

use FindBin;
use lib "$FindBin::Bin/../lib/perl5";
use Narada::Config qw( get_config set_config );
use File::Temp qw( tempfile );
use Cwd qw( cwd );

main(@ARGV) unless caller;  ## no critic (ProhibitPostfixControls)

sub err {   ## no critic (ProhibitBuiltinHomonyms)
    die "narada-setup-qmail: @_\n";
}

sub main {
    die "Usage: narada-setup-qmail [--clean]\n"
        if (@_ >  1)
        || (@_ == 1 && $_[0] ne '--clean');
    my $clean = @_ && $_[0] eq '--clean';

    if ($clean) {
        for my $file (ls_qmail()) {
            unlink $file                        or err "unlink($file): $!";
        }
    }
    else {
        setup_qmail();
    }

    return;
}

sub setup_qmail {
    my $cwd = cwd();

    for my $file (ls('config/qmail')) {
        # read from config/qmail/
        my $data = get_config("qmail/$file");
        $data =~ s/^[|]/|cd \Q$cwd\E || exit(100); /gmxs;
        # write to var/qmail/
        my $path = "$cwd/var/qmail/$file";
        replacefile($path, $data);
        # symlink at ~/.qmail-
        my $qmail = "$ENV{HOME}/.qmail-$file";
        if (! -e $qmail) {
            symlink $path, $qmail               or err "symlink($path,$qmail): $!";
        }
        elsif (! (-l $qmail && _readlink($qmail) eq $path)) {
            err "conflict detected on $qmail";
        }
    }

    # cleanup
    for my $file (grep {! -f "config/qmail/$_"} ls('var/qmail')) {
        unlink "var/qmail/$file"                or err "unlink(var/qmail/$file): $!";
    }
    for my $file (grep {! -f $_} ls_qmail()) {
        unlink $file                            or err "unlink($file): $!";
    }

    return;
}

sub ls_qmail {
    my $cwd = cwd();
    my @files =
        grep {-l $_ && _readlink($_) =~ m{\A\Q$cwd\E/}xms}
        map {"$ENV{HOME}/$_"}
        grep { m/\A[.]qmail-/xms }
        ls($ENV{HOME});
    return @files;
}

sub replacefile {
    my ($file, $data) = @_;
    (my $dir = $file) =~ s{/[^/]+\z}{}xms;
    my ($fh, $temp) = tempfile(DIR => $dir);
    print {$fh} $data;
    close $fh                                   or err "close($temp): $!";
    rename $temp, $file                         or err "rename($temp,$file): $!";
    return;
}

sub ls {
    my ($dir) = @_;
    opendir my $d, $dir                         or err "opendir($dir): $!";
    my @files = grep { $_ ne q{.} && $_ ne q{..} } readdir $d;
    closedir $d                                 or err "closedir($dir): $!";
    return @files;
}

sub _readlink {
    my ($link) = @_;
    my $path = readlink $link                   or err "readlink($link): $!";
    return $path;
}


1; # Magic true value required at end of module
__END__

=encoding utf8

=head1 NAME

narada-setup-qmail - install/remove project qmail configuration


=head1 VERSION

This document describes narada-setup-qmail version v1.4.5


=head1 USAGE

    narada-setup-qmail [--clean]


=head1 DESCRIPTION

Sync Narada project's qmail configuration with user's ~/.qmail-* files.

Script must be executed in "project root directory".

When executed with --clean option will remove all ~/.qmail-* files related to
this project.


=head1 SYNTAX OF "config/qmail/files"

Syntax of "config/qmail/files" is same as described in "man dot-qmail",
but commands in project's qmail configuration will be executed in project's
root directory instead of user's home directory. If config file have piped
command (started with "|") it will be modified to
"|cd /path/to/project || exit(100); " command will be added on-the-fly before
user command, i.e. every command line in "config/qmail/file_with_command" like:

|some_script some_opt;

will turn into line in user's dir like:

|cd /path/to/project || exit(100); some_script some_opt;


=head1 DIAGNOSTICS

=over

=item C<< conflict detected on ~/.qmail-NAME >>

This project have config/qmail/NAME, but ~/.qmail-NAME already exists and
it isn't symlink to this project's var/qmail/NAME file.

You should manually resolve this conflict by either renaming
config/qmail/NAME file in this project or removing ~/.qmail-NAME.
After that you should run narada-setup-qmail again.

=back


=head1 CONFIGURATION AND ENVIRONMENT

 read from config/qmail/*
 write to var/qmail/*
 create/delete symlinks at ~/.qmail-*


=head1 SUPPORT

=head2 Bugs / Feature Requests

Please report any bugs or feature requests through the issue tracker
at L<https://github.com/powerman/Narada/issues>.
You will be notified automatically of any progress on your issue.

=head2 Source Code

This is open source software. The code repository is available for
public review and contribution under the terms of the license.
Feel free to fork the repository and submit pull requests.

L<https://github.com/powerman/Narada>

    git clone https://github.com/powerman/Narada.git

=head2 Resources

=over

=item * MetaCPAN Search

L<https://metacpan.org/search?q=Narada>

=item * CPAN Ratings

L<http://cpanratings.perl.org/dist/Narada>

=item * AnnoCPAN: Annotated CPAN documentation

L<http://annocpan.org/dist/Narada>

=item * CPAN Testers Matrix

L<http://matrix.cpantesters.org/?dist=Narada>

=item * CPANTS: A CPAN Testing Service (Kwalitee)

L<http://cpants.cpanauthors.org/dist/Narada>

=back


=head1 AUTHOR

Alex Efros E<lt>powerman@cpan.orgE<gt>

Nick Levchenko C<< <nick-lev@ya.ru> >>


=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2008-2015 by Alex Efros E<lt>powerman@cpan.orgE<gt>.

This is free software, licensed under:

  The MIT (X11) License

=cut