#!/bin/bash

# 'builtin' command to use internal echo command
# echo'ed passwd will not show up in process list
builtin

if [ $# -gt 3 ]; then
   echo "Usage: mount-home"
   exit 1
fi

# record under which user we are running
# touch /tmp/uidtest

X11_INFO=""

while getopts "XU:" Option
do
  case $Option in
    X)  X11_INFO="yes"
    break
    ;;
    U)  UNAME=$OPTARG;
    break
    ;;
  esac
done
shift $(($OPTIND - 1))

if [ "$X11_INFO" = "yes" ]; then

	text="\
This script will mount your home directory's from other servers !

The password will NOT be displayed, but you HAVE to put the
mouse pointer in the xterm window !!!
(Text-Cursor should be solid black)

Or hit <STRG>+<C>, if you don't want to mount your home
directory's !
"
    dialog --infobox "$text" 15 70

fi

cd /

PASSWORD=""
if [ -z "$UNAME" ]; then 
  UNAME=$LOGNAME
fi
UUID=$(ypcat passwd | cat - /etc/passwd | grep "^$UNAME:" | cut -f 3 -d:)
HOMESRV=omikron
HOMEPREFIX=/export/home
if [ $UUID -gt 1000 ]; then
    HOMEPREFIX=/home/users
fi

LOG_FILE=$HOMEPREFIX/$UNAME/.mount-home.log
# delete LOG_File
if [ -f "$LOG_FILE" ]; then
	rm -f $LOG_FILE
fi

function getpwd () {
	# if stty, read password from stdin
    if stty -echo > /dev/null 2>&1; then
        echo -n "Enter your password ($UNAME): "
        read PASSWORD
        stty echo
		echo
    else
		# read password from pipe
        read PASSWORD
    fi
}

getpwd
if [ "$PASSWORD" = "" ]; then
	echo "No password entered !"
	exit 1
fi

#
# mount smbfs shares
#

#
# mount Q
#
if ! [ -d $HOMEPREFIX/$UNAME/Q ]; then
	mkdir -p $HOMEPREFIX/$UNAME/Q
fi
echo -n "Mounting Q ($HOMESRV) ... "
#SMBOPT="fmask=700,dmask=700"
SMBOPT="username=$UNAME,fmask=700,dmask=700"
 	 # smbmount.expect server share mountpoint username smboptions
echo "$PASSWORD" | smbmount.expect $HOMESRV $UNAME $HOMEPREFIX/$UNAME/Q \
	 		$SMBOPT > $LOG_FILE 2>&1
if ! [ "$?" -eq 0 ]; then
	  echo "failed !"
else
	  echo "done !"
fi

