#!/usr/bin/perl
# URL v2.0.2  (c) 1.5.1996 by Andreas Ley  (u) 26.2.2026
# Show URL to current directory or file

use locale;
use Getopt::Std;
use Cwd 'realpath';
#use File::Spec::Functions;
use URI::Escape;

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

getopts('Dhxv') or &usage;

&usage if (defined($opt_h));

if (defined($opt_v) || defined($opt_D)) {
	$|=1;
	select((select(STDERR),$|=1)[0]);
}

if (@ARGV) {
	for (@ARGV) {
		&url($_);
	}
}
else {
	&url('.');
}

sub escape
{
	my ($uri) = @_;

	# Default set (see URI::Escape) plus slash
	return(uri_escape($uri,"^A-Za-z0-9\-\._~/"));
}

sub hostname
{
	if (!defined($hostname)) {
		$hostname = `/bin/hostname --fqdn`;
		chomp $hostname;
	}
	return($hostname);
}

sub url
{
	my ($path) = @_;

	print STDERR "&url(\"$path\")\n" if ($opt_D>1);
	$_ = realpath($path);
	$_ .= '/' if (-d);
	print STDERR "\"$path\" => \"$_\"\n" if ($opt_D>0);

	if (m!/home/(?:hard/web|sccfs/web)[^/]*/[^/-]+-web-\d+/([^/.]+(?:\.[^/.]+)+)/htdocs/!) {
		print "http://$1/",escape($'),"\n";
		print "https://$1/",escape($'),"\n" if (-s "/var/www/$1/conf/ssl/server.crt");
	}
	elsif (m!/home/(?:hard/web|sccfs/web)[^/]*/[^/-]+-web-\d+/([^/.]+(?:\.[^/.]+)+)/cgi-bin/!) {
		print "http://$1/cgi-bin/",escape($'),"\n";
		print "https://$1/cgi-bin/",escape($'),"\n" if (-s "/var/www/$1/conf/ssl/server.crt");
	}
	elsif (m!/home/sys/rzinstcomserv/install/!) {
		print "http://rzadmin.rz.uni-karlsruhe.de/usr/common/rzserv/install/",escape($'),"\n";
	}
	elsif (m!/home/sys/sccwww/!) {
		print "http://www.scc.kit.edu/scc/",escape($'),"\n";
	}
	elsif (m!/home/sys/sccwww-intern/!) {
		print "https://www.scc.kit.edu/intern/",escape($'),"\n";
	}
	elsif (m!/home/sys/rzwww-intern/!) {
		print "https://www-intern.rz.uni-karlsruhe.de/",escape($'),"\n";
	}
	elsif (m!^/home/sys/sccfs-ftp/pub/|^/home/sys/ftp/!) {
		print "ftp://ftp.scc.kit.edu/pub/",escape($'),"\n";
		print "http://ftp.scc.kit.edu/pub/",escape($'),"\n";
		print 'file://',escape($_),"\n";
	}
	#elsif (m!/home/sys/source/andy/!) {
	elsif (m!/home/ws/zm6441/source/!) {
		print "$_\n";
		print "http://andy.scc.kit.edu/source/",escape($'),"\n";
	}
	elsif (m!^/home/andy/www/!) {
		print 'http://andy.scc.kit.edu/',escape($'),"\n";
	}
	elsif (&hostname =~ /^andy\d*\./) {
		if (&hostname eq 'andy.scc.kit.edu') {
			if (m!^/var/www/!) {
				print 'http://',&hostname,'/',escape($'),"\n";
			}
			elsif (m!^/hfs/2/andy/video/ETDS/!) {
				print 'http://',&hostname,'/ETDS/',escape($'),"\n";
			}
			elsif (m!^/hfs/1/andy/ETDS/!) {
				print 'http://',&hostname,'/ETDS/',escape($'),"\n";
			}
		}
		elsif (&hostname eq 'andy1.scc.kit.edu') {
			if (m!^/home/andy/hfs2/andy/video/ETDS/!) {
				print 'http://andy.scc.kit.edu/ETDS/',escape($'),"\n";
			}
			elsif (m!^/home/andy/hfs1/andy/ETDS/!) {
				print 'http://andy.scc.kit.edu/ETDS/',escape($'),"\n";
			}
		}
		print 'file://',escape($_),"\n";
	}
	else {
		warn "No match: $_\n";
	}
}
