#!/usr/bin/perl -w
###########################################
# conprep -- Prepare Docker containers
# Mike Schilli, 2014 (m@perlmeister.com)
###########################################
use strict;
use Sysadm::Install qw(:all);
use Log::Log4perl qw( :easy );
use File::Basename;
use Getopt::Std;

Log::Log4perl->easy_init($DEBUG);

for my $container_path ( <containers/*> ) {
  cd $container_path;
  my $container = basename $container_path;

  tap { raise_error => 1 },
    "docker", "build", "--no-cache", ".";

  my( $stdout, $stderr, $rc ) = 
    tap "docker", "ps", "-l";

  my @lines = split /\n/, $stdout;

  if( $lines[1] =~ /^(\w+)/ ) {
      my $container_id = $1;
      tap { raise_error => 1 }, 
        "docker", "commit", $container_id,
        "$container-perltest";
  } else {
      die "unexpected format: @lines";
  }

  cdback;
}
