commit 6781a653379a9d3fd1e98f88fd7b7e6f6cdb714b from: mtmn date: Sun Jul 26 11:40:44 2026 UTC add rbx_copy_local commit - 5222d7dd8ef96ff2f2533f6094fafc11c16c1221 commit + 6781a653379a9d3fd1e98f88fd7b7e6f6cdb714b blob - /dev/null blob + c6d35f9e06970734c2eac00035abd2fded2bc3e8 (mode 644) --- /dev/null +++ modules/mixins/dotfiles/bin/rbx_copy_local @@ -0,0 +1,84 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# Reads an M3U8 playlist and copies referenced files from a Windows-style +# path on a mounted disk to a local backlog directory. + +# Usage: rbx_copy_local PLAYLIST [DESTDIR] +# +# Defaults: +# DESTDIR = ~/misc/music/backlog/ +# MOUNT = ~/misc/mnt/disk/boo (what F:\ maps to) +# +# Override with environment variables: +# MOUNT_ROOT - Linux path equivalent of the Windows drive root +# DRIVE_LETTER - Windows drive letter to replace (default: F) + +require "pathname" +require "fileutils" + +abort "Usage: #{$PROGRAM_NAME} PLAYLIST [DESTDIR]" if ARGV.empty? + +playlist = Pathname.new(ARGV[0]).expand_path +playlist_name = playlist.sub_ext("").basename.to_s + +destdir = if ARGV[1] + Pathname.new(ARGV[1]).expand_path +else + Pathname.new("~").expand_path / "misc/music/backlog" / playlist_name +end + +mount_root = Pathname.new( + ENV.fetch("MOUNT_ROOT") { Pathname.new("~").expand_path / "misc/mnt/disk/boo" }.to_s +) + +drive_letter = ENV.fetch("DRIVE_LETTER", "F") + +def die(message) + warn "Error: #{message}" + exit 1 +end + +die "playlist not found: #{playlist}" unless playlist.file? +die "mount root does not exist: #{mount_root}" unless mount_root.directory? + +FileUtils.mkdir_p(destdir) + +copied = 0 +skipped = 0 +new_playlist_lines = [] + +playlist.readlines(chomp: true).each do |line| + if line.empty? || line.start_with?("#") + new_playlist_lines << line + next + end + + relative = line.sub(%r{^#{drive_letter}:}i, "").delete_prefix("\\").tr("\\", "/") + source = mount_root / relative + + unless source.file? + warn "Missing: #{source}" + skipped += 1 + new_playlist_lines << line + next + end + + target = destdir / relative + + if target.exist? + puts "Exists: #{target}" + skipped += 1 + else + FileUtils.mkdir_p(target.dirname) + FileUtils.cp(source, target, verbose: true) + copied += 1 + end + + new_playlist_lines << target.to_s +end + +new_playlist = destdir / playlist.basename +new_playlist.write(new_playlist_lines.join("\n") + "\n") + +puts "#{copied} copied, #{skipped} skipped"