#!/bin/sh

##**************************************************************
##
## Copyright (C) 1990-2007, Condor Team, Computer Sciences Department,
## University of Wisconsin-Madison, WI.
## 
## Licensed under the Apache License, Version 2.0 (the "License"); you
## may not use this file except in compliance with the License.  You may
## obtain a copy of the License at
## 
##    http://www.apache.org/licenses/LICENSE-2.0
## 
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##
##**************************************************************



#
#  This is the condor_init script.  It creates all the necessary
#  files and directories on a host to run Condor.  
# 

# Where can I find the condor_config_val binary?
CONFIG_VAL_PATH="condor_config_val"

# Create soft link, as needed, to find the global config file.

# Be sure to get just the hostname without domain name.
HOSTNAME=`hostname | sed "s/\..*//g"`

# Find out who should own the Condor files.
OWNER=`$CONFIG_VAL_PATH -owner 2> /dev/null`
if [ ! -n "$OWNER" ]; then
   echo 'Error determining who should own the Condor-related directories.'
   echo 'Either create a "condor" account, or set the CONDOR_IDS environment'
   echo 'variable to the valid uid.gid pair that should be used by Condor.'
   exit 1
fi

# We need the -h option so we don't get an error if the local config
# file doesn't exist.
CONFIG_VAL="$CONFIG_VAL_PATH -h $HOSTNAME"

CONDOR_LOCALDIR=`$CONFIG_VAL LOCAL_DIR 2> /dev/null`
CONDOR_LOG=`$CONFIG_VAL LOG 2> /dev/null`
CONDOR_SPOOL=`$CONFIG_VAL SPOOL 2> /dev/null`
CONDOR_EXECUTE=`$CONFIG_VAL EXECUTE 2> /dev/null`
CONDOR_LOCALCFG=`$CONFIG_VAL LOCAL_CONFIG_FILE 2> /dev/null`
CONDOR_LOCK=`$CONFIG_VAL LOCK 2> /dev/null`

# Create a soft link to the global config file.

if [ -n "$CONDOR_LOCALDIR" ]; then
	if [ ! -d $CONDOR_LOCALDIR ]; then
		echo "Creating $CONDOR_LOCALDIR"
		mkdir $CONDOR_LOCALDIR
		chmod 755 $CONDOR_LOCALDIR
		chown $OWNER $CONDOR_LOCALDIR
	fi
else
	echo "LOCAL_DIR is not defined, will not create"
fi

if [ -n "$CONDOR_LOG" ]; then
	if [ ! -d $CONDOR_LOG ]; then
		echo "Creating $CONDOR_LOG"
		mkdir $CONDOR_LOG 2> /dev/null
		if [ ! -d $CONDOR_LOG ]; then
			echo "Error creating $CONDOR_LOG"
			exit 1
		fi
		chmod 755 $CONDOR_LOG
		chown $OWNER $CONDOR_LOG
	else
		echo "$CONDOR_LOG already exists."
	fi
else
	echo "LOG is not defined here, fix this and try again."
	exit 1
fi

if [ -n "$CONDOR_SPOOL" ]; then

	CONDOR_SPOOL_PARENT=`echo $CONDOR_SPOOL | sed "s/\/[a-zA-Z]*$//g"`

	if [ ! -d $CONDOR_SPOOL_PARENT ]; then
		echo "Creating $CONDOR_SPOOL_PARENT"
		mkdir $CONDOR_SPOOL_PARENT
		chmod 755 $CONDOR_SPOOL_PARENT
	fi

	if [ ! -d $CONDOR_SPOOL ]; then
		echo "Creating $CONDOR_SPOOL"
		mkdir $CONDOR_SPOOL
		chmod 755 $CONDOR_SPOOL
		chown $OWNER $CONDOR_SPOOL
	else
		echo "$CONDOR_SPOOL already exists."
	fi
else
	echo "SPOOL is not defined, will not create."
fi

if [ -n "$CONDOR_EXECUTE" ]; then

	CONDOR_EXECUTE_PARENT=`echo $CONDOR_EXECUTE | sed "s/\/[a-zA-Z]*$//g"`

	if [ ! -d $CONDOR_EXECUTE_PARENT ]; then
		echo "Creating $CONDOR_EXECUTE_PARENT"
		mkdir $CONDOR_EXECUTE_PARENT
		chmod 755 $CONDOR_EXECUTE_PARENT
	fi

	if [ ! -d $CONDOR_EXECUTE ]; then
		echo "Creating $CONDOR_EXECUTE"
		mkdir $CONDOR_EXECUTE
		chmod 755 $CONDOR_EXECUTE
		chown $OWNER $CONDOR_EXECUTE
	else
		echo "$CONDOR_EXECUTE already exists."
	fi
else
	echo "EXECUTE is not defined, will not create."
fi

if [ -n "$CONDOR_LOCALCFG" ]; then
	if [ ! -f $CONDOR_LOCALCFG ]; then
		echo "Creating $CONDOR_LOCALCFG"
		touch $CONDOR_LOCALCFG 
		chmod 644 $CONDOR_LOCALCFG
	else 
		echo "$CONDOR_LOCALCFG already exists."
	fi
else
	echo "LOCAL_CONFIG_FILE is not defined, will not create."
fi

if [ -n "$CONDOR_LOCK" ]; then
	if [ ! -d $CONDOR_LOCK -a "$CONDOR_LOCK" != "$CONDOR_LOG" ];
	then
		echo "Creating $CONDOR_LOCK"
		mkdir $CONDOR_LOCK
		chmod 755 $CONDOR_LOCK
		chown $OWNER $CONDOR_LOCK
	fi
else
	echo "LOCK is not defined, will not create."
fi

echo "Condor has been initialized, but not started."
