App-Followme

 view release on metacpan or  search on metacpan

lib/App/Followme/Initialize.pm  view on Meta::CPAN

package App::Followme::Initialize;
use 5.008005;
use strict;
use warnings;
use lib '../..';

use IO::File;
use MIME::Base64  qw(decode_base64);
use File::Spec::Functions qw(splitdir catfile);

use App::Followme::FIO;
use App::Followme::NestedText;

our $VERSION = "2.03";

require Exporter;
our @ISA = qw(Exporter);
our @EXPORT_OK = qw(initialize);

our $var = {};
use constant CMD_PREFIX => '#>>>';

#----------------------------------------------------------------------
# Initialize a new web site

sub initialize {
    my ($directory) = @_;

    chdir($directory) if defined $directory;
    my ($read, $unread) = data_readers();

    while (my ($command, $lines) = next_command($read, $unread)) {
        my @args = split(' ', $command);
        my $cmd = shift @args;

        write_error("Missing lines after command", $command)
            if $cmd eq 'copy' && @$lines == 0;

        write_error("Unexpected lines after command", $command)
            if $cmd ne 'copy' && @$lines > 0;

        if ($cmd  eq 'copy') {
            write_file($lines, @args);

        } else {
            write_error("Error in command name", $command);
        }
    }

    return;
}

#----------------------------------------------------------------------
# Copy a binary file

sub copy_binary {
    my($file, $lines, @args) = @_;
    return if -e $file;

    my $out = IO::File->new($file, 'w') or die "Couldn't write $file: $!\n";
    binmode($out);

    foreach my $line (@$lines) {
        print $out decode_base64($line);
    }

    close($out);
    return;
}

#----------------------------------------------------------------------
# Copy a configuration file

sub copy_configuration {
    my ($file, $lines, @args) = @_;
    
    my $config;
    my %old_config = nt_parse_almost_yaml_string(join('', @$lines));

    if (-e $file) {
        my $page = fio_read_page($file);

        if ($page =~ /:[ \n]/) {
            my %new_config = nt_parse_almost_yaml_string($page);
            $config = nt_merge_items(\%old_config, \%new_config);

        } else {
            my $new_file = $file;
            $new_file =~ s/\.*$/ocfg/;
            rename($file, $new_file);
            $config = \%old_config;
        }

    } else {
        $config = \%old_config;
    }

    nt_write_almost_yaml_file($file, %$config);
    return;
}

#----------------------------------------------------------------------
# Copy a text file

sub copy_text {
    my ($file, $lines, @args) = @_;
    return if -e $file;

    my $out = IO::File->new($file, 'w') or die "Couldn't write $file: $!\n";
    foreach my $line (@$lines) {
        print $out $line;
    }

    close($out);
    return;
}

#----------------------------------------------------------------------
# Check path and create directories as necessary

sub create_dirs {



( run in 0.977 second using v1.01-cache-2.11-cpan-75ffa21a3d4 )