#!/bin/bash
#
# distcc-config - helper script for distcc and distccd
#
# Copyright 2003 Superlucidity Services, LLC 
# This program licensed under the GNU GPL version 2.
#
# This script developed by Zachary T Welch at Superlucidity Services, LLC
#  based on ideas from irc discussion and the clear need for this support
# 
# Additional features to come; this provides a starting point


[ -z "${ROOT}" ] && ROOT=/

source /sbin/functions.sh

# this should be getopt'd someday
DCCC_VERBOSE=1

dccc_echo() {
	[ -n "${DCCC_VERBOSE}" ] && echo "$*"
}
dccc_exit() {
	dccc_echo "$*"
	exit 1
}

[ "$(id -u)" != 0 ] && dccc_exit "$0 must be run by root"

###
# the following functions manage the distcc symlinks
#  they allow the user or other scripts (namely gcc-config) to
#  automatically update distcc's links when upgrading toolchains
#
dccc_remove_link() {
	local t="${ROOT}usr/lib/distcc/bin/${1}"
	if [ -L ${t} ]; then
		dccc_echo "Removing ${t}..."
		rm -f "${t}"
	fi
}
dccc_install_link() {
	# Search the PATH for the specified compiler
	#  then create shadow link in ${ROOT}usr/lib/distcc/bin to distcc
	#  note: this `type' doesn't do the right thing if ROOT != /
	if [ -n "$(type -p ${1})" ]; then 
		# first be sure any old link is removed
		DCCC_VERBOSE="" dccc_remove_link "${1}"

		# then create the new link
		local t="${ROOT}usr/lib/distcc/bin/${1}"
		dccc_echo "Creating distcc shadow link: ${t}..."
		ln -s /usr/bin/distcc "${t}"
	fi
}
dccc_links() {
	local a
	for a in gcc cc c++ g++ ; do
		[ -n "${2}" ] && a="${2}-${a}"
		eval "dccc_${1}_link" "${a}"
	done
}

dccc_install_user() {
	# update or create, depending on whether user already exists
	einfo "Updating or creating distcc user..."
	local USERFIX
	id distcc >/dev/null 2>&1 && USERFIX=usermod || USERFIX=useradd
	USERFIX="/usr/sbin/${USERFIX}"
	if [ -x "${USERFIX}" ]
	then
		if ! ${USERFIX} -g daemon -s /bin/false -d /dev/null \
				-c "distccd" distcc 
		then
			dccc_exit "unable to create or modify user"
		fi
	elif [ "${USERFIX}" = "/usr/sbin/useradd" ]
	then
		ewarn "${USERFIX} not found: You need to add the distcc user"
		ewarn "to /etc/passwd by hand.  Add the following line:\n"
		ewarn "  distcc:x:240:2:distccd:/dev/null:/bin/false\n"
		ewarn "and then run 'distcc-config --install' again root."
		dccc_exit "Unable to create distcc user!"
	fi

	for d in ${ROOT}usr/lib/distcc/bin ${ROOT}var/run/distccd ; do
		einfo "Configuring $d..."
		chown distcc:daemon $d && chmod 0755 $d || \
			dccc_exit "Unable to configure ${d}!"
	done
}

dccc_guess_chost() {
	echo "$(grep CHOST /etc/make.conf | \
		grep -v '^#' | sed 's,^.*=\"\(.*\)\",\1,')"

}

dccc_install() {
	local xxCHOST
	dccc_install_user
	dccc_links install ""
	for f in make.conf make.globals make.profile/make.defaults
	do
		xxCHOST="$(dccc_guess_chost /etc/${f})"
		if [ -n "${xxCHOST}" ]; then
			dccc_links install "${xxCHOST}"
			break
		fi
	done
}

###
# main routine

case "${1}" in
	--install )
		dccc_install
		;;
	--install-user )
		dccc_install_user
		;;
	--install-links )
		dccc_links install "${2}"
		;;
	--remove-links )
		dccc_links remove "${2}"
		;;
	* )
		echo "usage: ${0} {--install-links|--remove-links} [ CHOST ]"
		;;
esac

