#!/bin/sh

# Usage: fixenv <user> <editor>

# This little script takes care of setting directory permissions and login
# files correctly.

# Location of programs and files
GREP=/usr/bin/grep
CUT=/usr/bin/cut
CHMOD=/bin/chmod
PASSWD=/etc/passwd

# Shell files to modify (see case statement below)
SHELL_FILES=".bash_profile .cshrc .xenmenurc"

# Make sure 2 arguments were passed (user name and editor)
if [ X$1 = "X" -o X$2 = "X" ]; then
	exit
fi

# Make sure the user home directory was created
HOMEDIR=`$GREP ^$1: $PASSWD | $CUT -f6 -d':'`
if [ X$HOMEDIR = "X" -o ! -e $HOMEDIR ]; then
	exit
fi

EDITOR=$2

# Set the default directory permissions.
chmod 755 $HOMEDIR
if [ -e $HOMEDIR/.cfdir ]; then
	chmod 711 $HOMEDIR/.cfdir
	if [ -e $HOMEDIR/.cfdir/.cfonce ]; then
		chmod 644 $HOMEDIR/.cfdir/.cfonce
	fi
fi

# Set the appropiate shell login files.  We modify them all in case the user
# changes their shell later.
for FILE in $SHELL_FILES; do
	case $FILE in
	.bash_profile)
		echo "EDITOR=$EDITOR ; export EDITOR" >> $HOMEDIR/$FILE
		;;
	.cshrc)
		echo "setenv EDITOR $EDITOR" >> $HOMEDIR/$FILE
		;;
	.xenmenurc)
		echo "EDITOR $EDITOR" >> $HOMEDIR/$FILE
		;;
	esac
done
