#!/bin/bash
############################################
# Removing the complete wine working dir
# Usage: ./cleanup-wine
#
# Changelog:
# rpm69 - allowing wsi.xml to remain there after DTB is removed. I tis needed for the webpage.
# rpm77 - moving from /var/tmp/dtb into /var/pgsql/dtb_config/
############################################

# Import definitions
CMD=$(basename $0)
if [ -f /opt/dtb/bin/_common_definitions ]; then
    . /opt/dtb/bin/_common_definitions
else
	logger -t $CMD -p ERROR -s "The /opt/dtb/bin/_common_definitions file does not exist, exiting the script..."
	exit 1
fi

# Reset capabilities for wine binaries
for SETCAPBIN in $SETCAPBINS; do
{
    plog "Resetting capabilities for $SETCAPBIN."
    setcap -r $SETCAPBIN 1>/dev/null 2>/dev/null
} done;

# Remove wine working directory
plog "Removing the working directory from /var/opt/dtb/wineroot..."
rm -rf $DTB_WORKINGDIR
plog "Removing working directory complete."

# Remove DRBD share
plog "Removing the DTB related part of DRBD share from /var/pgsql/dtb..."
rm -rf $DTB_DRBD_SHARE
plog "Removing DRBD share complete."

plog "Removing the files from $DTB_CFGDIR..."
# Before removing the config files, back-up the wsi.ml into /var/tmp
cd $DTB_CFGDIR
if [ -f wsi.xml ]; then
	mv wsi.xml /var/tmp/
fi
# Remove the config files
rm -r $DTB_CFGDIR/*
# Create the config directory for the wsi.xml
if [ ! -d $DTB_CFGDIR ]; then
	mkdir $DTB_CFGDIR
	chmod 755 $DTB_CFGDIR
fi
# Restore the wsi.xml
cd /var/tmp/
if [ -f wsi.xml ]; then
	mv wsi.xml $DTB_CFGDIR/
fi
plog "Removing temporary files complete."
