# setup
#
# This utility sets up the instructor's machine for the
# Tarantella Enterprise II Administration II course.

DEBUG=$1			# Debugging turned on if non-NULL

TTA_DIR=/opt/tarantella		# Usual directory for Tarantella installations
ACCT_PREFIX=student		# Prefix used on account names

PATH=$PATH:.			# Include the binaries on the CD-ROM in the
export PATH			# search path.

# preliminary_questions()
#
# Asks upfront all the questions necessary to setup the Tarantella
# server for the class
#
# Arguments
# None
preliminary_questions()
{
    clear

    # How many student accounts should we create?
    while true
    do
        NUM_STUDENTS=10
        echo "\nHow many student accounts would you like to create? [$NUM_STUDENTS] \c"
        read TEMP ; [ "$TEMP" = "" ] && TEMP="$NUM_STUDENTS"
        is_num $TEMP && NUM_STUDENTS=$TEMP && break 1
        echo "Non-numeric response: $TEMP"
    done


    # Make sure we know where the Tarantella binaries are.
    while true
    do
	# Find out the base directory.
        while true
        do
            if [ -d "$TTA_DIR" ]
            then break 1
            else echo "\n$TTA_DIR does not exist."
                 echo "What directory did you install Tarantella in? \c"
                 read TTA_DIR
            fi
        done

	# Find out the bin directory.
        TTA_BIN_DIR=$TTA_DIR/bin
        while true
        do
            if [ -d "$TTA_BIN_DIR" ]
            then break 1
            else echo "\n$TTA_BIN_DIR does not exist."
                 echo "What directory are the Tarantella binaries in? \c"
                 read TTA_BIN_DIR
            fi
        done

	# Make sure we found the most important binary of them all...
        [ -x "$TTA_BIN_DIR/tarantella" ] && break 1
        echo "\nHmmmm... Couldn't find $TTA_BIN_DIR/tarantella. Try again."
    done

    # What shell should we give users?
    while true
    do
        USER_SHELL=/usr/bin/ksh
        echo "\nWhat shell should we give users? [$USER_SHELL] \c"
        read TEMP ; [ "$TEMP" = "" ] && TEMP="$USER_SHELL"
        [ -x "$TEMP" ] && USER_SHELL=$TEMP && break 1
        echo "No such file: $TEMP"
    done

    # Where should home directories go?
    while true
    do
        HOME_DIR=/home
        echo "\nWhere should users' home directories reside? [$HOME_DIR] \c"
        read TEMP ; [ "$TEMP" = "" ] && TEMP="$HOME_DIR"
        [ -d "$TEMP" ] && HOME_DIR=$TEMP && break 1
        echo "No such directory: $TEMP"
    done

    # Where is the anonymous ftp directory?
    while true
    do
        FTP_DIR=$HOME_DIR/ftp/pub
        echo "\nWhere is the anonymous ftp directory? [$FTP_DIR] \c"
        read TEMP ; [ "$TEMP" = "" ] && TEMP="$FTP_DIR"
        [ -d "$TEMP" ] && FTP_DIR=$TEMP && break 1
        echo "No such directory: $TEMP"
    done
    # Make sure $FTP_DIR exists
    mkdir -p $FTP_DIR 2>/dev/null

    # Copy the "tools" directory to the anonymous ftp directory?
#    echo "\nInstall the \"tools\" directory? [no] \c"
#    read TEMP
#    case "$TEMP" in
#      [yY]*) TOOLS=yep ;;
#    esac

# Note if you would like to download and install tools and utilites that are
# useful during this course see the \tools\readme.htm or readme.txt file for
# a list of utilities and tools to download.
 
    export TTA_DIR TTA_BIN_DIR USER_SHELL HOME_DIR FTP_DIR

    echo
}

# is_num()
#
# Returns true if $1 is a number
#
# Arguments
# $1 is the supposed number to test.
is_num()
{
   STRIPPED=`echo $1 | tr -d -c "0-9"`
   [ "$STRIPPED" = "$1" ]
   return $?
}

# user_exists()
#
# Returns true if $1 is an existing Unix user. Alas, the
# "tarantella info --user" command appears to check /etc/passwd,
# so we have no clean way to check if a _Tarantella_ account
# exists. Sigh...
#
# Arguments
# $1 is the user whose existence we're testing for.
user_exists()
{
    # Does a Unix user already exist?
    awk -F":" '{print $1}' < /etc/passwd | grep "^$1$" > /dev/null
    [ $? = 0 ] && \
        echo "Error: A Unix user called $1 already exists." && return 0
    return 1
}

# create_unix_account()
#
# Creates Unix accounts.
#
# Arguments
# $1 is the name of the Unix account to create
create_unix_account()
{
   echo "Unix account... \c"
   useradd -d $HOME_DIR/$1 -s $USER_SHELL -c "TTA Admin. account for $1" -m $1
}

# create_tta_account()
#
# Creates Tarantella accounts.
#
# Arguments
# $1 is the name of the Tarantella account to create
create_tta_account()
{
   echo "Tarantella account... \c"
   $TTA_BIN_DIR/tarantella admin new_person --name ".../_ens/Org/$1" -label $1
}

# create_accounts()
#
# Calls the functions which create the Unix and Tarantella accounts.
#
# Arguments
# $1 is the name of the account to create
create_accounts()
{
    COUNT=1
    while [ "$COUNT" -le "$NUM_STUDENTS" ]
    do
        user_exists $1 && exit 1
        echo "Creating accounts for $ACCT_PREFIX${COUNT}... \c"
        create_unix_account "$ACCT_PREFIX${COUNT}"
        create_tta_account "$ACCT_PREFIX${COUNT}"
        echo "Done."
        COUNT=`expr $COUNT + 1`
    done
}

# set_passwords()
#
# Prompts to set the account passwords.
# Is there some way to set passwords automatically? passwd disallows
# redirection of stdin. Bummer.
#
# Arguments
# None
set_passwords()
{
    COUNT=1
    while [ "$COUNT" -le "$NUM_STUDENTS" ]
    do
        echo "\nSetting password for $ACCT_PREFIX$COUNT..."

        [ "$DEBUG" ] && echo ./autopw $ACCT_PREFIX$COUNT $ACCT_PREFIX$COUNT
        ./autopw $ACCT_PREFIX$COUNT $ACCT_PREFIX$COUNT >/devnull 2>&1

        COUNT=`expr $COUNT + 1`
    done
}

# add_link()
#
# Adds a link to a user's webtop
#
# Arguments
# $1 is the user whose webtop we're adding the link to.
# $2 is the application that we're adding.
add_link()
{
    echo "Adding $2 to $1's webtop... \c"
    $TTA_BIN_DIR/tarantella admin add_link --webtop ".../_ens/Org/$1" --link ".../_ens/Org/$2"
    echo "Done."
}

# check_file_exists()
#
# Checks for the existence of a file.
#
# Arguments
# $* are the binaries that we're looking for.
check_file_exists()
{
    for file in $*
    do
        if [ ! -f $file ]
        then echo "\nWARNING: Could not locate $file"
             echo "Press ENTER to continue... \c" ; read PAUSE
        fi
    done
}

# lesson01()
#
# Performs whatever actions are necessary for lesson01.
# Does _not_ copy the lab files. That's done in copy_labs().
#
# Arguments
# None
lesson01()
{
    echo "Lesson 1:  Introduction to Tarantella"
    create_accounts
    set_passwords
    echo "Done (Lesson 1)\n"
}

# lesson02()
#
# Performs whatever actions are necessary for lesson02.
# Does _not_ copy the lab files. That's done in copy_labs().
#
# Arguments
# None
lesson02()
{
    echo "Lesson 2:  Installing Tarantella"
    echo "Done (Lesson 2)\n"
}

# lesson03()
#
# Performs whatever actions are necessary for lesson03.
# Does _not_ copy the lab files. That's done in copy_labs().
#
# Arguments
# None
lesson03()
{
    echo "Lesson 3:  Modeling an Organization"
    
    # Create an object for notepad.exe
    echo "Creating an application object for notepad.exe... \c"
    $TTA_BIN_DIR/tarantella admin new_windows --name ".../_ens/Org/Notepad" --label "Notepad" --appserv --usehosts --winproto None --trylocal --app "c:\windows\notepad.exe" --displayusing AWTWindow
    echo "Done."

    # Place the object(s) created above onto users' webtops
    COUNT=1
    while [ "$COUNT" -le "$NUM_STUDENTS" ]
    do
        add_link $ACCT_PREFIX$COUNT Notepad
        add_link $ACCT_PREFIX$COUNT UserLinks
        add_link $ACCT_PREFIX$COUNT Applications
        COUNT=`expr $COUNT + 1`
    done

    # Make sure we have all the Unix binaries that we need in this lesson.
    check_file_exists /usr/X/bin/xeyes /usr/X/bin/puzzle /usr/dt/bin/dtsession /usr/X/bin/xcalc /usr/X/bin/xedit /usr/sbin/rtpm /bin/vi
    echo "Done (Lesson 3)\n"
}

# lesson04()
#
# Performs whatever actions are necessary for lesson04.
# Does _not_ copy the lab files. That's done in copy_labs().
#
# Arguments
# None
lesson04()
{
    echo "Lesson 4:  The Tarantella Native Client"
    echo "Done (Lesson 4)\n"
}

# lesson05()
#
# Performs whatever actions are necessary for lesson05.
# Does _not_ copy the lab files. That's done in copy_labs().
#
# Arguments
# None
lesson05()
{
    echo "Lesson 5:  Printing under Tarantella"
    echo "Done (Lesson 5)\n"
}

# lesson06()
#
# Performs whatever actions are necessary for lesson06.
# Does _not_ copy the lab files. That's done in copy_labs().
#
# Arguments
# None
lesson06()
{
    echo "Lesson 6:  The Tarantella Security Pack"
    echo "Done (Lesson 6)\n"
}

# lesson07()
#
# Performs whatever actions are necessary for lesson07.
# Does _not_ copy the lab files. That's done in copy_labs().
#
# Arguments
# None
lesson07()
{
    echo "Lesson 7:  Tarantella Load Balancing"
    echo "Done (Lesson 7)\n"
}

# lesson08()
#
# Performs whatever actions are necessary for lesson08.
# Does _not_ copy the lab files. That's done in copy_labs().
#
# Arguments
# None
lesson08()
{
    echo "Lesson 8:  Network Basics and Tarantella"
    echo "Done (Lesson 8)\n"
}

# lesson09()
#
# Performs whatever actions are necessary for lesson09.
# Does _not_ copy the lab files. That's done in copy_labs().
#
# Arguments
# None
lesson09()
{
    echo "Lesson 9:  The X Window System"

    # Make sure we have all the Unix binaries that we need in this lesson.
    check_file_exists /usr/X/bin/xdpyinfo /usr/X/bin/xset /usr/X/bin/xedit /usr/X/bin/editres /usr/X/bin/xfontsel /usr/X/bin/xmodmap /usr/X/bin/xterm /usr/dt/bin/dtterm /bin/vi

    echo "Done (Lesson 9)\n"
}

# lesson10()
#
# Performs whatever actions are necessary for lesson10.
# Does _not_ copy the lab files. That's done in copy_labs().
#
# Arguments
# None
lesson10()
{
    echo "Lesson 10:  Customizing Tarantella"
    echo "Done (Lesson 10)\n"
}

# lesson11()
#
# Performs whatever actions are necessary for lesson11.
# Does _not_ copy the lab files. That's done in copy_labs().
#
# Arguments
# None
lesson11()
{
    echo "Lesson 11:  The Windows Connectivity Pack"
    echo "Done (Lesson 11)\n"
}

# lesson12()
#
# Performs whatever actions are necessary for lesson12.
# Does _not_ copy the lab files. That's done in copy_labs().
#
# Arguments
# None
lesson12()
{
    echo "Lesson 12:  The Tarantella 3270 Connectivity Pack"
    echo "Done (Lesson 12)\n"
}

# lessons()
#
# Calls the functions which prepare the server for the various lessons
# in the course.
#
# Arguments
# None
lessons()
{
    lesson01
    lesson02
    lesson03
    lesson04
    lesson05
    lesson06
    lesson07
    lesson08
    lesson09
    lesson10
    lesson11
    lesson12
}

# copy_labs()
#
# Copies the Lab files from the CD-ROM to the anonymous FTP directory.
#
# Arguments
# None
copy_labs()
{
    echo "Instaling the lab directories... \c"

    copy -rom ./labs $FTP_DIR/labs
    echo "Done\n"
}

# install_tools()
#
# Copies the directory on the CD-ROM which contains extra tools
# that may be useful to the anonymous FTP directory.
#
# NOTE please refer to the /tools/readme.htm or readme.txt to download
# the tools. They are not available on the curriculum CD-ROM. 
# Arguments
# None
# install_tools()
# {
#   [ ! "$TOOLS" ] && return
#    echo "Installing the \"tools\" directory... \c"
# Copy the \"tools\" directory to $FTP_DIR
#    copy -rom ./tools $FTP_DIR/tools
#    echo "Done\n"
# }

# info()
#
# The Info directory on the CD-ROM contains information useful to the
# instructor. This directory does _not_ get copied to the anonymous
# FTP directory, so we'll remind the instructor that it's there.
#
# Arguments
# None
info()
{
    echo "Be sure to check out the \"info\" directory on the CD-ROM"
    echo "for additional Tarantella related information."
}

# reminders()
#
# Reminders for the instructor.
#
# Arguments
# None
reminders()
{
    echo "\nThe student account names are ${ACCT_PREFIX}1 through ${ACCT_PREFIX}${NUM_STUDENTS}."
    echo "The passwords each account is the same as the name of the account."
}

# valid_os()
#
# Is this being run on a valid operating system (ie. UnixWare)?
#
# Arguments
# None
valid_os()
{
    UNAME_INFO="`uname -a`"

    case "`echo $UNAME_INFO | tr A-Z a-z`" in
      unixware*) return ;;
    esac

    echo "WARNING:  The courseware CD-ROM is intended to be installed on UnixWare 7.\n"
    echo "          uname -a reported:\n"
    echo "\t\t$UNAME_INFO\n"
    echo "Do you want to continue anyway? [no] \c"
    read TEMP
    case "$TEMP" in
      [yY]*) return ;;
    esac

    echo "ABORTED!"
    exit 1
}

# tta_installed()
#
# Is Tarantella installed? If not, abort.
#
# Arguments
# None
tta_installed()
{
    pkginfo tta > /dev/null 2>&1 || {
        echo "ERROR: You must first install Tarantella."
        echo "       The UnixWare 7 .shx is in ./labs/lab01"
        echo "       Aborting..."
        exit 1
    }
}

################################################################################
################################################################################
##                                    MAIN                                    ##
################################################################################
################################################################################

valid_os

tta_installed

preliminary_questions

copy_labs

lessons

# install_tools

info

reminders

exit
