#!/usr/bin/env ruby # Copyright (C) 2013-2016 all contributors # License: GPL-3.0+ # frozen_string_literal: true require 'tempfile' require 'dtas/xs' usage = "Usage: #$0 FILENAME" editor = ENV["VISUAL"] || ENV["EDITOR"] || "vi" ARGV.size > 0 or abort usage include DTAS::XS def err_msg(cmd, status) "E: #{xs(cmd)} failed: #{status.inspect}" end def x!(*cmd) system(*cmd) or abort err_msg(cmd, $?) end def tmpfile(file, suffix) tmp = Tempfile.new([File.basename(file), suffix]) tmp.sync = true tmp.binmode tmp end ARGV.each do |file| file =~ /\.flac\z/i or warn "Unsupported suffix, assuming FLAC" tmp = tmpfile(file, '.cue') begin # export the temporary file for the user to edit if system(*%W(metaflac --export-cuesheet-to=#{tmp.path} #{file})) remove_existing = true backup = tmpfile(file, '.backup.cue') else remove_existing = false backup = nil tmp.puts 'FILE "dtas-cueedit.tmp.flac" FLAC' tmp.puts ' TRACK 01 AUDIO' tmp.puts ' INDEX 01 00:00:00' end # keep a backup, in case the user screws up the edit original = File.binread(tmp.path) backup.write(original) if backup # user edits the file x!("#{editor} #{xs(tmp.path)}") # avoid an expensive update if the user didn't change anything current = File.binread(tmp.path) if current == original $stderr.puts "tags for #{xs(file)} unchanged" if $DEBUG next end # we must remove existing tags before importing again if remove_existing x!(*%W(metaflac --remove --block-type=CUESHEET #{file})) end # try to import the new file but restore from the original backup if the # user wrote an improperly formatted cue sheet cmd = %W(metaflac --import-cuesheet-from=#{tmp.path} #{file}) if ! system(*cmd) && backup warn err_msg(cmd, $?) warn "E: restoring original from backup" x!(*%W(metaflac --import-cuesheet-from=#{backup.path} #{file})) warn "E: backup cuesheet restored, #{xs(file)} unchanged" exit(false) end ensure tmp.close! backup.close! if backup end end