:
#
# @(#)mfcgicfg	7.1

if test ! -w /etc/passwd
then
        echo "$0: You must be root to run this script"
        exit 1
fi


USERCFG=usercgi.cfg
MFCGIENV=.mfcgienv

# Do we have any parameters?
case .$1 in
	.)
	   echo $0: check 
           echo "	- check usercfg.cfg for bad options, invalid directories"
	   echo
	   echo $0: list 
	   echo "	- lists usercfg.cfg"
	   echo
	   echo "$0: delete "'<name>' 
	   echo "	- deletes a user from usercfg.cfg"
	   echo
	   echo "$0: add "'<name> <directory <options>' 
	   echo "	- adds a user to usercfg.cfg"
	   echo
	   exit ;;
	*) ;;
esac


APPNAME=$0
OPTION=$1
shift
REST=$*

case .$OPTION in
	.create) ;;
	*)
# Can we find the usercgi.cfg?
		if test ! -f $USERCFG
		then
			echo $0: unable to open $USERCFG
			exit 255
		fi
		;;
esac

case $OPTION in
	list)
	OLDIFS=$IFS
	IFS=:
	echo "User	Directory	Access" | awk '{ printf "%-14s\t%-30s\t%s\n",$1,$2,$3 }'
	echo "----	--------	------" | awk '{ printf "%-14s\t%-30s\t%s\n",$1,$2,$3 }'
	echo
	cat $USERCFG | while read line
	do
		set -- $line
# skip comment
		case .`echo $1 | cut -c1` in
			.#) ;;
			*) echo "$1	$2	$3"
			;;
		esac
	done	| awk '{ printf "%-14s\t%-30s\t%s\n",$1,$2,$3 }'
	IFS=$OLDIFS

	;;


	check)
	OLDIFS=$IFS
	echo $0: Checking $USERCFG for:
	echo "	- bad configuration options"
	echo "	- missing cgi-bin directories"
	echo "	- missing .mfcgienv"
	echo
	ERRORS=no
	cat $USERCFG | while read line
	do
		IFS=:
		set -- $line
		IFS=$OLDIFS
# skip comment
		case .`echo $1 | cut -c1` in
			.#) ;;
			*) 
				USER=$1
				BASEDIR=$2
				USEROPT=$3

				case .$USEROPT in
					.access=full)       CHECKEXIST=y
							    CHECKREST=n	;;
					.access=restricted) CHECKEXIST=y
							    CHECKREST=y	;;
					.access=denied)     CHECKEXIST=n
							    CHECKREST=n	;;
					*)	echo $APPNAME: 'Warning unknown option (User='$USER', Option='$USEROPT')'
						ERRORS=yes
						;;
				esac
						            
			
				case .$CHECKEXIST in
					.y) 
					    IFS=:
					    set -- ""
				            set -f
            				    set -- `grep "^$USER:" /etc/passwd`
				            HOMEDIR=$6"/"$BASEDIR
					    MFCGIENV1=$6"/"$BASEDIR/.mfcgienv
					    MFCGIENV2=$6"/".mfcgienv
				            set +f
					    IFS=$OLDIFS
					    if test ! -d $HOMEDIR
					    then
						echo $APPNAME: cgi-bin directory for $USER does not exist
						ERRORS=yes
					    fi

					    if test ! -f $MFCGIENV1
					    then
					      if test ! -f $MFCGIENV2
					      then
						echo $APPNAME: .mfcgienv does not exist for $USER
						ERRORS=yes
                                              fi
                                            fi
					;;
				esac
			;;
		esac
	done		

	IFS=" "
	case .$ERRORS in
		.yes)	echo $APPNAME: Warning errors found in $USERCFG
			;;
		.no)	echo $APPNAME: $USERCFG passed error checking
			;;
	esac
	;;

	
	delete) USER=$REST
		
		cat $USERCFG | sed "/^$USER:*/d" >$$.tmp
		mv $$.tmp $USERCFG
		;;

	add)	IFS=" "
		set -- $REST
		USER=$1
		DIR=$2
		OPTIONS=$3

		
		case .$USER in
			.) 	echo $APPNAME: no user specified
				exit 255
				;;
		esac

		case .$DIR in
			.) 	echo $APPNAME: no directory specified
				exit 255
				;;
		esac

		case .$OPTIONS in
			.access=denied|.access=restricted|.access=full) ;;
			
			*) 	echo $APPNAME: invalid option specified
				exit 255
				;;
		esac

		IFS=:
		set -- ""
		set -f
            	set -- `grep "^$USER:" /etc/passwd`
		HOMEDIR=$6
		set +f
		IFS=" "
            	grep "^$USER:" /etc/passwd 2>&1 >/dev/null
		case $? in
			0) ;;
			*)	echo $APPNAME: no user not found is /etc/passwd
				exit 255
				;;
		esac

            	grep "^$USER:" $USERCFG 2>&1 >/dev/null
		case $? in
			0)	echo $APPNAME: Sorry $USER exists in $USERCFG
				exit 255
				;;
			*) ;;
		esac

	   	echo $APPNAME: check 
		if test ! -d $HOMEDIR/$DIR
		then
			mkdir -p $HOMEDIR/$DIR
			chown $USER $HOMEDIR/$DIR			
			echo $APPNAME: $HOMEDIR/$DIR - created
		else
			echo $APPNAME: $HOMEDIR/$DIR - already exists
		fi


		echo $APPNAME: Creating $HOMEDIR/$DIR/$MFCGIENV
		su - $USER "-c env" >$HOMEDIR/$DIR/$MFCGIENV
		chown $USER $HOMEDIR/$DIR/$MFCGIENV

		echo "# mfcgienv: added $USER, $DIR,  $OPTIONS @ `date`" >>$USERCFG
		echo "$USER:$DIR:$OPTIONS" >>$USERCFG
		echo $APPNAME: $USER has been added to $USERCFG

		echo -------------------------------------------------------
		$APPNAME check
		;;

	create)
		if test -f $USERCFG
		then
			echo $0: can not create $USERCFG as one already exists.
			exit 255
		fi
		IFS=:

		echo "# mfcgienv: create @ `date`" >$USERCFG
		cat /etc/passwd | while read line
		do
			set -- $line
			IFS=" "
			echo $1:www/cgi-bin:access=denied
			IFS=":"
		done >>$USERCFG

		IFS=" "
		echo $APPNAME: $USERCFG created, all users set to access=denied
		;;

	*)
		IFS=$OLDIFS
		echo $APPNAME: Unknown option
		exit 255
		;;
esac
