#!/bin/sh
# script file to create multiple user account
# 
# copyright 1987 W. Paul Zola

# All rights reserved.  Permission is given to copy this
# software, as long as as long as this notice of copyright and
# permission to copy is included.  No warranty, expressed or implied, is
# given to users of this software.

# This shell script is designed to re-configure a standard
# Unix account so that more than one inidividual can use it
# comfortably and with their own environment.  

	# variables

CSHFILE=
DIREC=
LARGEC=
LOGFILE=
NAME=
SMALLC=

	# file names

CSHRC=.cshrc 
LOGIN=.login 
MASKOBJ=$HOME/.mask
MASKSRC=$HOME/#mask.c
MYCUT=$HOME/#cut
MYCUTSRC=$HOME/#cut.c
USERFILE=$HOME/users$$

clear ; cd
echo -n "
Hello $USER, and welcome to $0!   This shell script is
designed to modify your account so that more than one person can use a
single account, with each individual working out of a different home
directory.  

When done, there will be a modified login procedure for this account.
Your $LOGIN file will prompt for a name.  Based on this name, the
person logging in will be placed into their own sub-directory.  For all
intents and purposes, this directory will be their home directory.

In order to achive this, $0 will modify the contents of the file
\"$LOGIN\" in your home directory, and will create the appropriate
number of sub-directories in your account.  This script has been fully
tested, and the changes it makes are designed to be harmless.  However,
mistakes are possible, and damage to your account might result from
them.

Please read the instructions carefully and be sure to follow them.

press return to continue --> "
read NAME 	# dummy read to pause script
clear

echo -n "
In order to work properly, this script will need two lists from you.
You will be asked to enter these lists one entry at a time.

The first list is a list of \"user names\".  The \"user name\" is the
name that a person will give upon logging in order to place themselves
in their own home directory.  Typically, this list consists of the
first names of the people who will be using the account.

These names canNOT contain spaces or tabs.  All of these names MUST
begin with a different letter.  Two user names beginning with the same
letter will cause the login process to behave incorrectly.  If you
your user list has names with duplicate first letters, abort the script
with ^C and try again.

press return to continue --> "
read NAME 	# dummy read to pause script
clear

echo -n "
The second list is a list of new home directories.  The directory names
must be distinct - it is not possible for two user names to be linked
to the same home directory.  These names cannot contain spaces or
tabs.  A name suitable as a login would be suitable for this list.

You will be prompted for these directory names one at a time.  The
prompt will indicate the user name, and ask for a directory name. 

press return to continue --> "
read NAME 	# dummy read to pause script
clear

echo -n "
This script may not work correctly if you have a cshell mask in your
$LOGIN file.  If you have a mask, abort the script, remove the mask
from the $LOGIN file, and try again.  This script allows each user to
have an inidvidual mask.  After you are done, you can edit the $LOGIN
file in each new home directory at the line above the one that says
 \"# insert cshell mask here ^^^^^^\"

press return to continue --> "
read NAME 	# dummy read to pause script
clear

echo -n "
As the script runs, it will give you indications of what it
is doing.

press return to continue --> "
read NAME 	# dummy read to pause script
clear

echo "Checking to make sure that I can do this without harming
anything.....
"

	# check to make sure that all the necessary files exist

if [ ! -r $HOME/$LOGIN ] ; then
    echo "Uh, oh, I can't find the file $HOME/$LOGIN 
    .. I quit";
    exit 1 
fi
if [ ! -r $HOME/$CSHRC ] ; then
    echo "Uh, oh, I can't find the file $HOME/$CSHRC 
    .. I quit";
    exit 1 
fi
	# check to make sure that we are using an un-modified
	# .login file as the template

if [ -f $HOME/${LOGIN}.OLD ] ; then
    echo "It looks like you have run this script before.  Move the file
${LOGIN}.OLD back to ${LOGIN} , and try again.  In the meantime, 
	.. I quit";
    exit 1 
fi
	# make sure not to over-write any existing files 
	# while creating object files

if [ -f $MASKOBJ ] ; then
    echo "You seem to have a file called $MASKOBJ .
This just won't do. I need that file. 
You'll just have to quit, rename it, and start over. 
Bye!
"
    exit 1
fi

echo "Well, everything _looks_ O.K. from here.

"

# set up clean exit with ^C

trap 'echo "
Ouch, that hurt!
";rm -f $USERFILE $MYCUT;exit 1' 1 2 3 14 15

trap 'rm -f $USERFILE $MYCUT' 0

echo "Excuse me, but I'm going to have to create a utility program ...."
cat > $MYCUTSRC << 'END_OF_TEXT'

#include <stdio.h>

main(argc)
int argc;
{
register int c;
    if (argc>1) {
	getchar();
	while ((c=getchar()) != EOF)
	    putchar(c);
    } else
	putchar(getchar());
}

END_OF_TEXT

echo "compiling $MYCUTSRC ..."

if cc -o $MYCUT $MYCUTSRC ; then
    echo "Well, that went O.K. ..."
else
    ERROR=$?
    echo "WHAT!? - the compile failed, exit status $ERROR"
    exit 1
fi
rm $MYCUTSRC

cat << END_OF_TEXT
 
This shell script will need two lists from you:  the first
list will consist of user names for this account, and the second list
will consist of the actual names of the alternate home directories.
For now, just enter user names, one per line, with no spaces. When you
have entered them all, type ^D.

END_OF_TEXT

#  get the names of the users and the directories they 
#  will be residing in

echo -n "Enter the name of someone who will be using this account
--> "
while read NAME ; do
   NAME=`echo "$NAME" | tr A-Z a-z`	       # strip capitals
   firstc=`echo "$NAME" | $MYCUT | tr a-z A-Z` # capitalize first letter
   remain=`echo "$NAME" | $MYCUT -`	# get rest of name
   NAME=$firstc$remain			# create capitalized name
   set $* $NAME				
# make list of names by appending current name at the end of the
# existing positional paramaters.
   echo -n "Enter a different name, or ^D to end the list
--> "					 # prompt for next name
done

if [ $# -eq 0 ] ; then  	# no names entered in while loop
    echo "
Wait just a second!  
How can you expect me to create new directories for non-entities?  
I need NAMES!  This just won't do.  
I quit!
"
    exit 1
fi

echo "
These are the names that you gave me for the alternate users
"
echo "$*"
echo -n "
It is very important that there be no duplicaton in this
list.  Specifically, no two user names can begin with the same
letter.  If any two user names begin with the same letter, or
if this list does not look right for any reason, abort the
script with a ^C followed by a return, and try again.   
Otherwise, just press return to continue.

press return to continue --> "
read NAME  		# dummy read to pause script

for NAME in $* ; do
    echo -n "enter the name of the new home directory for user $NAME
--> "
    read DIREC
    if [ "$DIREC" ] ; then    		# don't die on blank lines
	echo "$NAME $DIREC" >> $USERFILE
    else
	echo "There must be a directory name for every user name"
	echo "Aborting Script."
	exit 1
    fi
done

echo "
These are the names of the alternate users and their new home
directories.
"
sed -e "s/ /	/g" $USERFILE
echo -n "
If this list does not look right, abort this script with ^C followed
by a return, and try again.  Otherwise, just press return to continue.

press return to continue --> "
read NAME

# make sub-directories and set them up
echo "
Creating directories....."

{
while read NAME DIREC ; do
    LOGFILE=$HOME/$DIREC/$LOGIN
    CSHFILE=$HOME/$DIREC/$CSHRC
    if [ -d $HOME/$DIREC ] ; then
	echo "woops! it looks like the directory $DIREC exists already
I'm just going to ignore the directory $DIREC for the user $NAME
Error condition 3." 
	continue
    elif mkdir $HOME/$DIREC ; then
 	cat >> $HOME/$DIREC/READ.ME << END_OF_TEXT

	      Welcome to account $DIREC, $NAME.
 
     This account has been set up so that a number of people can use it,
     each with their own home directory.  Each home directory has its 
     own .login and .cshrc file, which can be customized individually.
     There is, however, only one .usrrc and one mailbox for the account.

     You are currently in your personal home directory, 
     $HOME/$DIREC.  

     If you want to customize your .login or .cshrc file, please
     customize the ones in this directory.  The .login for this
     directory is set up so that a .alias file in this directory will
     be sourced automatically.
 
END_OF_TEXT
    cat >$LOGFILE << END_OF_TEXT
setenv HOME $HOME/$DIREC
printenv COVER > /dev/null
if (\$status && \$USER == "$USER") then
    setenv COVER 1
    if (-x $MASKOBJ) \\
	exec $MASKOBJ /bin/csh -csh
    # insert cshell mask here  ^^^^^^^^^^
endif
END_OF_TEXT
    sed -e "/setenv HOME/d" >> $LOGFILE < $HOME/$LOGIN
    echo "if ( -e .alias ) source .alias" >> $LOGFILE
    sed -e "/setenv HOME/d" < $HOME/$CSHRC > $CSHFILE
    echo "Through creating directory $DIREC ..."
    else 
	echo "error: $0: could not create $DIREC "
	echo "Aborting Script."
	exit 1
    fi
    
done
} <$USERFILE

trap 'echo "Aborting script."; rm -f $USERFILE $MASKSRC \
$MYCUT $MASKOBJ; exit 1 ' 1 2 3 14 15
    
echo "creating source file $MASKSRC "
cat >$MASKSRC <<'EOMASK'

#include <stdio.h>

main(argc, argv)
int argc;
char **argv;
{
    int i;
	if(argc == 1) 
	    exit(1);
	execv(argv[1], argv+2, 0);
	printf("GASP, ACK, ARGH!!\n");
}

EOMASK

echo "compiling $MASKSRC "

if cc -o $MASKOBJ $MASKSRC ; then
    echo "done"
else
    ERROR=$?
    echo "compile failed, exit status $ERROR"
    exit 1
fi
rm $MASKSRC

LOGFILE=$HOME/$LOGIN
mv $LOGFILE ${LOGFILE}.OLD

echo "modifying $LOGIN file in root directory"

cat >$LOGFILE << 'END_OF_TEXT'
#
echo ""
prompt:
echo ""
echo -n "Who is this? (? for help): "
set resp = $<
switch ("$resp")
END_OF_TEXT
{
while read NAME DIREC ; do
    LARGEC=`echo $NAME | $MYCUT `
    SMALLC=`echo $LARGEC | tr A-Z a-z`
    cat >> $LOGFILE << END_OF_TEXT
    case [$LARGEC$SMALLC]*:
	if (-r $HOME/$DIREC/.login) \\
	    source $HOME/$DIREC/.login
	echo "$DIREC login file not found"
	breaksw
END_OF_TEXT
done
} < $USERFILE

cat >> $LOGFILE << 'END_OF_TEXT'
    case "?":
	echo ""
	echo "	Enter one of "
END_OF_TEXT
{
while read NAME DIREC; do
    cat >> $LOGFILE << END_OF_TEXT
        echo "		$NAME"
END_OF_TEXT
done
}<$USERFILE
cat >>$LOGFILE << 'END_OF_TEXT'
	echo "	or 	<RETURN> "
	goto prompt
	breaksw
    default:
	echo "Just checking."
	breaksw
endsw
unset resp
END_OF_TEXT
cat >> $LOGFILE < ${LOGFILE}.OLD

echo "Done."
echo "Okay, now log out and log in again to see how it works"
