#!/bin/bash
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# create APFS volume and play with snapshots commands
#
TestName="snapshot-apfs-test"
JobName=Incremental
. scripts/functions

${rscripts}/cleanup
${rscripts}/copy-test-confs

USE_SUDO=${USE_SUDO:-sudo}

if [ "$USE_SUDO" = sudo ]; then
    echo sudo=yes > $conf/bsnapshot.conf
fi

fdir=$cwd/apfs
touch $tmp/log2.out

mkdir -p $fdir

echo "$fdir" >${tmp}/file-list
cp -p ${src}/src/dird/*.c $fdir

make -C $cwd/build/src/tools installall

################################################################
# Unittest part -- This part can be used in the terminal
# cd bacula/src/tools
# bin=$PWD
# tmp=/tmp
# working=/tmp
(
    SNAPSHOT_DEVICE=`mount | awk '/on \/ / { print $1}'`
    DEVICE2=`mount | awk '/on \/System\/Volumes\/Data / { print $1}'`
    SNAPSHOT_NAME=Job.2021-07-02_17.59.49_08
    SNAPSHOT_FSTYPE=apfs
    SNAPSHOT_TYPE=apfs
    SNAPSHOT_WORKING=$working
    SNAPSHOT_MOUNTPOINT=/
    LOG="-d10 -o $tmp/bsnapshot.log"

    export SNAPSHOT_DEVICE SNAPSHOT_NAME SNAPSHOT_FSTYPE SNAPSHOT_TYPE SNAPSHOT_WORKING SNAPSHOT_MOUNTPOINT

    SNAPSHOT_ACTION=check   $bin/bsnapshot $LOG > $tmp/check.out
    grep status=1 $tmp/check.out > /dev/null
    if [ $? != 0 ]; then
        print_debug "ERROR: status in $tmp/check.out is incorrect"
        estat=1
    fi

    SNAPSHOT_ACTION=support $bin/bsnapshot $LOG > $tmp/support.out
    grep status=1 $tmp/support.out > /dev/null
    if [ $? != 0 ]; then
        print_debug "ERROR: support in $tmp/support.out is incorrect"
        estat=1
    fi

    SNAPSHOT_ACTION=create  SNAPSHOT_MOUNTPOINT=/ $bin/bsnapshot $LOG > $tmp/create.out
    grep status=1 $tmp/create.out > /dev/null
    if [ $? != 0 ]; then
        print_debug "ERROR: create in $tmp/create.out is incorrect"
        estat=1
    fi

    out=`sed 's/status/xxx/' $tmp/create.out`
    eval $out
    echo $volume

    SNAPSHOT_ACTION=list  SNAPSHOT_MOUNTPOINT=/ $bin/bsnapshot $LOG > $tmp/list.out
    nb=`grep name= $tmp/list.out | wc -l`
    if [ $nb -lt 1 ]; then
        print_debug "ERROR: list in $tmp/list.out is incorrect"
        estat=1
    fi

    SNAPSHOT_ACTION=mount SNAPSHOT_VOLUME=$volume SNAPSHOT_MOUNTPOINT=/ $bin/bsnapshot $LOG > $tmp/mount.out
    grep status=1 $tmp/mount.out > /dev/null
    if [ $? != 0 ]; then
        print_debug "ERROR: mount in $tmp/mount.out is incorrect"
        estat=1
    fi

    SNAPSHOT_ACTION=mount SNAPSHOT_VOLUME=$volume SNAPSHOT_DEVICE=$DEVICE2 SNAPSHOT_MOUNTPOINT=/System/Volumes/Data $bin/bsnapshot $LOG > $tmp/mount2.out
    grep status=1 $tmp/mount2.out > /dev/null
    if [ $? != 0 ]; then
        print_debug "ERROR: mount in $tmp/mount2.out is incorrect"
        estat=1
    fi

    out=`sed 's/status=/xxx=/' $tmp/mount.out`
    eval $out
    SNAPSHOT_ACTION=unmount SNAPSHOT_VOLUME=$volume SNAPSHOT_SNAPMOUNTPOINT=$snapmountpoint $bin/bsnapshot $LOG > $tmp/unmount.out
    grep status=1 $tmp/unmount.out > /dev/null
    if [ $? != 0 ]; then
        print_debug "ERROR: unmount in $tmp/unmount.out is incorrect"
        estat=1
    fi

    out=`sed 's/status=/xxx=/' $tmp/mount2.out`
    eval $out
    SNAPSHOT_ACTION=unmount SNAPSHOT_VOLUME=$volume SNAPSHOT_SNAPMOUNTPOINT=$snapmountpoint $bin/bsnapshot $LOG > $tmp/unmount2.out
    grep status=1 $tmp/unmount2.out > /dev/null
    if [ $? != 0 ]; then
        print_debug "ERROR: unmount in $tmp/unmount2.out is incorrect"
        estat=1
    fi

    SNAPSHOT_ACTION=delete SNAPSHOT_VOLUME=$volume $bin/bsnapshot $LOG > $tmp/delete.out
    grep status=1 $tmp/delete.out > /dev/null
    if [ $? != 0 ]; then
        print_debug "ERROR: delete in $tmp/delete.out is incorrect"
        estat=1
    fi

    unset SNAPSHOT_DEVICE SNAPSHOT_NAME SNAPSHOT_FSTYPE SNAPSHOT_TYPE SNAPSHOT_WORKING SNAPSHOT_MOUNTPOINT
    rm -f $working/*.out
)
################################################################


change_jobname CompressedTest $JobName
start_test

$bperl -e 'add_attribute("$conf/bacula-sd.conf", "MaximumConcurrentJobs", "10", "Device")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "SnapshotRetention", "30", "Client")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "SnapshotRetention", "60", "Job", "MonsterSave")'
$bperl -e 'add_attribute("$conf/bacula-dir.conf", "Enable Snapshot", "yes", "FileSet")'

cat <<END_OF_DATA >${tmp}/bconcmds
@output /dev/null
messages
@$out ${tmp}/log1.out
setdebug level=20 tags=snapshot fd trace=1
label storage=File volume=TestVolume001
label storage=File1 volume=TestVolume002
run job=$JobName yes
run job=MonsterSave yes
@sleep 2
status client
wait
messages
@output ${tmp}/list1
list files jobid=1
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File

cat <<END_OF_DATA >${tmp}/bconcmds
@$out ${tmp}/log3.out
setdebug level=20 tags=snapshot fd trace=1
list snapshot
llist snapshot
snapshot list
@sleep 30
@$out ${tmp}/log3.out
snapshot prune
list snapshot
snapshot list
quit
END_OF_DATA

run_bconsole

stop_bacula

check_two_logs

end_test
