#!/usr/bin/ruby.ruby3.4 

# This file is part of the jump project
# 
# Copyright (C) 2010 Flavio Castelli <flavio@castelli.name>
# Copyright (C) 2010 Giuseppe Capizzi <gcapizzi@gmail.com>
# 
# jump is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# jump is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with Keep; if not, write to the
# Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.

THIS_FILE = File.symlink?(__FILE__) ? File.readlink(__FILE__) : __FILE__

require "#{File.dirname(THIS_FILE)}/../lib/bookmarks"
require 'rubygems'
require 'optparse'

options = {}
begin
  opts = OptionParser.new do |opts|
    opts.banner = "Usage: jump [options] [BOOKMARK[/path/to/subfolder]]"

    opts.on("-a", "--add BOOKMARK", "Saves the current directory in BOOKMARK") do |v|
      options[:add] = v
    end

    opts.on("-d", "--del BOOKMARK", "Deletes BOOKMARK") do |v|
      options[:del] = v
    end

    opts.on("-l", "--list", "Prints the list of all saved bookmarks") do |v|
      options[:list] = true
    end

    opts.on("-p", "--path", "Prints the path of the bookmark") do |v|
      options[:path] = v
    end

    opts.on("--bash-integration", "Prints the path to directory containing the Bash integration files") do
      puts "#{File.dirname(THIS_FILE)}/../bash_integration"
      exit
    end

    opts.on("--zsh-integration", "Prints the path to the ZSH integration file") do
      puts "#{File.dirname(THIS_FILE)}/../zsh_integration/jump"
      exit
    end

    opts.on_tail("-h", "--help", "Show this message") do
      puts opts
      exit
    end
  end
  opts.parse!
rescue OptionParser::InvalidOption
  if $!.args.first == "--complete"
    # This is an hidden method used by the shell completion
    bookmarks = Bookmarks.new
    puts bookmarks.complete(ARGV[0])
    exit 0
  else
    STDERR.puts $!
    exit 1
  end
end

bookmarks = Bookmarks.new

### handle options, if present

if options.has_key?(:add)
  bookmarks.add(Dir.pwd, options[:add])
  bookmarks.save
  puts "Bookmark added."
end

if options.has_key?(:del)
  if bookmarks.del(options[:del])
    bookmarks.save
    puts "Bookmark deleted."
  else
    STDERR.puts "Bookmark '#{options[:del]}' has not been deleted, it's unknown."
  end
end

if options.has_key?(:list)
  puts bookmarks
end

exit if ARGV.empty?

### remaining argument(s) must be a single bookmark

if ARGV.size > 1
  STDERR.puts opts
  exit 1
end

expanded_path = bookmarks.expand_path(ARGV[0])
if expanded_path.nil?
  STDERR.puts "Unknown bookmark: '#{ARGV[0]}'."
  exit 42
else
  puts expanded_path
end

