#!/bin/sh

#Bo Bayles
#Student No. 12033806
#bmb3h6@umr.edu
#CS 284 Assignment 1

#dialog is a neat utility I found at the "Great shell scripting reference"
dialog --backtitle "Bo Bayles' Administrator Toolkit (20070830)"\
       --title "ADMINISTRATOR MENU"\
       --menu "Move using [UP] [DOWN],[Enter] to Select" 15 60 10\
         1 "Shows who is registered to this machine"\
         2 "Shows user disk usage"\
         3 "Shows what processes are running"\
         4 "Shows who is online" 2>/tmp/menuitem.$$
         #Hang onto the user's choice in a temporary file

menuitem=`cat /tmp/menuitem.$$`
#Save the user's choice out of the temporary file

if [ "$menuitem" = "1" ]
#Use double quotes and the = comparator because bash flips out if you
#cancel and don't have an input
then
  echo "The following users are registered"
  echo "---------------------------------"
  cat /etc/passwd | cut -f1-1 -d':'
  #The first field delimited by : in /etc/passwd is the userid
  echo "--------------------------------"
fi
#Could do nested if-else's, but it's prettier not to and not even really more
#efficient in bash.

if [ "$menuitem" = "2" ]
then
  echo "Disk Usage By User"
  cat /etc/passwd | while read pwdLine
  #read /etc/passwd into pwdLine one line at a time
  do
    user=$(echo $pwdLine | cut -f1-1 -d':')
    #Get the userid out of the current line of /etc/passwd
    homedir=$(echo $pwdLine | cut -f6-6 -d':')
    #Get the user's home directory out of /etc/passwd - it's field 6
    if [ "$homedir" != "/" ]
    #Ignore / as a home directory
    then
      du $homedir >& /dev/null
      #Just testing to see if the directory is accessible...
      status=$?
      #...if it is, du sets $? to 0, otherwise it sets it to 1.
      if [ $status -ne 1 ]
      then
        usage=$(du -c -h $homedir | tail -n 1 | cut -f1-1)
        #Just get the total, which is the firtst field of the last line
        echo "----------------------"
        echo " " $user"'s directory is" $homedir and uses $usage
      else
        echo "---------------------"
        echo " " $user"'s directory is unaccessible."
      fi
    else
      echo "---------------------"
      echo " " $user"'s directory is" $homedir.
    fi
  done
  echo  "---------------------"
fi

if [ "$menuitem" = "3" ]
then
  echo "Choose a user:"
  echo "---------------"
  count=0
  for users in $(who -u | cut -f1-1 -d' ')
  #Using a for loop to keep track of the number of iterations, because bash is
  #flipping stupid about variable scoping.
  do
    let count=$count+1
    userArray[$count]=$users
    echo $count")" ${userArray[$count]}
  done

  echo -n "Enter your choice: "
  declare -i userChoice
  #Cast the response as an integer to satisfy bash's weird error handling
  read
  userChoice=$REPLY
  #Assign the default user entry variable to the typecast integer...
  if [ $userChoice -gt 0 ] && [ $userChoice -le $count ]
  #...otherwise bash exits with an error here if there was no input
  #Using this if block to make sure the user entered a valid number.
  then
    echo "  "${userArray[$userChoice]} is running
    echo "----------------------------------"
    ps -u ${userArray[$userChoice]}
    echo "----------------------------------"
  else
    echo "Invalid index."
  fi
fi

if [ "$menuitem" = "4" ]
then
  echo "The following users are online"
  echo "-----------------------------"
  who -u | cut -f1-1 -d' '
  #First field of who -u is the online user list.
fi

echo " Exiting..."

