rendered paste body#!/usr/bin/env rubymodule Ascent def Ascent.init safe_mkdir( $*[0] ) @link_target = Dir.new($*[0]) @count = 0 @slash = "/" @space = " - " end def Ascent.safe_mkdir( dirname ) begin Dir.mkdir( dirname ) rescue Errno::EEXIST #puts( dirname + " exists; not creating" ) end end def Ascent.handle_target( target ) if target[-1] != "/" target = target + "/" end target = Dir.new( target ) target.each { |artist| handle_artist( target, artist ) } puts("#{count}") end def Ascent.handle_artist( target, artist ) if artist == "." #puts("Doing nothing for '.'") return 0 elsif artist == ".." #puts("Doing nothing for '..'") return 0 else artist = Dir.new( target.path + artist ) # There wil always be a slash at the end of the target.path artist.each { |album| handle_album( artist, album ) } end end def Ascent.handle_album( artist, album ) if album == "." #puts("Doing nothing for '.'") return elsif album == ".." #puts("Doing nothing for '..'") return else begin album = Dir.new( artist.path + @slash + album ) album.each { |file| handle_file( album, file, artist.path, album.path ) } rescue SystemCallError puts("Doing nothing for #{artist.path + @slash + album}") end end end def Ascent.handle_file( initial_path, filename, artist, album ) #puts("#{@count}") if filename == "." #puts("Doing nothing for '.'") return elsif filename == ".." #puts("Doing nothing for '..'") return else #puts( "#{initial_path.path} , #{filename} , #{artist} , #{album}" ) file_path = album + @slash + filename album = album.split(@slash) target = @link_target.path + @slash + album[1] + @space + album[2] + @space + filename begin File.link( file_path, target ) rescue SystemCallError #puts("Doing nothing for #{target}; file may exist") end end @count = @count + 1 end endif $*.length < 2 puts("Usage: descent-unlinker <target directory for new links> <directory 1> <directory 2> ... <directory n>") exitendAscent.init()$*[1..-1].each{|argument| Ascent.handle_target( argument )}