#!/bin/bash

BOSCO_ENV_DIR=$HOME/.bosco
BOSCO_SSH_KEY_FILES=$HOME/.ssh/bosco_key.*

IS_BOSCO=`condor_config_val IS_BOSCO`
echo "$IS_BOSCO" | grep -q -i true 
if [ $? != 0 ] ; then
    echo "Please source the bosco_setenv script first."
fi

echo "Ensuring Condor is stopped..."
bosco_stop --force
if [ $? != 0 ] ; then
    echo "Failed to stop Condor, aborting."
    exit 1
fi

if [ "x$1" == "x--all" ]; then
    echo "Removing the BOSCO key and all the installed clusters."
    cluster_list=`bosco_cluster --list`
    if [ $? -eq 2 ]; then
        echo "No cluster installed"
    else
        for i in `bosco_cluster --list`; do
            echo "Removing $i"
            bosco_cluster --remove $i
        done
    fi
    rm -rf $BOSCO_ENV_DIR  
    # keys may not be there if no cluster has been installed
    rm -f $BOSCO_SSH_KEY_FILES >& /dev/null
fi


RELEASE_DIR=`condor_config_val RELEASE_DIR`
if [ ! -d $RELEASE_DIR ] ; then
    echo "BOSCO installation directory $RELEASE_DIR doesn't exist, aborting."
    exit 1
fi
echo "Removing BOSCO installation under $RELEASE_DIR"
rm -rf $RELEASE_DIR

if [ -d $RELEASE_DIR ] ; then
    echo "BOSCO installation is still in $RELEASE_DIR. Retrying the removal"
    sleep 10
    rm -rf $RELEASE_DIR
fi

echo "Done"
