#!/bin/sh

if [ -z $1 ]; then echo "Usage: $0 [fs]"; exit 1; fi

fs="$1"
oldfs="tank/ekx1/$fs"
newfs="${oldfs}_z3"

if zfs list $newfs > /dev/null; then
   echo target fs $newfs exists
else
   echo target fs $newfs does not exist
fi

oldsnap=$(zfs list -r -t snapshot -H -o name $oldfs | tail -n1)

if [ -z $oldsnap ]; then
    echo "FATAL ERROR: No snapshots found on $fs"
    exit 1
fi

newsnap=$(zfs list -r -t snapshot -H -o name $newfs | tail -n1)

if [ -z $newsnap ]; then
   echo "no snapshots on target. This will be level 0.";

   echo "zfs send -v $oldsnap | zfs recv -Fu $newfs"
   zfs send -v $oldsnap | zfs recv -u $newfs
else
    newsnap=${newsnap//.*@/@}
    echo newsnap $newsnap
fi

   
        


