#!/bin/sh
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Test truncate command (replaces old purge action=truncate ...)
#
#
TestName="truncate-test"
JobName=FIFOTest
. scripts/functions

cwd=`pwd`
scripts/cleanup
scripts/copy-test-confs

echo $src > $tmp/file-list

sed 's/Pool Type = Backup/Pool Type = Backup; ActionOnPurge = Truncate/' $conf/bacula-dir.conf > $tmp/1
cp $tmp/1 $conf/bacula-dir.conf

case $(uname) in
  FreeBSD )
    DU="du -A -B 1"
    ;;
  * )
    DU="du -b"
    ;;
esac


start_test

cat >tmp/bconcmds <<END_OF_DATA
@$out /dev/null
messages
@$out $tmp/log1.out
label storage=File volume=TestVolume001
@########################################################
@# Run a first job on TestVolume001
@########################################################
run level=full job=$JobName yes
wait
messages
@#@$out $tmp/log2.out
@#restore where=${cwd}/tmp/bacula-restores select all storage=File done
@#yes
@#wait
@#messages
@#########################################################
@# Run a second job on TestVolume002
@#########################################################
@$out $tmp/log3.out
label storage=File volume=TestVolume002
update volume=TestVolume001 volstatus=Used
messages
show pool
@$out $tmp/log1.out
run level=full job=$JobName yes
wait
messages
@#########################################################
@# Run a 3th job on TestVolume003
@#########################################################
@$out $tmp/log3.out
update volume=TestVolume002 volstatus=Used
label storage=File volume=TestVolume003
messages
show pool
@$out $tmp/log1.out
run level=full job=$JobName yes
wait
messages
@#########################################################
@# Run a 4th job
@#########################################################
@$out $tmp/log5.out
update volume=TestVolume003 volstatus=Used
label storage=File volume=TestVolume004
messages
@$out $tmp/log1.out
run level=full job=$JobName yes
wait
messages
list volume
@$out $tmp/log1.out
update volume=TestVolume004 volstatus=Used
label storage=File volume=TestVolume005
run level=full job=$JobName yes
wait
messages
@$out $tmp/log7.out
@#########################################################
@# test that we cannot truncate a volume that is not purged
@#########################################################
@exec "$DU -s $tmp/TestVolume001"
truncate volume=TestVolume001  storage=File
@exec "$DU -s $tmp/TestVolume001"
messages
@$out $tmp/log6.out
@#########################################################
@# tests
@#########################################################
update volume=TestVolume002 actiononpurge=none
purge volume=TestVolume001
purge volume=TestVolume002
purge volume=TestVolume003
purge volume=TestVolume004
list volume
sql
select VolumeName, VolStatus, ActionOnPurge FROM Media;

setdebug level=100 director
wait
quit
END_OF_DATA

run_bacula
check_for_zombie_jobs storage=File

cd $tmp
if test "$debug" -eq 1 ; then
  echo "Volume sizes before truncate ..."
  ls -l TestVolume*
fi

print_debug "Check all media"

for i in 1 2 3 4 5 ; do
  size=`$DU TestVolume00$i|cut -f1`
  if test $size -lt 5000 ; then
     print_debug "ERROR 2: Volume TestVolum00$i is too small $size"
     ls -l TestVolume00$i
     estat=2
  fi
done

cd $cwd

echo "truncate volume=TestVolume001 storage=File" > $tmp/bconcmds
run_bconsole

cd $tmp

if test "$debug" -eq 1 ; then
  echo "Volume TestVolume001 should be truncated ..."
  ls -l TestVolume*
fi

for i in  2 3 4 5 ; do
  size=`$DU TestVolume00$i|cut -f1`
  if test $size -lt 5000 ; then
     print_debug "ERROR 3: Volume TestVolume00$i is too small $size"
     ls -l TestVolume00$i
     estat=3
  fi
done
for i in 1 ; do
  size=`$DU TestVolume00${i} | cut -f1`
  if test $size -gt 5000 ; then
     print_debug "ERROR 4: Volume TestVolume00$i is not truncated (too big) $size"
     ls -l TestVolume00$i
     estat=4
  fi
done

cd $cwd

echo "truncate volume storage=File" > $tmp/bconcmds
run_bconsole

cd $tmp
if test "$debug" -eq 1 ; then
  echo "Volumes 001, 003, and 004 should be truncated ..."
  ls -l TestVolume*
fi

for i in 2 5 ; do
  size=`$DU TestVolume00$i | cut -f1`
  if test $size -lt 5000 ; then
     print_debug "ERROR 5: Volume TestVolume00$i is too small $size"
     ls -l TestVolume00$i
     estat=5
  fi
done
for i in 1 3 4 ; do
  size=`$DU TestVolume00$i | cut -f1`
  if test $size -gt 5000 ; then
     print_debug "ERROR 6: Volume TestVolume00$i is not truncated (too big) $size"
     ls -l TestVolume00$i
     estat=6
  fi
done

if ! grep "No Volumes found to perform the command" log7.out > /dev/null ; then
    print_debug "ERROR 10: Should not be able to truncate TestVolum001 that holds bakcups"
    estat=1
else
   before=`grep $tmp/TestVolume001 log7.out | grep -v exec | head -n 1`
   after=`grep $tmp/TestVolume001 log7.out | grep -v exec | tail -n 1`
   if [ -z "$before" -o \( "$before" != "$after" \) ] ; then
      print_debug "ERROR 11: TestVolum001 has been truncated while it was holding bakcups"
      estat=1
   fi
fi

cd $cwd

cat <<END_OF_DATA > $tmp/bconcmds
@#########################################################
@# Display catalog settings for Pool and Media
@#########################################################
@$out $tmp/log4.out
setdebug level=0 director
sql
select VolumeName, ActionOnPurge FROM Media;
select Name, ActionOnPurge FROM Pool;

wait
quit
END_OF_DATA

run_bconsole

stop_bacula

touch $tmp/log2.out
check_two_logs

print_debug "Test if Pool record is ok"
r=`awk '/Default/ { print $4 }' $tmp/log4.out`
if [ "$r" != 1 ]; then
    print_debug "ERROR 5: ActionOnPurge on Pool record should be 1 ($r)"
    estat=5
fi

print_debug "Test TestVolume001 if Media record is ok"
r=`awk '/TestVolume001/ { print $4 }' $tmp/log4.out`
if [ "$r" != 1 ]; then
    print_debug "ERROR 6: ActionOnPurge on Media record should be 1"
    estat=6
fi

print_debug "Test TestVolume002 if Media record is ok"
r=`awk '/TestVolume002/ { print $4 }' $tmp/log4.out`
if [ "$r" != 0 ]; then
    print_debug "ERROR 7: ActionOnPurge on Media record should be 0"
    estat=7
fi

end_test
