eval 'exec perl -x $0 ${1+"$@"}' # -*-perl-*-
  if 0;
#!perl -w
#
# Different utility functions.
# Diego Zamboni, January 29, 2001.

use strict;
use vars qw(
	    $xmessage
	    $xprompt
	    $xpromptopts
	    $rox
	    $urldir
	    $bookname
	    $bookpos
	    $openurl
	    $basedir
	   );

$basedir=$0;
$basedir=~s!/[^/]+$!!;

######################################################################
# Configuration section

# commands
$xmessage="xmessage";
$xprompt="xprompt";
$xpromptopts="-tf $basedir/xprompt-translations -fn fixed -re";
$rox="rox";

# Where to store .url files
$urldir = $ENV{ROX_URLDIR} || "$ENV{HOME}/local/ROX-Filer/URLs";

# How the bookmarks panel is called, and its position
$bookname = "Bookmarks";
$bookpos = "t";

# Program to use to view URLs
$openurl = "$basedir/openurl";

# End config section
######################################################################

# Process options
foreach (@ARGV) {
  my $opt=shift @ARGV;
  unless ($opt =~ /^--/) {
    unshift @ARGV, $opt;
    last;
  }
  $opt=~s/^--//;
  if ($opt) {
    eval "do_$opt()";
    if ($@) {
      xmessage("An error occurred when executing action $opt:\n$@");
    }
    exit(0);
  }
}

# If we get here, no options were successfully parsed, so we execute the default
do_openurl();

exit(0);

sub xmessage {
  system("$xmessage @_");
}

sub xprompt {
  my @prompts=@_;
  my $prstr;
  my @repl;
  foreach (@prompts) {
    # The prompt string
    if ($_->[0]) {
      $prstr.="-p '".$_->[0]."' ";
      # The optional default reply string
      if ($_->[1]) {
	$prstr.="-r '".$_->[1]."' ";
      }
    }
  }
  if ($prstr) {
    @repl=`$xprompt $xpromptopts $prstr`;
    chomp @repl if @repl;
    foreach (@repl) {
      s/^\s+//; s/\s+$//;
    }
    if (wantarray) {
      return @repl;
    }
    else {
      return $repl[0];
    }
  }
  else {
    return (undef);
  }
}

sub xdie {
  xmessage(@_);
  exit 1;
}

# Add a URL file to $urldir
sub do_addurl {
  xdie("The directory $urldir does not exist.\nPlease create it first.")
    unless (-d $urldir);
  my ($url, $name)=xprompt(["Please enter the URL"],
			   ["Please enter the name for the file"]);
  if ($url && $name) {
    # Add .url extension, check if user gave it already
    $name =~ s/(\.url)?$/.url/;
    open FILE, ">$urldir/$name"
      or xdie("Error opening file $urldir/$name: $!");
    print FILE "$url\n";
    close FILE;
  }
}

# Show the bookmarks panel
sub do_showbook {
  system("$rox -$bookpos $bookname");
}

# Hide the bookmarks panel
sub do_hidebook {
  system("$rox -$bookpos ''");
}

# Open a URL, either dropped in or by asking
sub do_openurl {
  if (@ARGV) {
    open_url(@ARGV);
  }
  else {
    my $url=xprompt(["Please enter the URL"]);
    if ($url) {
      open_url($url);
    }
  }
}

# Process a URL file or string
sub open_url {
  foreach my $url (@_) {
    # If it exists as a file, then we use its contents
    # We understand both explorer-like .url files, and files
    # that only contain URL's, one per line. More than
    # one can be present in a single file.
    if (-f $url) {
      local @ARGV=($url);
      while (<>) {
	chomp;
	next if /^\[.*\]$/;
	s/^URL=//i;
	next unless $_;
	launch_url($_);
      }
    }
    else {
      # Consider it as a url by itself
      launch_url($url);
    }
  }
}

# Launch the viewer
sub launch_url {
  system("$openurl '@_'");
}

# Open the URLs directory
sub do_openurlsdir {
  system("$rox $urldir");
}
