#!/bin/bash
# setup_linux_accounts.sh

## This script sets up student Linux and Oracle accounts for SQL, PL/SQL,
## and other classes.  The accounts are named s1, s2, s3, etc, unless
## you choose a different starting number.  You also choose the number
## of accounts to set up, the default is 12.
##
## Note that both the Linux and the Oracle account will be removed
## completely, then re-created.
##
## This script must be run as root.
##
## On the command line, pass the directory where you have extracted
## the student files from the ZIP.  For example, if you extracted 
## o10sql.zip under /home/classes, creating /home/classes/o10sql/, then:
##
##   ./setup_linux_accounts.sh /home/classes/o10sql
##
## Before running this, set the Oracle SYSTEM and SYS passwords here.

## Oracle SYSTEM account password (default is often "manager")
ORACLE_SYSTEM_PW=oracle
## Oracle SYS account password (default is often "change_on_install")
ORACLE_SYS_PW=oracle

## Oracle home.  Adjust as necessary for your Oracle installation
ORACLE_HOME="/u01/app/oracle/product/11.1.0/db_1"; export ORACLE_HOME
PATH=$PATH:${ORACLE_HOME}/bin

## Accounts will be named s1, s2, etc. by default.
## If you want student1, student2, ... or sqlclass1, sqlclass2, etc,
##   prefix=student
## or
##   prefix=sqlclass
prefix=s

## Here we go.

if [ $# -ne 1 ]; then
  echo "usage: $0 student_file_source"
  echo "  where student_file_source is the location of the student file"
  echo "  directory extracted from the ZIP file"
  exit
fi

SF_DIR=$1

if [ ! -d ${SF_DIR} ] ; then
  echo "No student file directory ${SF_DIR}"
  echo "exiting."
  exit
fi

## Start the creation to the Linux user and the Oracle User.
AdminDir=${SF_DIR}/admin
if [ ! -d "${AdminDir}" ] ; then
  echo "Admin directory not found: ${AdminDir}"
  echo "Unable to run setup scripts.  Exiting."
  exit
fi

## Lob Data directory.  This is under the admin directory and must include
## the entire pathname up to and including the location of the lob data files.
LOB_DIR=${AdminDir}/lobdata

if [ -f ${ORACLE_HOME}/rdbms/admin/utlxplan.sql ]; then
  cp ${ORACLE_HOME}/rdbms/admin/utlxplan.sql ${AdminDir}
  echo -e "\n\nEXIT\n" >> ${AdminDir}/utlxplan.sql
else
  echo "Are you sure ORACLE_HOME is set correctly?  Unable to locate:"
  echo "${ORACLE_HOME}/rdbms/admin/utlxplan.sql"
  echo "Continuing, but student setup might be incomplete."
fi

if ! echo exit | sqlplus -logon -silent system/${ORACLE_SYSTEM_PW} >/dev/null 
then
  echo "Unable to run sqlplus as SYSTEM.  Make sure:"
  echo "  ORACLE_HOME and PATH environment variables are set correctly."
  echo "  The SYSTEM and SYS passwords in this script are correct.  Using:"
  echo "    SYSTEM/${ORACLE_SYSTEM_PW}    (in variable ORACLE_SYSTEM_PW)"
  echo "    SYS/${ORACLE_SYS_PW}    (in variable ORACLE_SYS_PW)"
  exit 2
fi
if ! echo exit | sqlplus -logon -silent sys/${ORACLE_SYS_PW} AS SYSDBA>/dev/null 
then
  echo "Unable to run sqlplus as SYS AS SYSDBA.  Make sure:"
  echo "  ORACLE_HOME and PATH environment variables are set correctly."
  echo "  The SYSTEM and SYS passwords in this script are correct.  Using:"
  echo "    SYSTEM/${ORACLE_SYSTEM_PW}    (in variable ORACLE_SYSTEM_PW)"
  echo "    SYS/${ORACLE_SYS_PW}    (in variable ORACLE_SYS_PW)"
  exit 2
fi

##
## Start of Functions ---------------------------------------------------------
##

## Create the linux user.
create_linux_user () {
  if [ "$#" -ne 2 ]; then
    echo "usage: create_linux_user username classfiledir"
    return;
  fi
  user=$1
  filedir=$2
  uid=`grep ^${user}: /etc/passwd | cut -d: -f3`
  if [ -n "${uid}" ]; then
    if [ ${uid} -le 100 ]; then
      echo "I'm sure you don't want to use the '${user}' account for this class."
      echo "skipping."
      return 1
    fi
    userhome=`grep ^${user}: /etc/passwd|cut -d: -f6`
    case "${userhome}" in
      # We arbitrarily exclude accounts with home directory names 6
      # characters or less (such as /, /root, /bin, /home/, etc.).
      ?|??|???|????|?????|??????)
        echo "Home directory path ${userhome} too short, it's probably important."
        echo "Skipping account ${user}."
        return 2
        ;;
      *)
        if [ -d ${userhome} ] ; then
          userdel -r ${user}
        else
          userdel ${user}
        fi
        if [ -d ${userhome} ] ; then
          rm -r ${userhome}
        fi
        ;;
    esac
  fi
  useradd -m ${user}
  echo ${user} | passwd --stdin ${user}
  userhome=`grep ^${user}: /etc/passwd|cut -d: -f6`
  cp -r ${filedir} ${userhome}
  subdir=`basename ${filedir}`
  chown -R ${user}:${user} ${userhome}/${subdir}
  return $?
}

##
## End Of Functions -----------------------------------------------------------
##

read -p "How many students? [12] " SCOUNT
  if [[ -z ${SCOUNT} || "${SCOUNT}" -lt 1 ]] ; then
    SCOUNT="12"
  fi
read -p "Starting number [1] : " start
  if [ -z ${start} ] ; then
    echo "starting at 1"
    start="1"
  fi
SCOUNT=$((${SCOUNT}+${start}))

## Minus the error checking, and assuming all scripts and files are here,
## for each student we:
##   create_linux_user ${user} ${SF_DIR}
##   sqlplus sys as sysdba create_oracle_user.sql ${user}
##   imp system file=student.dmp fromuser=student touser=${user}
##   sqlplus ${user}/${user} @$ORACLE_HOME/rdbms/admin/utlxplan.sql

while [ "${start}" -lt ${SCOUNT} ] ; do
  user="${prefix}${start}"
  create_linux_user ${user} ${SF_DIR} &&
  if [ -f ${AdminDir}/create_oracle_user.sql ]; then
    sqlplus sys/${ORACLE_SYS_PW} as sysdba @${AdminDir}/create_oracle_user.sql ${user}
    if [ -f ${AdminDir}/student.dmp ]; then
      imp userid=system/${ORACLE_SYSTEM_PW} file=${AdminDir}/student.dmp fromuser=student touser=${user}
    elif [ -f ${AdminDir}/create_student_tabs.sql ]; then
      sqlplus ${user}/${user} @${AdminDir}/create_student_tabs.sql << LoBdAtA
${LOB_DIR}
LoBdAtA
    else
      echo "Unable to load student data, student ${user} is NOT set up!"
    fi
    ## add exit at the bottom of utlxplan.sql
    if [ -f ${AdminDir}/utlxplan.sql ]; then
      sqlplus ${user}/${user} @${AdminDir}/utlxplan.sql
    fi
  else
    echo "Unable to find script ${AdminDir}/create_oracle_user.sql,"
    echo "${user} is NOT set up!"
  fi
  start=$((${start}+1))
done

