#!/bin/sh

[ -f /etc/profile.d/grid-env.sh ] && . /etc/profile.d/grid-env.sh
[ -f /etc/sysconfig/glite-lb ] && . /etc/sysconfig/glite-lb

GLITE_LOCATION=${GLITE_LOCATION:-'/usr'}
GLITE_LOCATION_ETC=${GLITE_LOCATION_ETC:-'/etc'}
GLITE_USER=${GLITE_USER:-'glite'}

GLITE_HOME=`getent passwd ${GLITE_USER} | cut -d: -f6`
GLITE_HOST_CERT=${GLITE_HOST_CERT:-"$GLITE_HOME/.certs/hostcert.pem"}
GLITE_HOST_KEY=${GLITE_HOST_KEY:-"$GLITE_HOME/.certs/hostkey.pem"}


NAME="glite-px-setup"
ACTIONS="certs crl startup"


qecho() {
	if test "$quiet" != "1"; then
		echo "$NAME: $@"
	fi
}


setup_all() {
	setup_certs=1
	setup_emir=1
	setup_crl=1
	setup_startup=1
}


while test -n "$1"; do
	case "$1" in
	-h|--help|help)
		cat <<EOF
Usage: $NAME [OPTIONS] [ACTIONS]

OPTIONS are:
	-q,--quiet ... print only errors
	-l,--list .... list actions to be executed and exit
	-h,--help .... usage

ACTIONS are:
	certs emir
	crl startup

	all ....... all actions (default)
	none ...... no actions (default for check)
	yaim ...... actions for yaim
EOF
		exit 0
		;;
	-q|--quiet)
		quiet=1
		;;
	-l|--list)
		setup_list=1
		;;
	all)
		setup_all=1
		setup_all
		;;
	none)	setup_none=1 ;;
	yaim)
		setup_all
		setup_crl=0
		;;
	certs)	setup_certs=1 ;;
	crl)	setup_crl=1 ;;
	startup) setup_startup=1 ;;
	*)
		echo "$NAME: ERROR: unknown argument '$1'"
		exit 1
		;;
	esac
	shift
done
for action in $ACTIONS all none yaim; do
	eval value=\"$`echo setup_$action`\"
	if test "$value" = "1"; then
		setup=1;
	fi
done
if test "$setup" != "1"; then
	setup_all
fi

if test "$setup_list" = "1"; then
	for action in $ACTIONS; do
		eval value=\"$`echo setup_$action`\"
		if test "$value" = "1"; then
			echo -n "$action "
		fi
	done
	echo
	exit 0
fi

if test -z "$GLITE_HOME"; then
	echo "$NAME: ERROR: The home directory of ${GLITE_USER} doesn't exist. Check whether the user ${GLITE_USER} was properly created"
	exit 2
fi


# ==== certificates ====

if test "$setup_certs" = "1"; then
	if test ! -d $GLITE_HOME/.certs; then
		mkdir -p $GLITE_HOME/.certs
		chown $GLITE_USER:$GLITE_USER $GLITE_HOME/.certs
	fi
	cp -fp /etc/grid-security/hostcert.pem /etc/grid-security/hostkey.pem $GLITE_HOME/.certs/
	if test $? -eq 0; then
		chown $GLITE_USER:$GLITE_USER $GLITE_HOME/.certs/hostcert.pem $GLITE_HOME/.certs/hostkey.pem
		chmod 0644 $GLITE_HOME/.certs/hostcert.pem
		chmod 0400 $GLITE_HOME/.certs/hostkey.pem
		qecho "Certificates copied to $GLITE_HOME/.certs/"
	else
		echo "$NAME: WARNING: Please copy host certificate and key into /etc/grid-security and"
		echo "  $GLITE_HOME/.certs/, change the owner of the ones in"
		echo "  $GLITE_HOME/.certs/ to $GLITE_USER"
		error="$error certs"
	fi
fi


# ==== fetch CRL ====

if test "$setup_crl" = "1"; then
	if test -x /usr/sbin/fetch-crl; then
		/sbin/chkconfig fetch-crl-cron on
		/sbin/chkconfig fetch-crl-boot on
		/sbin/service fetch-crl-cron start
		/sbin/service fetch-crl-boot start
	else
		echo "$NAME: WARNING: fetch-crl not found, fetching won't be configured"
	fi
fi


# ==== startup ====

if test "$setup_startup" = "1"; then
	if test ! -x $GLITE_LOCATION/bin/glite-proxy-renewd; then
		echo "$NAME: ERROR: glite-proxy-renewd not installed"
		startup_error=1
	fi
	if test "$startup_error" = "1"; then
		error="$error startup"
	fi
fi
if test "$setup_startup" = "1" -a "$startup_error" != "1"; then
	/sbin/chkconfig glite-proxy-renewald on

	startup_error=0
	/sbin/service glite-proxy-renewald restart || startup_error=1

	if test "$startup_error" = "0"; then
		qecho "Services started successfully"
	else
		echo "$NAME: ERROR: Some services failed to start"
		error="$error startup"
	fi
fi


# ==== the end ====

if test -z "$error"; then
	true
else
	echo "$NAME: ERROR: $error"
	false
fi
