#!/usr/bin/env perl

use strict;
use warnings;

use FindBin;
use Time::Piece;

my %dataSets = (
    'tank'               => '',
    'tank/source'        => 'src',
    'backup'             => '',
    'backup/destination' => 'dst',
    'remote'             => '',
    'remote/destination' => 'dst',
);

#print zfs command
print STDERR '# zfs ' . join(' ', @ARGV) . "\n";

my $command = shift @ARGV or exit 1;

for ($command){
    /^(?:set|inherit|send|recv)$/ && exit;

    #pretend snapshot to fail => snapshot does exist already
    /^snapshot$/ && exit 1;

    /^list$/ && do {
        if ($ARGV[4] && $ARGV[3] eq '-t' && $ARGV[4] eq 'snapshot'){
            if ($ARGV[-1] =~ /^[^\@]+\@[^\@]+$/){
                print $ARGV[-1] . "\n";
                exit;
            }
            exit 1 if !exists $dataSets{$ARGV[-1]};
            my $snapCount = ($dataSets{$ARGV[-1]} eq 'src') ? 60 : 58;
            #get timestamp rounded to minutes
            my $time = localtime(int(time / 60) * 60 - 3600);
            for (my $i = 0; $i < $snapCount; $i++){
                print $ARGV[-1] . '@' . $time->strftime('%Y-%m-%d-%H%M%S') . "\n";
                $time += 60;
            }
            exit;
        }
        #if there is a '/' it is a dataset
        if ($ARGV[-1] =~ /\//){
            print $ARGV[-1] if grep { $ARGV[-1] eq $_ } keys %dataSets;
            exit;
        }
        for (sort keys %dataSets){
            print "$_\n";
        }
        exit;
    };

    /^destroy$/ && do {
        if ($ARGV[0] eq '-nv'){
            print "would reclaim 1337G\n";
        }
        exit;
    };

    /^get$/ && do {
        if ($ARGV[6] && $ARGV[6] eq 'usedbysnapshots'){
            print "10G\n";
            exit;
        }
        exit if !(exists $dataSets{$ARGV[-1]} && $dataSets{$ARGV[-1]} eq 'src');
        print <<"ZFS_GET_END";
org.znapzend:dst_0_plan     1hours=>10minutes,30minutes=>5minutes,10minutes=>60seconds
org.znapzend:dst_a_plan     1hours=>10minutes,40minutes=>5minutes,20minutes=>60seconds
org.znapzend:dst_fail_plan  1hours=>1minutes
org.znapzend:src            tank/source
org.znapzend:src_plan       1hours=>10minutes,30minutes=>5minutes,1minutes=>6seconds
org.znapzend:recursive      on
org.znapzend:tsformat       %Y-%m-%d-%H%M%S
org.znapzend:enabled        on
org.znapzend:dst_0          backup/destination
org.znapzend:dst_a          root\@remote:remote/destination
org.znapzend:dst_fail       backup/destfail
org.znapzend:mbuffer        $FindBin::Bin/mbuffer:31337
org.znapzend:mbuffer_size   100M
org.znapzend:pre_znap_cmd   /bin/echo 'pre znap command'
org.znapzend:post_znap_cmd  /bin/echo 'post znap command'
ZFS_GET_END

        exit;
    };

    exit 1;
}

1;
