#!/usr/bin/env perl

use warnings;
use strict;
use util;
use File::Spec::Functions;
use File::Find;
use File::Basename;

my $hostname = `hostname`;
chomp $hostname;
$hostname = $ARGV[0] if @ARGV >= 1;

print "HOSTNAME = $hostname\n";

if ( !defined( $hostname ) || $hostname eq "" ) {
    print "USAGE: pdspkgs hostname [-v]\n";
    exit 1;
}

my @files;

for ( getkeys( $hostname ) ) {
    my $prefix = catfile( selfdir(), "conf", $_ );

    find( sub {
            my $name = $File::Find::name;
            my $target = $name =~ s/^$prefix//r;
            my $targetdir = dirname( $target );
            my $targetname = basename( $target );
            return unless -f $name;

            print "$target -> $name\n";
            if ( @ARGV >= 1 && $ARGV[0] eq "diff" ) {
                system_( "diff $name $target" );
            } else {
                system_( "mkdir -p $targetdir" );
                system_( "cp -f $name $target" );
            }
        }, $prefix ) if -d $prefix;
}
