#!/usr/bin/env ruby # Copyright (C) 2015-2016 all contributors # 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 ', "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