#!/bin/sh

#
# pbs_mkdirs:
#
# This will make the various directories that are needed for PBS
# housekeeping.
#
# Usage:
#  pbs_mkdirs [-v] [-c] what
#    -v     be verbose
#    -c     allow usage of chk_tree (do not use this within a make install)
#    what   select from the following list
#              client   -- dirs needed by client programs
#              server   -- dirs needed by pbs_server
#              mom      -- dirs needed by pbs_mom
#              aux      -- the aux directory
#              default  -- the default file
#              check    -- run chk_tree (turns on -c automatically)
#              all      -- do all of the above
#
#
# $Id: pbs_mkdirs.in,v 2.0 1998/10/14 22:46:29 hender Exp $
#

#
# check tree should not be called until it is installed since
# only relative paths are used by autoconf.
#
prefix=@prefix@
exec_prefix=@exec_prefix@
CHK_TREE=@bindir@/chk_tree

PBS_SERVER_HOME=@PBS_SERVER_HOME@
PBS_DEFAULT_FILE=@PBS_DEFAULT_FILE@
PBS_DEFAULT_SERVER=@PBS_DEFAULT_SERVER@

PBS_ENVIRON=$PBS_SERVER_HOME/pbs_environment

#
# manual page directories are created by doc/ers/Makefile
#
PBS_UBIN=@bindir@
PBS_SBIN=@sbindir@
PBS_LIB=@libdir@
PBS_UINC=@includedir@

verbose=""


#
# this will create a directory and any chain of parent directories
# that might be needed
# All of this is somewhat lame because install-sh is used
# along with install_dir in various places. 
#
install_dir()
{
	# use the id_ for namespace disambiguation
	id_dname="$1"
	id_mode=`test -n "$2" && echo "$2" || echo 755`
	id_owner=`test -n "$3" && echo "$3" || echo root`

	# dlist will contain a list of directory names to make
	id_dlist=""

	while [ ! \( -z "$id_dname" -o "$id_dname" = / -o \
		"$id_dname" = "." \) ]; do

		test -f $id_dname && return 1
		test -d $id_dname && break

		id_dlist="`basename $id_dname` $id_dlist"
		id_dname=`dirname "$id_dname"`
	done

	id_dir="$id_dname"
	for id_d in $id_dlist ""; do
		id_dir="$id_dir/$id_d"
		test -z "$id_d" && return 0
		mkdir $id_dir && chmod $id_mode $id_dir && \
		    chown $id_owner $id_dir || return 1
	done
	return 0
}

#
# This will take the name of the variable (not value) and
# make a directory. This is just to stick with the original
# way things were done in the top-level make file in PBS
#
install_dir_by_varname()
{
	idv_dvarname="$1"
	idv_dname=`eval 'echo $'$idv_dvarname`

	idv_mode=`test -n "$2" && echo "$2" || echo 755`
	idv_owner=`test -n "$3" && echo "$3" || echo root`

	if [ X$idv_dname = X ] ; then
		echo $idv_dvarname is not defined 1>&2
		return 1
	elif [ -f $idv_dname ] ; then
		echo $idv_dvarname exists and is not a directory 1>&2
		return 1
	elif [ -d $idv_dname ] ; then
		chmod $idv_mode $idv_dname > /dev/null 2>&1
		chown $idv_owner $idv_dname > /dev/null 2>&1
		return 0
	elif [ ! -d $idv_dname ] ; then
		test -n "$verbose" && echo Creating $idv_dname
		install_dir `dirname $idv_dname` 755 $idv_owner && \
		    mkdir $idv_dname && chmod $idv_mode $idv_dname && \
		    chown $idv_owner $idv_dname || return 1
	fi

	return 0
}

check_env()
{
	test -n "$verbose" && echo Making environment file
	if [ ! -f $PBS_ENVIRON ] ; then
		install_dir `dirname $PBS_ENVIRON`
		echo "PATH=/bin:/usr/bin" > $PBS_ENVIRON
		if [ X$TZ != X ] ; then
			echo "TZ=$TZ" >> $PBS_ENVIRON
		fi
		chmod 644 $PBS_ENVIRON
	fi
}

chk_tree_wrap()
{
	test -n "$chktree" && $CHK_TREE $*
}

mk_cli_dirs()
{
	test -n "$verbose" && echo Creating Directories required for clients
	install_dir_by_varname PBS_UBIN 755 $owner
	install_dir_by_varname PBS_SBIN 755 $owner
	install_dir_by_varname PBS_LIB  755 $owner
	install_dir_by_varname PBS_UINC 755 $owner

	chk_tree_wrap -d -n $PBS_UBIN $PBS_SBIN $PBS_LIB $PBS_UINC 
}


mk_server_dirs()
{
	test -n "$verbose" && echo Creating Directories required for the Server
	install_dir_by_varname PBS_SERVER_HOME 755 $owner

	install_dir $PBS_SERVER_HOME/spool 1777 $owner
	install_dir $PBS_SERVER_HOME/server_priv 750 $owner
	install_dir $PBS_SERVER_HOME/server_priv/jobs 750 $owner
	install_dir $PBS_SERVER_HOME/server_priv/queues 750 $owner
	install_dir $PBS_SERVER_HOME/server_priv/acl_svr 750 $owner
	install_dir $PBS_SERVER_HOME/server_priv/acl_hosts 750 $owner
	install_dir $PBS_SERVER_HOME/server_priv/acl_users 750 $owner
	install_dir $PBS_SERVER_HOME/server_priv/acl_groups 750 $owner
	install_dir $PBS_SERVER_HOME/server_priv/accounting 755 $owner
	install_dir $PBS_SERVER_HOME/server_logs 755 $owner
	install_dir $PBS_SERVER_HOME/sched_priv 750 $owner
	install_dir $PBS_SERVER_HOME/sched_logs 755 $owner

	test -f $PBS_ENVIRON || check_env

	chk_tree_wrap -d -n $PBS_SERVER_HOME/server_priv/jobs \
		$PBS_SERVER_HOME/server_priv/queues \
		$PBS_SERVER_HOME/server_priv/acl_svr \
		$PBS_SERVER_HOME/server_priv/acl_hosts \
		$PBS_SERVER_HOME/server_priv/acl_users \
		$PBS_SERVER_HOME/server_priv/acl_groups \
		$PBS_SERVER_HOME/server_priv/accounting \
		$PBS_SERVER_HOME/server_logs
	chk_tree_wrap -d -n -s $PBS_SERVER_HOME/spool
	chk_tree_wrap -n $PBS_ENVIRON
}


mk_mom_dirs()
{
	test -n "$verbose" && echo Creating Directories required for MOM

	install_dir_by_varname PBS_SERVER_HOME 755 $owner

	test -d $PBS_SERVER_HOME/spool || \
	    install_dir $PBS_SERVER_HOME/spool 1777 $owner

	install_dir $PBS_SERVER_HOME/checkpoint 700 $owner
	install_dir $PBS_SERVER_HOME/undelivered  1777 $owner
	install_dir $PBS_SERVER_HOME/mom_priv 751 $owner
	install_dir $PBS_SERVER_HOME/mom_priv/jobs 751 $owner
	install_dir $PBS_SERVER_HOME/mom_logs 755 $owner

	test -f $PBS_ENVIRON || check_env

	chk_tree_wrap -d -n $PBS_SERVER_HOME/checkpoint \
		$PBS_SERVER_HOME/mom_priv/jobs \
		$PBS_SERVER_HOME/mom_logs
	chk_tree_wrap -d -n -s $PBS_SERVER_HOME/spool \
		$PBS_SERVER_HOME/undelivered
	chk_tree_wrap -n $PBS_ENVIRON
}


mk_default_file()
{
	test -n "$verbose" && echo Making default server file

	install_dir `dirname $PBS_DEFAULT_FILE`

	rm -f $PBS_DEFAULT_FILE > /dev/null 2>&1
	echo $PBS_DEFAULT_SERVER > $PBS_DEFAULT_FILE && \
	    chmod 644 $PBS_DEFAULT_FILE

	chk_tree_wrap -n $PBS_DEFAULT_FILE
}

mk_aux_dir()
{
	install_dir $PBS_SERVER_HOME/aux 755
	chk_tree_wrap -d -n $PBS_SERVER_HOME/aux
}

do_check_tree()
{
	chk_tree_wrap -d $PBS_SERVER_HOME/server_priv/jobs \
		$PBS_SERVER_HOME/server_priv/queues \
		$PBS_SERVER_HOME/server_priv/acl_svr \
		$PBS_SERVER_HOME/server_priv/acl_hosts \
		$PBS_SERVER_HOME/server_priv/acl_users \
		$PBS_SERVER_HOME/server_priv/acl_groups \
		$PBS_SERVER_HOME/server_priv/accounting \
		$PBS_SERVER_HOME/server_logs
	chk_tree_wrap -d $PBS_SERVER_HOME/checkpoint \
		$PBS_SERVER_HOME/mom_priv/jobs \
		$PBS_SERVER_HOME/mom_logs
	chk_tree_wrap -d -s $PBS_SERVER_HOME/spool \
		$PBS_SERVER_HOME/undelivered
	chk_tree_wrap -d $PBS_SERVER_HOME/aux
	chk_tree_wrap $PBS_DEFAULT_FILE
}


# mk_dirs() will generate the set of directories needed 
# for server and mom execution.

mk_dirs()
{
	chktree=yes
	mk_cli_dirs
	mk_server_dirs
	mk_mom_dirs
	mk_default_file
	mk_aux_dir
}

while true; do
	case "$1" in
	    -v)       verbose=yes ; shift; continue ;;
	    -c)       chktree=yes ; shift; continue ;;
	    cli*)     mk_cli_dirs ; break ;;
	    serv*)    mk_server_dirs ; break ;;
	    mom*)     mk_mom_dirs ; break ;;
	    aux*)     mk_aux_dir ; break ;;
	    default*) mk_default_file ; break ;;
	    check*)   chktree=yes; do_check_tree ; break ;;
	    all*)     mk_dirs ; break ;;
	esac
done

