#!/usr/bin/perl -CA
# combine_ssh_known_hosts v1.0.1  (c) 18.1.2019 by Andreas Ley  (u) 27.1.2020
# Combine known hosts entries with the same key into a single line

use strict;
#no strict 'vars';
use warnings;
no warnings 'uninitialized';
use sigtrap;
# No diagnostics on F5, so only enable this while developing
#use diagnostics;

# The script itself may use utf-8 encoded identifiers and literals
use utf8;
# Latin-1 codepoints are considered characters
use feature 'unicode_strings';
#use locale;
# Enable UTF-8 encoding for all files (but not already open handles)
use open ':encoding(utf8)';

use Getopt::Long;
use File::Basename;
#use File::Spec::Functions;

use Term::Cap;
use Data::Dumper;
$Data::Dumper::Indent = 1;
$Data::Dumper::Terse = 1;
$Data::Dumper::Sortkeys = 1;
$Data::Dumper::Deepcopy = 1;
#warn Data::Dumper->Dump([\%hash],['*']) if ($debug{'file'}>0);
#warn Data::Dumper->new([\%hash],['*'])->Indent(0)->Dump if ($debug{'file'}>0);

sub usage
{
	my $image = $0;
	$image =~ s!.*/!!;
	print  STDERR  "Usage: $image [options] filename [...]\n";
	print  STDERR  "-v, --verbose	Verbose mode\n";
	exit(1);
}

my %opt;
@_ = @ARGV;
$Getopt::Long::ignorecase = 0;
GetOptions (\%opt,'debug|D:s','help|h','trace|x','verbose|v+','dry-run|n');
exec($^X,'-d:Trace',$0,@_) if (defined($opt{'trace'}) && !defined($Devel::Trace::TRACE));

&usage() if (defined($opt{'help'}) || !@ARGV);

if (defined($opt{'verbose'}) || defined($opt{'debug'})) {
	$|=1;
	select((select(STDERR),$|=1)[0]);
}

my (%debug,$on,$off);

sub debug
{
	my $file = shift;
	my $line = shift;
	my $prefix = defined($debug{'path'})?$file:defined($debug{'file'})?basename($file):'';
	if (defined($debug{'line'})) {
		$prefix .= ':' if (length($prefix));
		$prefix .= $line;
	}
	$prefix .= '  ' if (length($prefix));
	warn $prefix.(scalar(@_)>1 ? join(', ',map("\"$_\"",@_))."\n" : "@_\n");
}
$debug{undef} = \&debug;

if (defined($opt{'debug'})) {
	for (split(',',$opt{'debug'})) {
		if (/=/) {
			$debug{$`} = $';
		}
		else {
			$debug{$_} = 1;
		}
	}
	&debug(__FILE__,__LINE__,'%debug = '.Data::Dumper->Dump([\%debug],['*debug'])) if ($debug{'debug'}>0);
	my $term = Tgetent Term::Cap { 'OSPEED'=>9600 };
	$on = $term->Tputs('md');
	$off = $term->Tputs('me');
}

# Enable UTF-8 encoding for already open handles
# ":utf8" would enable perl native coding which happens to be UTF-8 only on ASCII platforms
binmode(STDIN,':encoding(utf8)');
binmode(STDOUT,':encoding(utf8)');
binmode(STDERR,':encoding(utf8)') unless (defined($Devel::Trace::TRACE));

for my $file (@ARGV) {
	my %key;
	open(FILE,'<',$file) or die "Can't read $file: $!\n";
	while (<FILE>) {
		chomp;
		&debug(__FILE__,__LINE__,$.,$_) if ($debug{'read'}>0);
		if (/\s+/) {
			for (split(',',$`)) {
				$key{$'}{$_} = 1;
			}
		}
		else {
			die "Can't parse line $.: $_\n";
		}
	}
	close(FILE);

	# FIXME: Write to tmp file and mv
	open(FILE,'>',$file) or die "Can't write to $file: $!\n";
	for my $key (sort keys %key) {
		print FILE join(',',sort keys %{$key{$key}}),' ',$key,"\n";
	}
	close(FILE);
}
