#!/usr/bin/perl -CA
# symlink v1.0  (c) 26.3.2019 by Andreas Ley  (u) 26.3.2019
# Show shortest symlink from one absolute path to another

use strict;
#no strict 'vars';
use warnings;
no warnings 'uninitialized';
use sigtrap;
#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 [...] destination\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));

my $link = pop(@ARGV);
$link = catfile($ENV{'PWD'},$link) unless ($link =~ /^\//);

for my $target (@ARGV) {
	$target = catfile($ENV{'PWD'},$target) unless ($target =~ /^\//);
	print &shortest_symlink(-d $link ? catfile($link,basename($target)) : $link, $target),"\n";
}

sub shortest_symlink
{
	&debug(__FILE__,__LINE__,'+ &shortest_symlink('.join(',',map("'$_'",@_)).')') if ($debug{'call'}>0 || $debug{'shortest_symlink'}>0);
	my ($src,$dst) = @_;

	my @src = split('/',$src);
	my @dst = split('/',$dst);
	while ($src[0] eq $dst[0]) {
		shift(@src);
		&debug(__FILE__,__LINE__,@src) if ($debug{'shortest_symlink'}>2);
		shift(@dst);
		&debug(__FILE__,__LINE__,@dst) if ($debug{'shortest_symlink'}>2);
		die "Too many levels of symbolic links\n" unless (@dst);
	}
	pop(@src);
	my $link = catfile(map('..',@src),@dst);

	&debug(__FILE__,__LINE__,'+ &shortest_symlink = '.$link) if ($debug{'return'}>1 || $debug{'shortest_symlink'}>1);
	return($link);
}
