#!/usr/bin/perl
use strict; use warnings; # no warnings 'once';

=pod

=head1 Description

Deploys the code, packaging it for a release.

=cut

use lib '/home/mike/project/textbender/code_recombinant/matrix/codeset.repo', '/usr/local/lib';
require File::Copy;
use File::Path ();
require textbender::recombinant::_::Function;
#require Zelea::File;
require Zelea::Gentoo::ChangeLog;


    my $_project_directory = '/home/mike/project/Screenpager';
    my $_build_directory = '/home/mike/var/build/screenpager'; File::Path::mkpath $_build_directory or die $!;

    my $_version; my $_revision; ( $_version, $_revision ) =
        Zelea::Gentoo::ChangeLog::extract_latest_release( "$_project_directory/ChangeLog" );
    defined $_version or die;
    defined $_revision or $_revision = '';

   # Create tarball.
   # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    {
        my $file = textbender::recombinant::_::Function::join_clean
            ( '-', "$_project_directory/release/screenpager", $_version )
            . '.tar.gz';
        -f $file and die "cannot overwrite existing release: $file";

        my $image_directory = $_build_directory . '/image';
        -e $image_directory and File::Path::rmtree( $image_directory );
        File::Path::mkpath( $image_directory ) or die;
        File::Path::mkpath( $image_directory . '/etc/xdg/screenpager' ) or die;
#       File::Path::mkpath( $image_directory . '/var/cache/screenpager' ) or die;
#               chmod 0777, $image_directory . '/var/cache/screenpager' or die;

       # Config.pm
       # ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` `
        open( SOURCE,                 '</etc/xdg/screenpager/Config_1-example.pm' ) or die $!;
        open( TARGET, ">$image_directory/etc/xdg/screenpager/Config_1-example.pm" ) or die $!;
        {
            my $invalidated = 0; # thus far
#           my $font_uncommented = 0;
#           my $font_comment_count = 0;
            while( <SOURCE> ) # copy to target
            {
                if( !$invalidated && s/^( *my \$valid =) 1(; *)$/$1 0$2/ )
                {
                    $invalidated = 1;
                }
#               if( !$font_uncommented && s`^( *)#( font => '12x24',)$`$1 $2` )
#               {
#                   $font_uncommented = 1;
#               }
#               else
#               {
#                   s`^( *) ( font => ')`$1#$2` and ++$font_comment_count;
#               }
                print TARGET;
            }
            close SOURCE;
            close TARGET;

            $invalidated or die;
#           $invalidated && $font_uncommented or die;
#           $font_comment_count == 1
#               or die 'unexpected count of font line in Config.pm: '
#                   . $font_comment_count;
        }

       # Pref.pm
       # ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` `
        File::Copy::copy(      '/etc/xdg/screenpager/Pref_0-example.pm',
            $image_directory . '/etc/xdg/screenpager/' ) or die $!;

       # Script
       # ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` `
        {
            my $image_bin_directory = $image_directory . '/usr/bin';
            File::Path::mkpath( $image_bin_directory ) or die;
            File::Copy::copy( $_project_directory . '/screenpager',
                "$image_bin_directory/" ) or die;
            chmod 0755, $image_bin_directory . '/screenpager';
        }

       # ` ` `
        my $command = "tar --create --file=$file --group=0 --gzip --owner=0 --verbose"
            . " --directory=$image_directory .";
        system $command and die 'unable to execute: ' . $command;

#       Zelea::File::ensure_no_directory( $image_directory );
      # -e $image_directory and File::Path::rmtree( $image_directory );
    }

   # Copy ebuild. Normally this only creates revision 0;
   # subsequent revisions are manually created, and all of them use the same tarball.
   # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    {
        my $file = textbender::recombinant::_::Function::join_clean
            ( '-', "$_project_directory/release/screenpager", $_version, $_revision )
            . '.ebuild';
        -f $file and die "cannot overwrite existing release: $file";

        File::Copy::copy( $_project_directory.'/ebuild', $file ) or die $!;
    }

   # - - -
#   print "To test: [need more thorough test...]\n";
#   print "    = extract script (as root) and copy to /usr/bin\n";
#   print "    > screenpager --version\n";
#   print "    > screenpager --man\n";
    exit;