#!/bin/sh
#set -xv

# This script can be used to clean out student accounts
# from previous quarters, and create new student accounts.
#

# Written by AA Sako 09/13/1996
# 
# Rev 09/18/2002 - switched the home directories from /h to /home
# Rev 09/10/2005 - added prompts to set max number of accounts 
#                  to create
# Rev 09/18/2005 - added userdel to clean out passwd file and
#                  groups and shadow passwords

# This section cleans out the old student directories

clear
echo "This section will permanently delete all of the student"
echo "directories. Make sure that anything you want to save"
echo "has been backed up ... (If you want to skip this section"
echo "and proceed to the next section answer n to the prompt.)"
echo -en "\nProceed with removal? (y/n) "
read resp junk


#mkdir ~/home/student1
#mkdir ~/home/student1/sub1
#touch ~/home/student1/sub1/junk

resp=`echo $resp | tr [A-Z] [a-z]`
if [ $resp = 'y' ]
then
#  rm -r /home/student*
  rm -r /home/student*
  if [ $? = "0" ]
  then
    echo -e "\nFinished removing directories and deleting users"
  else
    echo -e "Get out the sonic screwdriver. There was a problem deleting the student directories"
  fi
fi

########################################################
# Deleting user accounts
########################################################

echo -e "Deleting user accounts. This is set to a default of 25. If there are"
echo -e " more, edit the script to change the number"

numAccounts=50
i=1
while [ $i -le $numAccounts ]
do
  echo "Removing user: student$i"
  /usr/sbin/userdel student$i
  if [ $? = "0" ]
  then
    echo -e "\nFinished removing student$i"
  else
    echo -e "Get out the sonic screwdriver. There was a problem removing student$i"
  fi
  i=`expr $i + 1`
done



######################################################
#
# This section creates the new user accounts
# You'll be prompted to see how many accounts you
# want to create. It's hard coded to set the default
# shell to /bin/tcsh. 

#
echo -e "\n\nReady to create new accounts. Note that the default"
echo -e "shell is being set to /bin/tcsh. If you want to skip"
echo -e "this section answer n at the prompt"

echo -en "Create student accounts? (y/n) " 
read resp junk

resp=`echo $resp | tr [A-Z] [a-z]`
if [ $resp = 'y' ]
then

  numStudents=1
  echo -en "How many students are there this quarter? "
  read numStudents junk

  i=1
  while [ $i -le $numStudents ]
  do
    /usr/sbin/adduser -s /bin/tcsh student$i

  if [ $? = "0" ]
  then
    echo student$i created
  else
    echo -e "Get out the sonic screwdriver. There was a problem creating the student directories"
    exit
  fi

  i=`expr $i + 1`
  done

fi

######################################################
#  Change password section
######################################################

i=1
while [ $i -le $numStudents ]
do
  echo "setting password for student$i"
#  echo e changeme\nchangeme | (passwd --stdin student$i) 
  echo -e "student$i:changeme" | /usr/sbin/chpasswd
  if [ $? = "0" ]
  then
    echo student$i password changed
  else
    echo -e "Get out the sonic screwdriver. There was a problem deleting the student directories"
  fi
  i=`expr $i + 1 ` 
done
