#!/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

if [ `uname` = Darwin ] ; then
    MD5SUM=md5
else
    MD5SUM=md5sum
fi


# Generate the password file, if it doesn't already exist 
bosco_passwd_file=`condor_config_val SEC_PASSWORD_FILE`
if [ ! -e $bosco_passwd_file ]; then
    random_string=`echo $RANDOM | $MD5SUM | awk '{print $1}'`
    condor_store_cred -p $random_string -f $bosco_passwd_file

    # Change the ownership of the normal passwdfile to the user in CONDOR_IDS
    chown `condor_config_val CONDOR_IDS | sed 's/\./:/'` $bosco_passwd_file
fi

if [ ! -e $bosco_passwd_file.daemon ]; then
    cp $bosco_passwd_file $bosco_passwd_file.daemon
fi

# Check file ownership of the passwd file, and change if possible
if [ `uname` = Darwin ] ; then
    daemon_owner=`stat -f %Su $bosco_passwd_file.daemon`
else
    daemon_owner=`stat -c %U $bosco_passwd_file.daemon`
fi
if [ "$daemon_owner" != `whoami` ]; then
    chown `whoami`: $bosco_passwd_file.daemon
fi

condor_master
if [ $? == 0 ] ; then
    echo "BOSCO Started"
else
    echo "ERROR: BOSCO not started."
fi

