#!/bin/bash
#
# $Id: cdd-user,v 1.4 2004/04/15 23:16:43 kalfa Exp $

usage () {
   echo "Usage:   `basename $0` <action> <CDD> <user> [<Role>]"
   echo	"action:  add|del"
   echo "CDD:     `getCDDList|tr ' ' '|'`"
   echo "user:    user of the system who should be added to the CDD"
   echo "Role:    a registered Role for specified CDD" 
   echo "          (default: the one named like CDD)"
}


# the base dir for CDD conffiles, where script expects to find dirs named like
# each registered CDDs
CONFBASE=${CONFBASE:-/etc/cdd}

# a local per CDD conf is sourced later, after argoument parsing
source ${CONFBASE}/cdd.conf


# Check consistency of passed argouments
if [ $# -eq 0 ] ; then
   usage
   exit 67 # EX_USAGE
fi
if [ "`toLower $1`" != "add" -a "`toLower $1`" != "del" ] ; then
   echo "Missing or wrong action name."
   echo
   usage
   exit 67 # EX_USAGE
fi
if [ -z "$2" ] ; then
   echo "Missing cdd name."
   echo
   usage
   exit 67 # EX_USAGE
fi
if [ -z "$3" ] ; then
   echo "Missing user name."
   echo
   usage
   exit 67 # EX_USAGE
fi

ACTION=$1
CDD=$2
CDDUSER=$3
ROLE=${4:-${CDD}}

# Now that we know the selected CDD, we can check if there is a local
# configuration for it (ie differnt backend from the default one)
test -n "${CDD}" -a  -f ${CONFBASE}/${CDD}/${CDD}.conf &&
	source ${CONFBASE}/${CDD}/${CDD}.conf

if [ -n "${DBBACKEND}" ]; then
	set -e
	checkCDD ${CDD} || cddFail $? "Custom distribution ${CDD} does not exist"
	checkUser ${CDDUSER} || cddFail $? "User ${CDDUSER} does not exist"
	checkRole ${ROLE} || cddFail $? "Role ${ROLE} does not exist"
	checkRoleInCDD ${CDD} ${ROLE} || \
			cddFail $? "CDD (${CDD}) and Role (${ROLE}) are not correct or incompatible with the selected backend"

	if [ "`toLower ${ACTION}`" = "add" ]; then
		setUserRole ${CDD} ${CDDUSER} ${ROLE} || \
			cddFail $? "Failed to set user ${CDDUSER} to role ${ROLE}"
	elif [ "`toLower ${ACTION}`" = "del" ]; then
		unsetUserRole ${CDD} ${CDDUSER} ${ROLE} || \
			cddFail $? "Failed to unset user ${CDDUSER} to role ${ROLE}"
	fi	

#	# MENU installation
#
#	# All CDD menu entries will go in .cdd-menu, only one file will goes in
#	# .menu, so it's easy to remove/update
#	UHOME=`getUserHome ${CDDUSER}`
#	if [ ! -d "${UHOME}" ] ; then
#	   logFail 67  "Home directory for user ${CDDUSER} does not exist.\n... Sorry can not update menus."
#	fi
#	
#	if [ ! -d ${UHOME}/.menu ] ; then
#	   mkdir -p ${UHOME}/.menu
#	   chown ${CDDUSER}: ${UHOME}/.menu
#	fi
#
#	if [ ! -d ${UHOME}/.cdd-menu ] ; then
#	   mkdir -p ${UHOME}/.cdd-menu
#	   chown ${CDDUSER}: ${UHOME}/.cdd-menu
#	fi
#
#	MENUS=""	
#	if [ -d ${CONFBASE}/${CDD} ] ; then
#	   if [ -d ${CONFBASE}/${CDD}/menu ] ; then
#	      for MENU in `ls ${CONFBASE}/${CDD}/menu` ; do
#	         cp -a ${CONFBASE}/${CDD}/menu/${MENU} ${UHOME}/.cdd-menu
#	         chown ${CDDUSER}: ${UHOME}/.cdd-menu/${MENU}
#			 MENUS="${MENUS} ${MENU}"
#	      done
#	   fi
#	fi
#	
#	for MENU in ${MENUS}; do
#		echo '!include'" ${UHOME}/.cdd-menu/${MENU}"
#	done > ${UHOME}/.menu/cdd-menu

	set +e
else
	# EX_USAGE
	cddFail 67 "You choose to not use Roles"
fi
