module Sys

def copy(file_name, dest_file)

Copy a single file from +file_name+ to +dest_file+.
def copy(file_name, dest_file)
  log "Copying file #{file_name} to #{dest_file}"
  File.copy(file_name, dest_file)
end

def copy_files(wildcard, dest_dir)

Copy all files matching +wildcard+ into the directory +dest_dir+.
def copy_files(wildcard, dest_dir)
  for_matching_files(wildcard, dest_dir) { |from, to| copy(from, to) }
end

def delete(*wildcards)

recursively delete directories.
directory, it must be empty to be removed. used +delete_all+ to
Remove all files matching +wildcard+. If a matching file is a
def delete(*wildcards)
  wildcards.each do |wildcard|
    Rake.glob(wildcard).each do |fn|
      if File.directory?(fn)
        log "Deleting directory #{fn}"
        Dir.delete(fn)
      else
        log "Deleting file #{fn}"
        File.delete(fn)
      end
    end
  end
end

def delete_all(*wildcards)

Recursively delete all files and directories matching +wildcard+.
def delete_all(*wildcards)
  wildcards.each do |wildcard|
    Rake.glob(wildcard).each do |fn|
      next if ! File.exist?(fn)
      if File.directory?(fn)
        Rake.glob("#{fn}/*").each do |subfn|
          next if subfn=='.' || subfn=='..'
          delete_all(subfn)
        end
        log "Deleting directory #{fn}"
        Dir.delete(fn)
      else
        log "Deleting file #{fn}"
        File.delete(fn)
      end
    end
  end
end

def for_files(*wildcards)

Perform a block with each file matching a set of wildcards.
def for_files(*wildcards)
  wildcards.each do |wildcard|
    Rake.glob(wildcard).each do |fn|
      yield(fn)
    end
  end
end

def for_matching_files(wildcard, dest_dir)

def for_matching_files(wildcard, dest_dir)
  Rake.glob(wildcard).each do |fn|
    dest_file = File.join(dest_dir, fn)
    parent = File.dirname(dest_file)
    makedirs(parent) if ! File.directory?(parent)
    yield(fn, dest_file)
  end
end

def indir(dir)

executing the given block.
Make +dir+ the current working directory for the duration of
def indir(dir)
  olddir = Dir.pwd
  Dir.chdir(dir)
  yield
ensure
  Dir.chdir(olddir)
end

def install(wildcard, dest_dir, mode)

directory. The permission mode is set to +mode+.
Install all the files matching +wildcard+ into the +dest_dir+
def install(wildcard, dest_dir, mode)
  Rake.glob(wildcard).each do |fn|
    File.install(fn, dest_dir, mode, $verbose)
  end
end

def link(file_name, dest_file)

Link +file_name+ to +dest_file+.
def link(file_name, dest_file)
  log "Linking file #{file_name} to #{dest_file}"
  File.link(file_name, dest_file)
end

def link_files(wildcard, dest_dir)

Link all files matching +wildcard+ into the directory +dest_dir+.
def link_files(wildcard, dest_dir)
  for_matching_files(wildcard, dest_dir) { |from, to| link(from, to) }
end

def log(msg)

Write a message to standard error if $verbose is enabled.
def log(msg)
  print "  " if $trace && $verbose
  $stderr.puts msg if $verbose
end

def makedirs(*dirs)

Make the directories given in +dirs+.
def makedirs(*dirs)
  dirs.each do |fn|
    log "Making directory #{fn}"
    File.makedirs(fn)
  end
end

def quiet(&block)

Perform a block with $verbose disabled.
def quiet(&block)
  with_verbose(false, &block)
end

def ruby(*args)

Run a Ruby interpreter with the given arguments.
def ruby(*args)
  run "#{RUBY} #{args.join(' ')}"
end

def run(cmd)

Run the system command +cmd+.
def run(cmd)
  log cmd
  system(cmd) or fail "Command Failed: [#{cmd}]"
end

def split_all(path)

split_all("a/b/c") => ['a', 'b', 'c']
For example:

Split a file path into individual directory names.
def split_all(path)
  head, tail = File.split(path)
  return [tail] if head == '.' || tail == '/'
  return [head, tail] if head == '/'
  return split_all(head) + [tail]
end

def symlink(file_name, dest_file)

Symlink +file_name+ to +dest_file+.
def symlink(file_name, dest_file)
  log "Symlinking file #{file_name} to #{dest_file}"
  File.symlink(file_name, dest_file)
end

def symlink_files(wildcard, dest_dir)

Symlink all files matching +wildcard+ into the directory +dest_dir+.
def symlink_files(wildcard, dest_dir)
  for_matching_files(wildcard, dest_dir) { |from, to| link(from, to) }
end

def verbose(&block)

Perform a block with $verbose enabled.
def verbose(&block)
  with_verbose(true, &block)
end

def with_verbose(v)

def with_verbose(v)
  oldverbose = $verbose
  $verbose = v
  yield
ensure
  $verbose = oldverbose
end