#!/bin/bash

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

off_ok=0
address_file=`condor_config_val MASTER_ADDRESS_FILE`
if [ x$address_file != x -a -r $address_file ] ; then
    echo "Sending off command to condor_master."
    condor_off -master
    if [ $? != 0 ] ; then
        echo "Failed to stop HTCondor."
        off_ok=1
        #exit 1
    else
        echo "Stopped HTCondor" 
    fi
fi

# Condor is actually the only service, same as Condor is now off
#echo "BOSCO is now off."

# Adding a part to force down Condor processes left behind (usually due to some misconfiguration)
if [ "x$1" != "x--force" ]; then
  if [ $off_ok != 0 ]; then
    echo "Unable to stop BOSCO, try '$0 --force'"
    exit 1
  fi
  echo "BOSCO is now off."  
  exit 0
fi

#check that processes really exited

echoerr() { echo "$@" 1>&2; }

# Checking commands on Linux and Mac 
# (GNU ps, paths)
PS="/bin/ps"
PS_OPTS="axwww"
PS_FORMAT_NAME="o user,pid,command"
PS_FORMAT_ID="o uid,pid,command"
# AWK is /usr/bin/awk on Mac, /bin/awk and /usr/bin/awk on Linux
AWK="/usr/bin/awk"
CAT="/bin/cat"

CONDOR_USERS=""
CONDOR_UIDS=""

# Get the condor user(s)
detect_condor_users() {
  if [ "$CONDOR_USERS" != "" ]; then
    return 0
  fi
  if [ "$USER" != "root" -a "$USER" != "condor" ] ; then
    MYUSER="$USER"
  fi
  IDS=`condor_config_val CONDOR_IDS 2> /dev/null`
  if [ "$IDS" = "" ]; then
    CONDOR_USERS="condor,root,$MYUSER" 
    return 0
  fi

  TMP_USER=`echo $IDS|awk 'BEGIN{FS="[ ,.]"} /^[^0-9]/{print $1}'`
  if [ "$TMP_USER" != "" ] ; then
    CONDOR_USERS="$TMP_USER,root,$MYUSER" 
    return 0
  fi
  CONDOR_UID=`echo $IDS|$AWK 'BEGIN{FS="[ ,.]"} /^[0-9]/{print $1}'`

  for u in condor ; do
    TMP_UID=`id $u >& /dev/null |$AWK '{ sub(/uid=/,"",$1); sub(/\(.*\)/,"",$1); print $1; }'`
    if [ "$TMP_UID" = "$CONDOR_UID" ] ; then
      CONDOR_USERS="${u},root,$MYUSER" 
      return 0
    fi
  done
  
  TMP_USER=`$CAT /etc/passwd|$AWK 'BEGIN{FS=":"; UID=ARGV[1]; ARGC=0; } { if($3==UID){print $1} }' $CONDOR_UID`
  if [ "$TMP_USER" != "" ] ; then
    #echo "$TMP_USER had uid $CONDOR_UID"
    CONDOR_USERS="$TMP_USER,root,$MYUSER" #set_condor_users "$TMP_USER" "root" "$MYUSER"
    return 0
  fi

  CONDOR_USER="$CONDOR_UID"
  CONDOR_UIDS="$CONDOR_UID,0"
  return 0
}

find_masters() {
  #echoerr "Find masters ($CONDOR_USERS|$CONDOR_UIDS)"

  if [ "$CONDOR_USERS" = "" ] ; then
    OPTS="${PS_OPTS}${PS_FORMAT_ID}" 
    TMP_USERS=$CONDOR_UIDS
  else
    OPTS="${PS_OPTS}${PS_FORMAT_NAME}"
    TMP_USERS=$CONDOR_USERS
  fi
  pids=`$PS $OPTS|awk 'BEGIN{split(ARGV[1],users,","); ARGC=0; } { if( (index($0,"condor_master") != 0) && (index($0,"awk") == 0) ) { for(i in users){ if($1==users[i]){print $2; break;} } } }' $TMP_USERS`
  echo "$pids"
}

detect_condor_users

CONDOR_PIDS=`find_masters`
if [ "$CONDOR_PIDS" != "" ]; then
  echo "Waiting for the processes to quit..."
  sleep 5
  CONDOR_PIDS=`find_masters`
  if [ "$CONDOR_PIDS" != "" ]; then
    #echo "Forcing to quit processes $CONDOR_PIDS"
    for pid in `echo "$CONDOR_PIDS"` ; do
      echo "Process $pid is still running. Sending QUIT."
      kill -QUIT "$pid"
    done
  fi
  #echo "Condor is now really off."
fi

# Condor is actually the only service, same as Condor is now off
echo "BOSCO is now off."

