#!/bin/sh
#
# /usr/local/sbin/adduser.local
# Copyright 2001-2006 Jonas Smedegaard <dr@jones.dk>
#
# $Id: adduser.local,v 1.26 2006-08-25 04:39:27 jonas Exp $
#
# Adduser additions for Redpill networks
#
# TODO: Allow local overriding of using /etc/mailname
# TODO: Only ask additional questions if real user
# TODO: handle samba passwords in /var/lib/samba/passdb.tdb
# TODO: Invent a way to flag for INTERACTIVE without abusing VERBOSE
#

set -e

NEWUSERNAME=$1
NEWUID=$2
NEWGID=$3
NEWHOMEDIR=$4

fullname()	{ getent passwd $NEWUSERNAME | awk -F: '{print $5}' | awk -F, '{print $1}'; }
maildomain()	{ if [ -r /etc/mailname ]; then head -n 1 /etc/mailname; fi; }

listlists()	{ if [ -x /usr/sbin/list_lists ]; then /usr/sbin/list_lists -ab -V "$1"; fi; }
add2list()	{ /bin/echo "$1" | /usr/sbin/add_members -w y -a y -r - "$2"; }

. /etc/adduser.conf

# Ignore non-human accounts silently
[ "$NEWUID" -ge "$FIRST_UID" -a "$NEWUID" -le "$LAST_UID" ] || exit 0

[ -f /etc/local/users.conf ] && . /etc/local/users.conf

# Samba password
#FIXME: Check if enabled in samba.conf (or included files!)
if [ -n "$VERBOSE" ] && [ "$VERBOSE" -gt 0 ] && ([ -x /usr/bin/pdbedit ] || [ -x /usr/bin/smbpasswd ]); then
	echo -n "Add samba access to the account (y/N)? "
	read sambaaccount
	case $sambaaccount in
	    y|Y)
		if [ -x /usr/bin/pdbedit ]; then
			/usr/bin/pdbedit -a -u "$NEWUSERNAME"
		elif [ -x /usr/bin/smbpasswd ] && [ -f /etc/samba/smbpasswd ]; then
			/usr/bin/smbpasswd -a $NEWUSERNAME
		fi
		;;
	esac
fi

# Mail forwarding
if [ -n "$VERBOSE" ] && [ "$VERBOSE" -gt 0 ] && [ -x /usr/local/sbin/userforward ]; then
	echo -n "Forward mail to an external account (y/N)? "
	read mailforward
	case $mailforward in
	    y|Y)
		echo -n "Enter external email address: "
		read mail_address
		case $mail_address in
		    ?*@?*.*)
			/usr/local/sbin/userforward $NEWUSERNAME $mail_address
			;;
		    ?*)
			echo "That wasn't a proper email address - skipping..."
			;;
		esac
		;;
	esac
fi

# Mailing lists
maildomain="`maildomain`"
if [ -n "$VERBOSE" ] && [ "$VERBOSE" -gt 0 ] && [ -n "$maildomain" ]; then
	echo -n "Subscribe $NEWUSERNAME@$maildomain to mailinglists (y/N)? "
	read subscribe
	case $subscribe in
	    y|Y)
		if [ -n "$fullname" ]; then
			subscriber="`fullname` <$NEWUSERNAME@$maildomain>"
		else
			subscriber="$NEWUSERNAME@$maildomain"
		fi
		lists="`listlists "$maildomain"`"
		if [ -z "$lists" ]; then
			echo "No mailinglists found."
		fi
		for list in $lists; do
			echo -n "Subscribe to mailinglist $list (y/N)? "
			read subscribe
			case $subscribe in
			    y|Y)
				add2list "$subscriber" "$list";;
			esac
		done
	esac
fi

if [ -f /etc/local/users.conf ]; then
	. /etc/local/users.conf

	if [ -n "$USERS_GROUPNAME" ]; then
		addgroup $NEWUSERNAME $USERS_GROUPNAME
	fi

	if [ -n "$VERBOSE" ] && [ "$VERBOSE" -gt 0 ] && [ -n "$REALUSERS_GROUPNAME" ]; then
		echo "This system has both dummy and real users:"
		echo "    Dummy users have read access to specific files."
		echo "    Real users have read/write access to personal files."
		echo -n "Is this a dummy user (Y/n)? "
		read realuser
		case $realuser in
		    y|Y|"")
			;;
		    *)
			addgroup $NEWUSERNAME $REALUSERS_GROUPNAME
			;;
		esac
	fi
fi

#if [ -n "$VERBOSE" ] && [ "$VERBOSE" -gt 0 ] && [ -n "$REALUSERS_GROUPNAME" ]; then
#	cat <<+
#Is this a remote or local user (with need for physical
#access to devices like CD-ROM, soundcard and modem needed)?
#
#+
if [ -n "$VERBOSE" ] && [ "$VERBOSE" -gt 0 ]; then
	echo -n "Grant this user access to local utilities (y/N)?"
	read localuser
	case $localuser in
	    y|Y)
		for group in floppy dialout cdrom audio video games; do
			addgroup $NEWUSERNAME $group
		done
		;;
	    *)
		;;
	esac
fi

if [ -x /usr/local/sbin/user-init ]; then
	/usr/local/sbin/user-init $NEWUSERNAME
fi