about summary refs log tree commit homepage
path: root/bin/dtas-mlib
blob: 45c3d0163ab649c0fea4fe8607174a45fc7af5bd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!/usr/bin/env ruby
# Copyright (C) 2015-2016 all contributors <dtas-all@nongnu.org>
# License: GPL-3.0+ <https://www.gnu.org/licenses/gpl-3.0.txt>
# frozen_string_literal: true
usage = "#$0 [-d DATABASE-URI] ACTION [ARGS]"
Thread.abort_on_exception = $stderr.sync = $stdout.sync = true
trap(:INT, 'SYSTEM_DEFAULT')
trap(:PIPE, 'SYSTEM_DEFAULT')
require 'dtas/mlib'
require 'optparse'
path = '~/.dtas/mlib.sqlite'
db = File.expand_path(path)
force = false
OptionParser.new('', 24, '  ') do |op|
  op.banner = usage
  op.on('-d', '--database <URI|PATH>', "database (default: #{path}") do |d|
    db = d
  end
  op.on('-f', '--force', 'force updates') { force = true }
  op.on('-h', '--help') do
    puts(op.to_s)
    exit
  end
  op.parse!(ARGV)
end

unless db.include?('://')
  dir = File.dirname(db)
  unless File.directory?(dir)
    require 'fileutils'
    FileUtils.mkpath(dir)
  end
end

def mlib(db, migrate = false)
  m = DTAS::Mlib.new(db)
  m.migrate if migrate
  m
end

case action = ARGV.shift
when 'update', 'up'
  directory = ARGV.shift or abort "DIRECTORY required\n#{usage}"
  mlib(db, migrate = true).update(directory, force: force)
when 'stats'
  s = mlib(db, true).stats
  %w(artists albums songs db_playtime).each { |k| puts "#{k}: #{s[k.to_sym]}" }
when 'dump' # mainly for debugging
  directory = ARGV.shift || '/'
  mlib(db).dump(directory, {}, lambda do |parent, node, comments|
    puts "Path: #{parent[:dirname]}#{node[:name]}"
    puts "Length: #{node[:tlen]}"
    return if comments.empty?
    puts 'Comments:'
    comments.each do |k,v|
      if v.size == 1
        puts "\t#{k}: #{v[0]}"
      else
        v << ''
        puts "\t#{k}:\n\t\t#{v.join("\t\t\n")}"
      end
    end
    puts
  end)
when 'find', 'search'
  m = mlib(db)
  cache = {}
  m.__send__(action, *ARGV) { |node| puts m.path_of(node, cache) }
else
  abort usage
end