#!/usr/bin/perl
##
##  GotoURL -- go to a particular URL
##  Copyright (c) 1997 Ralf S. Engelschall, All Rights Reserved.
##

$url = $ARGV[0];

@RC = ();
open(RC, "<", "$ENV{HOME}/.gotourlrc");
while (<RC>) {
    next if (m|^\s*#.*|);
    next if (m|^\s*$|);
    if (m|^(\S+)\s+(.+)$|) {
        push(@RC, { PAT => $1, CMD => $2 });
    }
}
close(RC);

use IPC::Open2;
open2(my $isel_out, my $isel_in, 'iselect', '-n', 'GotoURL 1.0', '-t', 'Select Client...', '-p6');

print $isel_in "\n";
print $isel_in "URL: $url\n";
print $isel_in "\n";
print $isel_in "Available clients for this URL type:\n";
print $isel_in "\n";
foreach $rc (@RC) {
    if ($url =~ m|$rc->{PAT}|) {
        $cmd = $rc->{CMD};
        $cmd =~ s|%U|$url|g;
        $txt = $rc->{CMD};
        $txt =~ s|%U|...|g;
        print $isel_in sprintf("%s <S:%s>\n", $txt, $cmd);
    }
}
print $isel_in <<'EOT';
_____________________________________________________

Help: <up>,<down> .......... browse client list
      <return>,<right> ..... select client
      q,<left> ............. quit
EOT
close($isel_in);
$client = <$isel_out>;
if ($client ne '') {
    system("$client");
}
