#!/bin/ksh
#
# Please report changes and bugs to egan@cbs.cis.com
# version .1
# 10/24/94
#
# NOTES:
# 1.  Use / where you would use \
# 2.  Use - where you would use /
#     e.g.  DOS:    NET TIME /SET \\CBS
#           SAMBA:  net time -set //winnt
# 3.  net logon/logoff is intended to be used with smbmount when it becomes
#     available
# 4.  net use (see 3.)
# 5.  net password (dunno yet)
# 6.  net init (?)
# 7.  net view does not search other workgroups or list the machines in your
#     workgroup.  Will when samba does.
#


PATH=/usr/samba/bin:$PATH

WORKGROUP=SLC
COMMENT="AIX 3.2.5 Server"
LOGFILE=/usr/samba/etc/smb.log
DL=0

SAMBAVER=$(smbclient -h | head -2 | tail -1 | awk '{print $2}')
SMBCONF=/usr/samba/etc/smb.conf
ROOTDIR=$(grep "root dir" $SMBCONF | awk -F= '{print $2}' | awk '{print $1}')
if [ -z "$ROOTDIR" ]
then
	ROOTDIR=/
fi
if [ -z "$PAGER" ]
then
	PAGER=more
fi


COMMAND=$1
case $COMMAND in
	config)
		if [ "$2" = "-?" ]
		then
			echo
			echo "Display your current Samba settings."
			echo
			echo "net config"
			echo
		else
			echo "Computer name                  //$(uname -n)"
			echo "User name                      $LOGNAME"
			echo
			echo "Software version               $(uname -s) $(uname -v).$(uname -r)"
			echo "Samba version                  $SAMBAVER"
			echo "Workstation root directory     $ROOTDIR"
			echo
			echo "Workgroup                      $WORKGROUP"
		fi
		;;
	diag)
		if [ "$2" = "-?" ]
		then
			echo
			echo "Display your Samba logfile."
			echo
			echo "net diag"
			echo	
		else
			if [ -r $LOGFILE ]
			then
				tail -f $LOGFILE
			else
				echo "net diag: unable to read $LOGFILE"
			fi
		fi
		;;
	logoff)
		if [ "$2" = "-?" ]
		then
			echo
			echo "Breaks the connections between your computer and the"
			echo "shared resources to which it is connected."
			echo
			echo "net logoff [-y]"
			echo
			echo "  -y          Carries out the net logoff command without"
			echo "              first prompting you to provide information or"
			echo "              confirm actions."
			echo
		else
			echo "Currently unavailable."
		fi
		;;
	logon)
		if [ "$2" = "-?" ]
		then
			echo
			echo "Identifies you as a member of a workgroup and"
			echo "reestablishes your persistent connections."
			echo
			echo "net logon [-y]"
			echo
			echo "  -y          Carries out the net logon command without"
			echo "              first prompting you to provide information or"
			echo "              confirm actions."
			echo
		else
			echo "Currently unavailable."
		fi
		;;
	password)
		if [ "$2" = "-?" ]
		then
			echo
			echo "Changes your logon password."
			echo
			echo "net password //computer | -domain:name [user [oldpassword [newpassword]]]"
			echo
			echo "  oldpassword   Specifies your current password."
			echo "  newpassword   Specifies your new password.  It can have as"
			echo "                many as 14 characters."
			echo "  computer      Specifies the Windows NT or LAN Manager server"
			echo "                on which you want to change your password."
			echo "  -domain       Specifies that you want to change your"
			echo "                password on a Windows NT or LAN Manager domain."
			echo "  name          Specifies the Windows NT or LAN Manager domain on which"
			echo "                you want to change your password."
			echo "  user          Specifies your Windows NT or LAN Manager user name."
			echo
		else
			echo "Currently unavailable."
		fi
		;;
	print)
		if [ "$2" = "-?" ]
		then
			echo
			echo "Displays information about the print queue on a shared or local"
			echo "printer."
			echo
			echo "net print //computer[/printer]"
			echo "net print [printer]"
			echo
			echo "  computer   Specifies the name of the computer whose"
			echo "             print queue you want information about."
			echo "  printer    Specifies the name of the printer you "
			echo "             want information about."
			echo
			echo "When you specify the name of a computer by using the NET PRINT"
			echo "command, you receive information about the print queues on"
			echo "each of the shared printers that are connected to the computer."
			echo
		else
			if [ -z "$2" ]
			then
				lpstat
			else
				lpstat -a$2
			fi
		fi
		;;
	ps)
		if [ "$2" = "-?" ]
		then
			echo
			echo "Displays information about samba processes."
			echo
			echo "net ps"
			echo
		else
			ps -ef | grep mbd | grep -v grep
		fi
		;;
	restart)
		if [ "$2" = "-?" ]
		then
			echo
			echo "Restarts services."
			echo
			echo "net restart [-y]"
			echo
			echo "  -y          Carries out the net stop command without"
			echo "              first prompting you to provide information or"
			echo "              confirm actions."
			echo
		else
			if [ "$LOGNAME" = "root" ]
			then
				if [ "$2" = "-y" ]
				then
					net stop -y
					net start -y
				else
					echo
					echo "Do you really want to restart samba services? \c"
					read YN
					case $YN in
						y|Y)
							net stop -y
							net start -y
							;;
					esac
				fi
			else
				echo
				echo "net restart: You must be logged in as root."
				echo
			fi
		fi
		;;
	start)
		if [ "$2" = "-?" ]
		then
			echo
			echo "Starts services."
			echo
			echo "net start [-y]"
			echo
			echo "  -y          Carries out the net start command without"
			echo "              first prompting you to provide information or"
			echo "              confirm actions."
			echo
		else
			if [ "$LOGNAME" = "root" ]
			then
				if [ "$2" = "-y" ]
				then
					nmbd -D -d $DL -G $WORKGROUP -C "$COMMENT" -l $LOGFILE
				else
					echo
					echo "Do you really want to start samba services? \c"
					read YN
					case $YN in
						y|Y)
							nmbd -D -d $DL -G $WORKGROUP -C "$COMMENT" -l $LOGFILE
							;;
					esac
				fi
			else
				echo
				echo "net start: You must be logged in as root."
				echo
			fi
		fi
		;;
	stop)
		if [ "$2" = "-?" ]
		then
			echo
			echo "Stops services."
			echo
			echo "net stop [-y]"
			echo
			echo "  -y          Carries out the net stop command without"
			echo "              first prompting you to provide information or"
			echo "              confirm actions."
			echo
		else
			if [ "$LOGNAME" = "root" ]
			then
				if [ "$2" = "-y" ]
				then
					for i in $((ps -e | \
						grep smbd; ps -e | \
						grep nmbd) | grep -v grep | \
						awk '{print $1}')
					do
						kill -1 $i >/dev/null 2>&1
						sleep 1
						kill -9 $i >/dev/null 2>&1
					done
				else
					echo
					echo "Do you really want to stop samba services? \c"
					read YN
					case $YN in
						y|Y)
							for i in $((ps -e | \
								grep smbd; ps -e | \
								grep nmbd) | grep -v grep | \
								awk '{print $1}')
							do
								kill -1 $i >/dev/null 2>&1
								sleep 1
								kill -9 $i >/dev/null 2>&1
							done
							;;
					esac
				fi
			else
				echo
				echo "net stop: You must be logged in as root."
				echo
			fi
		fi
		;;
	time)
		if [ "$2" = "-?" ]
		then
			echo
			echo "Displays the time on or synchronizes your computer's "
			echo "clock with the shared clock on a Microsoft Windows for "
			echo "Workgroups, Windows NT, LAN Manager, or Samba time server."
			echo
			echo "net time //computer [-set]"
			echo
			echo "  computer    Specifies the name of the computer (time"
			echo "              server) whose time you want to check or"
			echo "              synchronize your computer's clock with."
			echo "  -set        Synchronizes your computer's clock with the"
			echo "              clock on the computer or workgroup you"
			echo "              specify."
			echo
		else
			HOST=$(echo $2 | tr -d '/')
			if [ -z "$HOST" ]
			then
				echo
				echo "net time: You must supply a host name."
				echo
			else
				if [ "$3" = "-set" ]
				then
					if [ "$LOGNAME" = "root" ]
					then
						set $(smbclient -L $HOST | head -1)
						case $5 in
							Jan)
								MM=01
								;;
							Feb)
								MM=02
								;;
							Mar)
								MM=03
								;;
							Apr)
								MM=04
								;;
							May)
								MM=05
								;;
							Jun)
								MM=06
								;;
							Jul)
								MM=07
								;;
							Aug)
								MM=08
								;;
							Sep)
								MM=09
								;;
							Oct)
								MM=10
								;;
							Nov)
								MM=11
								;;
							Dec)
								MM=12
								;;
						esac
						DD=$6
						integer day=$6
						if ((day < 10))
						then
							DD="0$day"
						fi
						YY=$(echo $8 | awk '{print substr($1,3,2)}')
						set $(echo $7 | tr ':' ' ')
						date ${YY}${MM}${DD}${1}${2}.${3}
					else
						echo
						echo "net time //$HOST -set: You must be logged in as root."
						echo
					fi
				else
					smbclient -L $HOST | head -1 \
						| eval sed 's/Server\ time\ is/Current\ time\ at\ \\/\\/$HOST\ is/'
				fi
			fi
		fi
		;;
	use)
		if [ "$2" = "-?" ]
		then
			echo
			echo "Connects or disconnects your computer from a shared"
			echo "resource or displays information about your"
			echo "connections."
			echo
			echo "net use /dir [//computer/directory [password | ?]]"
			echo "    [-persistent:yes | no] [-savepw:no] [-y] [-n] "
			echo
			echo "net use /dir | //computer/directory -delete [-y] "
			echo
			echo "net use -persistent:yes | no | list | save | clear [-y] [-n]"
			echo
			echo "  dir         Specifies the unix directory you assign to a"
			echo "              shared directory."
			echo "  computer    Specifies the name of the computer sharing"
			echo "              the resource."
			echo "  directory   Specifies the name of the shared directory."
			echo "  printer     Specifies the name of the shared printer."
			echo "  password    Specifies the password for the shared "
			echo "              resource, if any."
			echo "  ?           Specifies that you want to be prompted for the "
			echo "              password of the shared resource. You don't "
			echo "              need to use this option unless the password is "
			echo "              optional."
			echo "  -persistent Specifies which connections should be"
			echo "              restored the next time you log on to"
			echo "              the network. It must be followed"
			echo "              by one of the values below:"
			echo "              yes   Specifies that the connection you are"
			echo "                    making and any subsequent connections"
			echo "                    should be persistent."
			echo "              no    Specifies that the connection you are"
			echo "                    making and any subsequent connections"
			echo "                    should not be persistent."
			echo "              list  Lists your persistent connections."
			echo "              save  Specifies that all current"
			echo "                    connections should be persistent."
			echo "              clear Clears your persistent connections."
			echo "  -savepw:no  Specifies that the password you type"
			echo "              should not be saved in your password-list"
			echo "              file. You need to retype the password the"
			echo "              next time you connect to this resource."
			echo "  -yes -y     Carries out the net use command without"
			echo "              first prompting you to provide information or"
			echo "              confirm actions."
			echo "  -delete     Breaks the specified connection to a shared"
			echo "              resource."
			echo "  -no -n      Carries out the net use command, responding "
			echo "              with NO automatically when you are prompted"
			echo "              to confirm actions."
			echo
			echo "To list all of your connections, type net use without"
			echo "options."
			echo
			echo "To see this information one screen at a time, type the"
			echo "following at the command prompt:"
			echo
		else
			echo "Currently unavailable."
		fi
		;;
	ver)
		if [ "$2" = "-?" ]
		then
			echo
			echo "Displays the type and version number of the MS LAN"
			echo "redirector you are using."
			echo
			echo "  net ver"
			echo
		else
			echo "Samba $SAMBAVER"
		fi
		;;
	view)
		if [ "$2" = "-?" ]
		then
			echo
			echo "Displays a list of computers in a specified workgroup or"
			echo "the shared resources available on a specified computer."
			echo
			echo "  net view [//computer]"
			echo "  net view [-workgroup:wgname] (unsupported)"
			echo "  net view [-IP:computer]"
			echo
			echo "  computer    Specifies the name of the computer whose"
			echo "              shared resources you want to see listed."
			echo "  -workgroup  Specifies that you want to view the names "
			echo "              of the computers in another workgroup that"
			echo "              share resources. "
			echo "  wgname      Specifies the name of the workgroup whose"
			echo "              computer names you want to view."
			echo "  -IP         Specifies IP address of remote computer."
			echo
			echo "To display a list of computers in your workgroup that share"
			echo "resources, type NET VIEW without options."
			echo
		else
			HOST=$(echo $2 | tr -d '/')
			if [ -z "$HOST" ]
			then
				echo
				echo "net view: You must supply a host name."
				echo
			else
				FC=$(echo $HOST | awk '{print substr($1,1,1)}')
				if [ "$FC" = "-" ]
				then
					echo $HOST | if grep : >/dev/null 2>&1
					then
						IP=$(echo $HOST | \
							awk -F: '{print substr($1,2,length($1)-1)}')
						COMP=$(echo $HOST | awk -F: '{print $2}')
						echo "Shared resources at -$IP:$COMP"
						smbclient -L $COMP -I $IP | tail +3 | sed 's/^ //'
					else
						echo
						echo "net view:  Must be in -IP:computer format."
						echo
					fi
				else
					echo "Shared resources at //$HOST"
					smbclient -L $HOST | tail +3 | sed 's/^	//'
				fi
			fi
		fi
		;;
	*)
		echo
		echo "For more information about a specific net"
		echo "command, type the command name followed by -?"
		echo "(for example, net view -?)."
		echo
		echo "net          Same as 'net help'"
		echo "net config   Displays information about your Samba settings."
		echo "net diag     tail -f LOGFILE"
		echo "net help     Provides information about commands."
		echo "net logoff   Breaks the connection between your computer and"
		echo "             the shared resources to which it is connected."
		echo "net logon    Identifies you as a member of a workgroup and"
		echo "             reestablishes your persistent connections."
		echo "net password Changes your logon password."
		echo "net print    Displays information about print queues."
		echo "net ps       Displays information about samba processes."
		echo "net restart  Restarts services."
		echo "net start    Starts services."
		echo "net stop     Stops services."
		echo "net time     Displays the time on or synchronizes your"
		echo "             computer's clock with the clock on a Microsoft"
		echo "             Windows for Workgroups, Windows NT, LAN Manager,"
		echo "             or Samba time server."
		echo "net use      Connects to or disconnects from a shared"
		echo "             resource or displays information about"
		echo "             connections."
		echo "net ver      Displays the type and version number of the"
		echo "             MS LAN redirector you are using."
		echo "net view     Displays a list of computers that share"
		echo "             resources or a list of shared resources"
		echo "             on a specific computer."
		;;
esac