#!/usr/bin/env ruby $LOAD_PATH.unshift("#{__dir__}/../lib") require 'logger' require 'optparse' require 'unitf/tag' logger = Logger.new($stdout) actions = [] files = [] opt = { recursive: false, force: false } targets = OptionParser.new do |opts| opts.on('-r', '--recursive', 'Auto Cover') do opt[:recursive] = true end opts.on('--dump', 'Dump Tags') do opt[:dump] = true end opts.on('-d', '--debug', 'Debug') do UnitF::Tag.logger.level = Logger::DEBUG end opts.on('-j', '--json', 'JSON Output') do opt[:format] = :json end opts.on('-l', '--line', 'Single Line Output') do opt[:format] = :line end opts.on('--auto_cover', 'Auto Cover') do actions << :auto_cover end opts.on('--delete_cover', 'Delete Cover') do actions << :delete_cover end opts.on('--auto_tag', 'Auto Tag') do actions << :auto_tag end opts.on('-f', '--force', 'Force') do opt[:force] = true end opts.on('--artist ARTIST', 'Artist') do |arg| actions << :artist opt[:artist] = arg end opts.on('--album ALBUM', 'Album') do |arg| actions << :album opt[:album] = arg end opts.on('--title TITLE', 'Song Title') do |arg| actions << :title opt[:title] = arg end opts.on('--genre GENRE', 'Genre') do |arg| actions << :genre opt[:genre] = arg end end.parse! files = UnitF::Tag::FileSet.new(targets) if files.size.zero? logger.error('Cannot find any files to operate on') end if actions.size.zero? UnitF::Tag.list(files, format: opt[:format]) exit 0 end files.each do |file| logger.info("Processing file: #{file}") file.open do |o| actions.each do |action| case action when :delete_cover logger.info('Delete cover') o.delete_cover! when :auto_cover unless o.cover? logger.info("Auto Cover #{file.cover_path}") begin o.auto_cover! rescue => e logger.error("Failed to auto-cover file #{e}") end end when :auto_tag logger.info('Auto Tag') o.auto_tag! when :artist logger.info("Setting artist to #{opt[:artist]}") o.tag.artist = opt[:artist] when :album logger.info("Setting album to #{opt[:album]}") o.tag.album = opt[:album] when :title logger.info("Setting title to #{opt[:title]}") o.tag.title = opt[:title] when :genre logger.info("Setting genre to #{opt[:genre]}") o.tag.genre = opt[:genre] when :track logger.info("Setting track to #{opt[:track]}") o.tag.genre = opt[:track] end end o.save || logger.error("Unable to save changes to #{file}") end end