#!/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
#	       conf	-- /etc/pbs.conf.new file
#              client   -- dirs needed by client programs
#              server   -- dirs needed by pbs_server
#              mom      -- dirs needed by pbs_mom
#              comm	-- dirs needed by pbs_comm
#              aux      -- the aux directory
#              default  -- the default file
#              check    -- run chk_tree (turns on -c automatically)
#              all      -- do all of the above
#
#
#
# 
#
# Copyright (C) 1994-2018 Altair Engineering, Inc.
# For more information, contact Altair at www.altair.com.
#
# This file is part of the PBS Professional ("PBS Pro") software.
#
# Open Source License Information:
#
# PBS Pro is free software. You can redistribute it and/or modify it under the
# terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# PBS Pro is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.
# See the GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Commercial License Information:
#
# For a copy of the commercial license terms and conditions,
# go to: (http://www.pbspro.com/UserArea/agreement.html)
# or contact the Altair Legal Department.
#
# Altair’s dual-license business model allows companies, individuals, and
# organizations to create proprietary derivative works of PBS Pro and
# distribute them - whether embedded or bundled with other software -
# under a commercial license agreement.
#
# Use of Altair’s trademarks, including but not limited to "PBS™",
# "PBS Professional®", and "PBS Pro™" and Altair’s logos is subject to Altair's
# trademark licensing policies.
#

#
# 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_DEFAULT_DATAPORT=@PBS_DEFAULT_DATAPORT@

PBS_CONF_FILE=@PBS_CONF_FILE@
PBS_ENVIRON=@PBS_ENVIRON@
SCP_PATH=@SCP_PATH@
database_dir=@database_dir@
database_user=@database_user@
database_port=@database_port@
core_limit=@core_limit@

PBS_UBIN=@bindir@
PBS_SBIN=@sbindir@
PBS_ETC=@etcdir@
PBS_LIB=@libdir@
PBS_UINC=@includedir@
PBS_UNSUP=@unsupporteddir@

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="${IROOT}$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=${IROOT}`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 ${IROOT}$PBS_ENVIRON ] ; then
		install_dir `dirname $PBS_ENVIRON`
		echo "PATH=/bin:/usr/bin" > ${IROOT}$PBS_ENVIRON
		if [ X$TZ != X ] ; then
			echo "TZ=$TZ" >> ${IROOT}$PBS_ENVIRON
		fi
		if [ X$LANG != X ] ; then
			echo "LANG=$LANG" >> ${IROOT}$PBS_ENVIRON
		fi
		if [ X$LC_ALL != X ] ; then
			echo "LC_ALL=$LC_ALL" >> ${IROOT}$PBS_ENVIRON
		fi
		if [ X$LC_COLLATE != X ] ; then
			echo "LC_COLLATE=$LC_COLLATE" >> ${IROOT}$PBS_ENVIRON
		fi
		if [ X$LC_CTYPE != X ] ; then
			echo "LC_CTYPE=$LC_CTYPE" >> ${IROOT}$PBS_ENVIRON
		fi
		if [ X$LC_MONETARY != X ] ; then
			echo "LC_MONETARY=$LC_MONETARY" >> ${IROOT}$PBS_ENVIRON
		fi
		if [ X$LC_NUMERIC != X ] ; then
			echo "LC_NUMERIC=$LC_NUMERIC" >> ${IROOT}$PBS_ENVIRON
		fi
		if [ X$LC_TIME != X ] ; then
			echo "LC_TIME=$LC_TIME" >> ${IROOT}$PBS_ENVIRON
		fi
		chmod 644 ${IROOT}$PBS_ENVIRON
	fi
}

make_momconfig()
{
	test -n "$verbose" && echo Creating Moms config file
	if [ ! -f ${IROOT}$PBS_SERVER_HOME/mom_priv/config ] ; then
		echo '$logevent 0x1ff' > ${IROOT}$PBS_SERVER_HOME/mom_priv/config
		echo \$clienthost $PBS_DEFAULT_SERVER \
			>> ${IROOT}$PBS_SERVER_HOME/mom_priv/config
		chmod 644  ${IROOT}$PBS_SERVER_HOME/mom_priv/config
	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_LIB/pm  755 $owner
	install_dir_by_varname PBS_UINC 755 $owner
	install_dir_by_varname PBS_ETC 755 $owner
	install_dir_by_varname PBS_UNSUP 755 $owner

	chk_tree_wrap -d -n ${IROOT}$PBS_UBIN ${IROOT}$PBS_SBIN \
			    ${IROOT}$PBS_LIB ${IROOT}$PBS_LIB/pm \
			    ${IROOT}$PBS_UINC ${IROOT}$PBS_ETC \
			    ${IROOT}$PBS_UNSUP		
}

mk_comm_dirs()
{
	test -n "$verbose" && echo Creating Directories required for Communication
	install_dir $PBS_SERVER_HOME/comm_logs 755 $owner
}


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/users 750 $owner
	install_dir $PBS_SERVER_HOME/server_priv/hooks 750 $owner
	install_dir $PBS_SERVER_HOME/server_priv/hooks/tmp 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 ${IROOT}$PBS_ENVIRON || check_env

	chk_tree_wrap -d -n ${IROOT}$PBS_SERVER_HOME/server_priv/jobs \
		${IROOT}$PBS_SERVER_HOME/server_priv/users \
		${IROOT}$PBS_SERVER_HOME/server_priv/hooks \
		${IROOT}$PBS_SERVER_HOME/server_priv/hooks/tmp \
		${IROOT}$PBS_SERVER_HOME/server_priv/accounting \
		${IROOT}$PBS_SERVER_HOME/server_logs
	chk_tree_wrap -d -n -s ${IROOT}$PBS_SERVER_HOME/spool
	chk_tree_wrap -n ${IROOT}$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 ${IROOT}$PBS_SERVER_HOME/spool || \
	    install_dir ${IROOT}$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_priv/hooks 750 $owner
	install_dir $PBS_SERVER_HOME/mom_priv/hooks/tmp 750 $owner
	install_dir $PBS_SERVER_HOME/mom_logs 755 $owner

	test -f ${IROOT}$PBS_ENVIRON || check_env

	test -f ${IROOT}$PBS_SERVER_HOME/mom_priv/config || make_momconfig

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

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

	install_dir `dirname ${IROOT}$PBS_DEFAULT_FILE`

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

	chk_tree_wrap -n ${IROOT}$PBS_DEFAULT_FILE
}

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

do_check_tree()
{
	chk_tree_wrap -d ${IROOT}$PBS_SERVER_HOME/server_priv/jobs \
		${IROOT}$PBS_SERVER_HOME/server_priv/users \
		${IROOT}$PBS_SERVER_HOME/server_priv/hooks \
		${IROOT}$PBS_SERVER_HOME/server_priv/hooks/tmp \
		${IROOT}$PBS_SERVER_HOME/server_priv/accounting \
		${IROOT}$PBS_SERVER_HOME/server_logs
	chk_tree_wrap -d ${IROOT}$PBS_SERVER_HOME/checkpoint \
		${IROOT}$PBS_SERVER_HOME/mom_priv/jobs \
		${IROOT}$PBS_SERVER_HOME/mom_priv/hooks \
		${IROOT}$PBS_SERVER_HOME/mom_priv/hooks/tmp \
		${IROOT}$PBS_SERVER_HOME/mom_logs
	chk_tree_wrap -d -s ${IROOT}$PBS_SERVER_HOME/spool \
		${IROOT}$PBS_SERVER_HOME/undelivered
	chk_tree_wrap -d ${IROOT}$PBS_SERVER_HOME/aux
	chk_tree_wrap ${IROOT}$PBS_DEFAULT_FILE
}

# mk_conf_file() will make /etc/pbs.conf.new with basic info
# as server/remom files are added the start entries for those daemons are added

mk_conf_file()
{
	test -n "$verbose" && echo Making global config file
	install_dir `dirname ${IROOT}${PBS_CONF_FILE}`
	if [ -f ${IROOT}${PBS_CONF_FILE} ] ; then
		target="${IROOT}${PBS_CONF_FILE}.`date +%s`"
		echo Moving existing ${IROOT}${PBS_CONF_FILE} to ${target}
		mv ${IROOT}${PBS_CONF_FILE} ${target}
	fi
	echo PBS_EXEC=$exec_prefix > ${IROOT}${PBS_CONF_FILE}
	echo PBS_HOME=$PBS_SERVER_HOME >>  ${IROOT}${PBS_CONF_FILE}
	echo PBS_START_SERVER=0 >> ${IROOT}${PBS_CONF_FILE}
	echo PBS_START_MOM=0 >> ${IROOT}${PBS_CONF_FILE}
	echo PBS_START_SCHED=0 >> ${IROOT}${PBS_CONF_FILE}
	echo PBS_START_COMM=0 >> ${IROOT}${PBS_CONF_FILE}
	echo PBS_SERVER=$PBS_DEFAULT_SERVER >> ${IROOT}${PBS_CONF_FILE}
	echo PBS_ENVIRONMENT=$PBS_ENVIRON >> ${IROOT}${PBS_CONF_FILE}
        echo PBS_CORE_LIMIT=$core_limit >> ${IROOT}${PBS_CONF_FILE}
	if [ -n "$SCP_PATH" ] ; then
		echo PBS_SCP=$SCP_PATH >> ${IROOT}${PBS_CONF_FILE}
	fi
	if [ "$database_port" != "$PBS_DEFAULT_DATAPORT" ]; then
		echo PBS_DATA_SERVICE_PORT=$database_port >> ${IROOT}${PBS_CONF_FILE}
	fi
	chmod 644 ${IROOT}${PBS_CONF_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_comm_dirs
	mk_default_file
	mk_aux_dir
}


while true; do
	case "$1" in
	    -v)       verbose=yes ; shift; continue ;;
	    -c)       chktree=yes ; shift; continue ;;
	    -d)	      shift; PBS_SERVER_HOME=$1; shift; continue;;
	    conf)     mk_conf_file ; break ;;
	    cli*)     mk_cli_dirs ; break ;;
	    serv*)    mk_server_dirs ; break ;;
	    mom*)     mk_mom_dirs ; break ;;
	    com*)     mk_comm_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

