#!/usr/bin/ruby
$KCODE = "u"
require 'jcode'
# get the path of the input file
inputFile = ARGV[0]
outputFile = ARGV[1]
# checks if the arguments are ok
if(ARGV[0].nil? || ARGV[1].nil?) then
puts "ERROR: Arguments missing. It should be:\n 'iTunes_to_zip.rb '"
exit
end
text = File.read(inputFile)
if(text =~ /^(.*?)Location/) then
nTabs = $1.count("\t")
else
puts "ERROR: the header for the file #{inputFile} is not as expected"
exit
end
# get the paths from the text
pathList = text.scan(/(?>(?>.*?\t){#{nTabs}})(.*?)\r/u).flatten
pathList.shift
# checks if there are bad elements in the list (ie. empty or nil elements)
badElements = pathList.any?{|p| p.nil? || p.empty?}
if(badElements) then
puts "ERROR: Some of the elements of pathList are empty or nil"
exit
end
# fixes a few things in each path: adds /Volumes/ and inverts : with /
pathList.collect!{ |s| "/Volumes/"+s.gsub!(/\/|:/) {|c| c==':'?'/':':'}}
puts "Total of files to be zipped = #{pathList.length}"
# compose the second argument to zip
list = '"'+pathList.join('" "').gsub('`','\\\`')+'"'
exec('zip -v '+outputFile+' '+list)