## Uploading files

# Here I'm saying: make a new Strongspace object, add in files
# from 6 different directories, and upload (via rsync) the files in
# those directories to the folder (path) _ambrose_ on my strongspace.
Strongspace.new do |stuff|
stuff << from('~') { %w(bin mess) }
stuff << from('~/Pictures') { ['avatar', 'iPhoto Library'] }
stuff << from('~/Documents') { %w(iChats receipts secure work) }
stuff << from('~/Music/iTunes') { ['iTunes Library', 'iTunes Music', 'iTunes Music Library.xml'] }
stuff << from('~/Library') { %w(Calendars Keychains Safari) }
end.upload!(:path => 'ambrose')

## The whole thing

#!/usr/bin/ruby

class Strongspace
attr_accessor :files
REMOTE = 'you@your.strongspace.com'

def initialize(&files)
super
grab(&files) if block_given?
self
end

# Rsync the files to a certain path, keeping the local directory structure
def upload!(options = {})
path = options[:path] || ''

@files.map{|file| File.expand_path(file)}.each do |file|
full_path = path.dup
full_path << File.dirname(file).gsub(File.expand_path('~/'), '')# if File.directory?(file)
cmd = "rsync -rltvz --dry-run \"#{file}\" #{REMOTE}:\"#{full_path}/\""

puts "\n#{file} -> #{REMOTE}:#{full_path}"
# This prints the sync in real time, just _puts `cmd`_ waits for the command to finish before printing it.
rsync = IO.popen(cmd, "r")
while line = rsync.gets
puts line
end
rsync.close
end
end

# Get the files you want to upload.
# Pass them in to the yielded array from within a block.
#
# Strongspace.new.grab do |files|
# files << '~/Documents/please_dear_god_dont_lose_this.txt'
# end
def grab
return 'No local files given in block' unless block_given?
@files = (yield []).flatten
end

def list
puts @files.inspect
end

module Helpers
# Get files from a certain directory:
# from('~') { [array, of, files/directories] }
# will give you
# ['~/array', '~/of', ...]
def from(directory)
return unless directory and block_given?
yield.map {|file| "#{directory}/#{file}" }
end
end
end

include Strongspace::Helpers

# Here I'm saying: make a new Strongspace object, add in files
# from 6 different directories, and upload (via rsync) the files in
# those directories to the folder (path) _ambrose_ on my strongspace.
Strongspace.new do |stuff|
stuff << from('~') { %w(bin mess) }
stuff << from('~/Pictures') { ['avatar', 'iPhoto Library'] }
stuff << from('~/Documents') { %w(iChats receipts secure work) }
stuff << from('~/Music/iTunes') { ['iTunes Library', 'iTunes Music', 'iTunes Music Library.xml'] }
stuff << from('~/Library') { %w(Calendars Keychains Safari) }
end.upload!(:path => 'ambrose')