#!/usr/bin/env bash
#
# Copyright (C) 2000-2022 Kern Sibbald
# License: BSD 2-Clause; see file LICENSE-FOSS
#
# Cleanup left over files -- both before and after test run
#
. scripts/functions

cwd=`pwd`
chmod -R u+rwx ${tmp}
rm -rf ${tmp}/FileVolume*
rm -rf ${tmp}/TestVolume001 ${tmp}bacula-restores ${tmp}/Small*
rm -rf ${tmp}/sed_tmp ${tmp}/file-list
rm -rf ${tmp}/*
rm -rf ${working}/log ${tmp}/TEST-*
rm -rf ${working}/log ${tmp}/Backup-*
rm -rf ${working}/*.bsr ${working}/log*.sd
rm -rf ${working}/*.trace ${working}/*.traceback ${working}/*.state
rm -rf ${working}/@*
rm -rf ${working}/*
rm -rf ${tmp}disk-changer
rm -f ${cwd}/bin/plugins/test-plugin-fd.so
rm -rf ${cwd}/mnt
find . -name "gigaslam.gif" -exec rm -f {} \;


# bin directory does not always exist
if [ -d ${bin} ] ; then
  if [ -f ${bin}/bacula ] ; then
     ${bin}/bacula stop -KILL 2>&1 >/dev/null
  fi
fi

cd ${scripts}
./drop_bacula_tables      >/dev/null 2>&1
./make_bacula_tables      >/dev/null 2>&1
./grant_bacula_privileges >/dev/null 2>&1
cd ${cwd}

if [ x$USE_VTAPE = xyes ]; then
   rm -rf ${working}/ach
fi

if [ x$FORCE_DEDUP = xyes ]; then
   rm -rf ${working}/dde
   rm -rf ${working}/ddefd
fi

# Remove cloud test volumes
if [ x$FORCE_CLOUD = xyes ]; then

   # update cloud before cleanup so we can retrieve accurate information
   update_cloud

   args=""
   cloud_attr=$($bperl -e "get_attribute('$conf/bacula-sd.conf', 'Cloud', '$CLOUD_NAME', 'HostName')")
   if [ "${cloud_attr}" ]; then 
      args="${args} -h ${cloud_attr}"
   fi
   cloud_attr=$($bperl -e "get_attribute('$conf/bacula-sd.conf', 'Cloud', '$CLOUD_NAME', 'BucketName')")
   if [ "${cloud_attr}" ]; then 
      args="${args} -n ${cloud_attr}"
   fi
   cloud_attr=$($bperl -e "get_attribute('$conf/bacula-sd.conf', 'Cloud', '$CLOUD_NAME', 'AccessKey')")
   if [ "${cloud_attr}" ]; then 
      args="${args} -a ${cloud_attr}"
   fi
   cloud_attr=$($bperl -e "get_attribute('$conf/bacula-sd.conf', 'Cloud', '$CLOUD_NAME', 'SecretKey')")
   if [ "${cloud_attr}" ]; then 
      args="${args} -s ${cloud_attr}"
   fi
   cloud_attr=$($bperl -e "get_attribute('$conf/bacula-sd.conf', 'Cloud', '$CLOUD_NAME', 'Protocol')")
   if [ "${cloud_attr}" ]; then 
      args="${args} -p ${cloud_attr}"
   fi
   cloud_attr=$($bperl -e "get_attribute('$conf/bacula-sd.conf', 'Cloud', '$CLOUD_NAME', 'Region')")
   if [ "${cloud_attr}" ]; then 
      args="${args} -r ${cloud_attr}"
   fi
   cloud_attr=$($bperl -e "get_attribute('$conf/bacula-sd.conf', 'Cloud', '$CLOUD_NAME', 'Driver')")
   if [ "${cloud_attr}" ]; then 
      args="${args} -y ${cloud_attr}"

      shopt -s nocasematch
      case "${cloud_attr}" in
         "google") 
            args="${args} -c ${cwd}/bin/plugins/google_cloud_driver"
         ;;
         "oracle") 
            args="${args} -c ${cwd}/bin/plugins/oracle_cloud_driver"
         ;;
         "swift") 
            args="${args} -c ${cwd}/bin/plugins/swift_cloud_driver"
         ;;
         "generic") 
            args="${args} -c ${cwd}/bin/plugins/generic_cloud_driver"
         ;;
         "azure") 
            args="${args} -c ${cwd}/bin/plugins/was_cloud_driver"
         ;;
         *)
         ;;
      esac
   fi
   cloud_attr=$($bperl -e "get_attribute('$conf/bacula-sd.conf', 'Cloud', '$CLOUD_NAME', 'UriStyle')")
   if [ "${cloud_attr}" ]; then 
      args="${args} -u ${cloud_attr}"
   fi
   cloud_attr=$($bperl -e "get_attribute('$conf/bacula-sd.conf', 'Cloud', '$CLOUD_NAME', 'BlobEndpoint')")
   if [ "${cloud_attr}" ]; then 
      args="${args} -b ${cloud_attr}"
   fi
   cloud_attr=$($bperl -e "get_attribute('$conf/bacula-sd.conf', 'Cloud', '$CLOUD_NAME', 'TableEndpoint')")
   if [ "${cloud_attr}" ]; then 
      args="${args} -t ${cloud_attr}"
   fi
   cloud_attr=$($bperl -e "get_attribute('$conf/bacula-sd.conf', 'Cloud', '$CLOUD_NAME', 'QueueEndpoint')")
   if [ "${cloud_attr}" ]; then 
      args="${args} -q ${cloud_attr}"
   fi
   cloud_attr=$($bperl -e "get_attribute('$conf/bacula-sd.conf', 'Cloud', '$CLOUD_NAME', 'DriverCommand')")
   if [ "${cloud_attr}" ]; then 
      args="${args} -c ${cloud_attr}"
   fi
   args="${args} -l ${cwd}/bin/plugins/"

   if [ -d ${bin} ] ; then
     if [ -f ${bin}/bcloud ] ; then
        ${bin}/bcloud ${args} -v 'Vol' clean
        ${bin}/bcloud ${args} -v 'vol' clean
        ${bin}/bcloud ${args} -v 'TestVol' clean   
        ${bin}/bcloud ${args} -v 'Small' clean   
        ${bin}/bcloud ${args} -v 'TEST' clean   
        ${bin}/bcloud ${args} -v 'Backup-' clean
     fi
   fi

   # try to shutdown emulators

   # Might report incorrect error if not setup correctly
   true
fi
