#!/usr/local/bin/ruby

lib_dir = File.join(File.dirname(__FILE__), '..', 'lib')
$LOAD_PATH.unshift lib_dir if File.directory?(lib_dir)

require 'getoptlong'
require 'zfstools'

opts = GetoptLong.new(
  [ "--dry-run",   "-n",           GetoptLong::NO_ARGUMENT ],
  [ "--verbose",   "-v",           GetoptLong::NO_ARGUMENT ],
  [ "--debug",     "-d",           GetoptLong::NO_ARGUMENT ]
)

$dry_run = false
opts.each do |opt, arg|
  case opt
  when '--dry-run'
    $dry_run = true
  when '--verbose'
    $verbose = true
  when '--debug'
    $debug = true
  end
end


def usage
  puts <<-EOF
Usage: #{$0} [-dnv] DATASET
  EOF
  format = "    %-15s %s"
  puts format % ["-d", "Show debug output."]
  puts format % ["-n", "Do a dry-run. Nothing is committed. Only show what would be done."]
  puts format % ["-v", "Show what is being done."]
  exit
end

usage if ARGV.length < 1

dataset=ARGV[0]

snapshot_format = "%Y-%m-%dT%H:%M:%S"
snapshot_name = Time.now.strftime(snapshot_format)
snapshot = "#{dataset}@#{snapshot_name}"
Zfs::Snapshot.create(snapshot, 'db' => 'mysql', 'recursive' => true)
